java实现扫雷小游戏
本文实例为大家分享了java实现扫雷小游戏的具体代码,供大家参考,具体内容如下
代码:
packageGUI; importjava.util.Random; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.IOException; importjava.awt.BorderLayout; importjava.awt.GridLayout; importjava.awt.Font; importjava.awt.Color; importjava.awt.EventQueue; importjava.awt.event.InputEvent; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.event.MouseAdapter; importjava.awt.event.MouseEvent; importjava.awt.event.WindowAdapter; importjava.awt.event.WindowEvent; importjavax.swing.JFrame; importjavax.swing.JLabel; importjavax.swing.JPanel; importjavax.swing.JButton; importjavax.swing.JTextField; importjavax.swing.JMenuBar; importjavax.swing.JMenu; importjavax.swing.JMenuItem; importjavax.swing.JOptionPane; importjavax.swing.UIManager; importjavax.swing.ImageIcon; importjavax.swing.BorderFactory; importjavax.swing.border.EmptyBorder; publicclassMineSweeperFrameextendsJFrame{ //游戏模式编号 publicstaticfinalintCustom_Mode=0; publicstaticfinalintElementary_Mode=1; publicstaticfinalintIntermediate_Mode=2; publicstaticfinalintAdvanced_Mode=3; //游戏模式参数 publicstaticfinalint[]Elementary={9,9,10,Elementary_Mode}; publicstaticfinalint[]Intermediate={16,16,40,Intermediate_Mode}; publicstaticfinalint[]Advanced={16,30,99,Advanced_Mode}; //游戏字体、图片、文字颜色 privatestaticfinalFontFontC=newFont("Consolas",Font.PLAIN,22); privatestaticfinalFontFontD=newFont("等线",Font.PLAIN,20); privatestaticfinalImageIconimgicon=newImageIcon( MineSweeperFrame.class.getResource("/Images/icon.jpg")); privatestaticfinalImageIconimgflag=newImageIcon( MineSweeperFrame.class.getResource("/Images/flag.png")); privatestaticfinalImageIconimgbomb=newImageIcon( MineSweeperFrame.class.getResource("/Images/bomb.png")); privatestaticfinalColor[]NumberColor={Color.blue, Color.green,Color.orange,Color.magenta, Color.red,Color.cyan,Color.black,Color.gray}; //数据文件及高分记录 privatestaticFilefile=newFile("MineSweeper.txt"); privatestaticintHighScore_Elementary; privatestaticintHighScore_Intermediate; privatestaticintHighScore_Advanced; privateintgamemode; privateintrow; privateintcolumn; privateintarea; privateintminecount; privateintflagcount; privateintenablecount; privateboolean[][]minearea; privateboolean[][]flagmark; privatebooleanisGameOver; privatebooleanisFirstClick; privateTimeThreadtimeThread; privateJPanelcontentPane; privateJMenuBarmenuBar; privateJPanelpnlCount; privateJPanelpnlMine; privateMineButton[][]btnMine; privateJTextFieldtxtTime; privateJTextFieldtxtCount; /** *Launchtheapplication. */ publicstaticvoidmain(String[]args){ int[]i=getDataFromFile(); EventQueue.invokeLater(newRunnable(){ publicvoidrun(){ try{ newMineSweeperFrame(i[0],i[1],i[2],i[3]); }catch(Exceptione){ e.printStackTrace(); } } }); } /** *Createtheframe. */ publicMineSweeperFrame(introw,intcolumn,intminecount,intmode){ //设置各组件的默认字体 UIManager.put("Menu.font",FontD); UIManager.put("Label.font",FontD); UIManager.put("Button.font",FontD); UIManager.put("MenuBar.font",FontD); UIManager.put("MenuItem.font",FontD); UIManager.put("TextField.font",FontD); UIManager.put("OptionPane.buttonFont",FontD); UIManager.put("OptionPane.messageFont",FontD); setTitle("扫雷"); setResizable(false);//设置窗口不可改变大小 setVisible(true);//设置窗口初始可见 setIconImage(imgicon.getImage());//设置窗体的icon setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //注册监听器,响应窗口关闭事件 addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ saveDataToFile(); } }); menuBar=newJMenuBar(); setJMenuBar(menuBar); JMenumGame=newJMenu("游戏"); menuBar.add(mGame); JMenumRestart=newJMenu("重新开始"); mGame.add(mRestart); JMenuItemmitRestart=newJMenuItem("重新开始本局"); mitRestart.addActionListener(newRestartListener()); mRestart.add(mitRestart); JMenuItemmitReset=newJMenuItem("开始新的游戏"); mitReset.addActionListener(newResetMineListener()); mRestart.add(mitReset); JMenumDifficulty=newJMenu("难度设置"); mGame.add(mDifficulty); JMenuItemmitElementary=newJMenuItem("初级"); mitElementary.addActionListener(newResetModeListener(Elementary)); mDifficulty.add(mitElementary); JMenuItemmitIntermediate=newJMenuItem("中级"); mitIntermediate.addActionListener(newResetModeListener(Intermediate)); mDifficulty.add(mitIntermediate); JMenuItemmitAdvanced=newJMenuItem("高级"); mitAdvanced.addActionListener(newResetModeListener(Advanced)); mDifficulty.add(mitAdvanced); JMenuItemmitCustom=newJMenuItem("自定义"); mitCustom.addActionListener(newCustomSizeFrameListener()); mDifficulty.add(mitCustom); JMenuItemmitHighScore=newJMenuItem("高分榜"); mitHighScore.addActionListener(newHighScoreListener()); mGame.add(mitHighScore); mGame.addSeparator(); JMenuItemmitExit=newJMenuItem("退出"); //注册监听器,监听“退出”菜单项 mitExit.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente){ saveDataToFile(); System.exit(0); } }); mGame.add(mitExit); JMenumHelp=newJMenu("帮助"); mHelp.add(newJMenuItem("http://www.baidu.com/")); menuBar.add(mHelp); contentPane=newJPanel(); contentPane.setBorder(newEmptyBorder(10,10,10,10)); contentPane.setLayout(newBorderLayout(0,0)); setContentPane(contentPane); pnlCount=newJPanel(); contentPane.add(pnlCount,BorderLayout.NORTH); JLabellblTime=newJLabel("时间"); pnlCount.add(lblTime); txtTime=newJTextField("0",3); txtTime.setEditable(false); pnlCount.add(txtTime); JLabellblCount=newJLabel("计数"); pnlCount.add(lblCount); txtCount=newJTextField("0/"+minecount,5); txtCount.setEditable(false); pnlCount.add(txtCount); pnlMine=newJPanel(); contentPane.add(pnlMine,BorderLayout.CENTER); gamemode=mode; setMineArea(row,column,minecount); setMinePosition(); setMineButton(); UIManager.put("Label.font",FontC); } //从文件中读取数据 publicstaticint[]getDataFromFile(){ try{ FileInputStreamfis=newFileInputStream(file); byte[]buffer=newbyte[256]; intsize=fis.read(buffer); fis.close(); String[]s=newString(buffer,0,size).split(""); HighScore_Elementary=Integer.parseInt(s[0]); HighScore_Intermediate=Integer.parseInt(s[1]); HighScore_Advanced=Integer.parseInt(s[2]); int[]i={Integer.parseInt(s[3]),Integer.parseInt(s[4]), Integer.parseInt(s[5]),Integer.parseInt(s[6])}; returni; }catch(Exceptione){ HighScore_Elementary=Integer.MAX_VALUE; HighScore_Intermediate=Integer.MAX_VALUE; HighScore_Advanced=Integer.MAX_VALUE; returnElementary; } } //保存数据到文件 publicvoidsaveDataToFile(){ try{ Strings=HighScore_Elementary+""+HighScore_Intermediate+"" +HighScore_Advanced+""+row+""+column+"" +minecount+""+gamemode; FileOutputStreamfos=newFileOutputStream(file); fos.write(s.getBytes()); fos.close(); }catch(IOExceptione){ e.printStackTrace(); } } //初始化面板的各项参数 privatevoidsetMineArea(introw,intcolumn,intminecount){ setSize(30*column+20,30*row+120); //设置窗口在屏幕的正中心 setLocationRelativeTo(null); this.row=row; this.column=column; this.minecount=minecount; area=row*column; flagcount=0; enablecount=area; minearea=newboolean[row][column]; flagmark=newboolean[row][column]; isGameOver=false; isFirstClick=true; } //随机生成地雷的位置 privatevoidsetMinePosition(){ //生成minecount个不重复的随机数 int[]pos=newint[area]; for(inti=0;i24||c>30){ JOptionPane.showMessageDialog(null,"限制行数:1~24\n限制列数:1~30", "警告",JOptionPane.WARNING_MESSAGE); return; } if(count<=0||count>r*c){ JOptionPane.showMessageDialog(null,"限制雷数:1~"+(r*c), "警告",JOptionPane.WARNING_MESSAGE); return; } newMenuFunction().ResetSize(r,c,count); gamemode=Custom_Mode; setVisible(false); }catch(NumberFormatExceptionex){ JOptionPane.showMessageDialog(null,"无效输入!", "错误",JOptionPane.ERROR_MESSAGE); } } }); pnlButton.add(btnOK); } publicvoidactionPerformed(ActionEvente){ setVisible(true); } } //“高分榜”菜单项监听类 privateclassHighScoreListenerimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ JOptionPane.showMessageDialog(null,"初级:"+HighScore_Elementary +"秒\n中级:"+HighScore_Intermediate +"秒\n高级:"+HighScore_Advanced+"秒", "高分榜",JOptionPane.INFORMATION_MESSAGE); } } //菜单栏事件监听函数类 privateclassMenuFunction{ //“重新开始本局”函数 publicvoidRestart(){ flagcount=0; enablecount=area; if(!isFirstClick){ timeThread.interrupt(); isFirstClick=true; txtTime.setText("0"); } txtCount.setText("0/"+minecount); for(inti=0;i|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。