本文实例为大家分享了java实现扫雷游戏的具体代码,供大家参考,具体内容如下
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SaoLei implements MouseListener,ActionListener{ JPanel p=new JPanel(); JFrame frame = new JFrame("扫雷"); @SuppressWarnings("rawtypes") JComboBox combobox = new JComboBox(); JButton reset = new JButton("重新开始"); Container container = new Container(); //游戏数据结构 SaoLeiConstant constant = new SaoLeiConstant(); JButton[][] buttons = new JButton[constant.row][constant.col];//定义按钮 int[][] counts = new int [constant.row][constant.col];//定义整型数组保存按钮下方雷数 //创建构造方法 public SaoLei() { //显示窗口 frame.setSize(600,700);//600*700 frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); //添加重来、选择难度按钮 addtopButton(); //添加雷区按钮 addButtons(); //埋雷 addLei(); //添加雷的计数 calcNeiboLei(); frame.setVisible(true); } void addtopButton() { p.removeAll(); p.add(reset); reset.setBackground(Color.green); reset.setOpaque(true); reset.addActionListener(this); //combobox.addItem("选择难度"); combobox.addItem("新手难度"); combobox.addItem("初级难度"); combobox.addItem("中级难度"); combobox.addItem("高级难度"); combobox.addItem("大师难度"); combobox.setBackground(Color.GREEN); combobox.setOpaque(true); combobox.addItemListener(new ItemListener(){ @Override public void itemStateChanged(ItemEvent e) { String item = e.getItem().toString(); if(item == "新手难度") { constant.leiCount = 20; ResetGame(); } else if(item == "初级难度") { constant.leiCount = 43; ResetGame(); } else if(item == "中级难度"){ constant.leiCount = 63; ResetGame(); } else if(item == "高级难度"){ constant.leiCount = 99; ResetGame(); } else if(item == "大师难度") { constant.leiCount = 119; ResetGame(); } } }); p.add(combobox); frame.add(p,BorderLayout.NORTH); //p.add(new Label("总雷数:"+constant.leiCount,Label.CENTER)); //p.add(new Label("总雷数:"+constant.leiCount,Label.RIGHT)); } /* void addnanduButton() { nandu.setBackground(Color.green); nandu.setOpaque(true); nandu.addActionListener(this); frame.add(nandu,BorderLayout.WEST); } void addResetButton() { reset.setBackground(Color.green); reset.setOpaque(true); reset.addActionListener(this); //reset.addMouseListener(this); frame.add(reset,BorderLayout.NORTH); } */ void addLei() { Random rand = new Random(); int randRow,randCol; for(int i=0; i0 && j>0 && counts[i-1][j-1] == constant.LEICODE) count++; if(i>0 && counts[i-1][j] == constant.LEICODE) count++; if(i>0 && j<19 &&counts[i-1][j+1] == constant.LEICODE) count++; if(j>0 && counts[i][j-1] == constant.LEICODE) count++; if(j<19 && counts[i][j+1] == constant.LEICODE) count++; if(i<19 && j>0 && counts[i+1][j-1] == constant.LEICODE) count++; if(i<19 && counts[i+1][j] == constant.LEICODE) count++; if(i<19 && j<19 && counts[i+1][j+1] == constant.LEICODE) count++; counts[i][j] = count; buttons[i][j].setMargin(new Insets(0,0,0,0));//让按钮随按钮上的图案变化 //buttons[i][j].setText(counts[i][j] + ""); } } } @Override public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); if(button.equals(reset)) { ResetGame();//重新开始游戏 } else { int count = 0; for(int i=0;i 0 && j>0 && counts[i-1][j-1] != constant.LEICODE) openCell(i-1,j-1); if(i>0 && j<19 && counts[i-1][j] != constant.LEICODE) openCell(i-1,j); if(i>0 && j<19 &&counts[i-1][j+1] != constant.LEICODE) openCell(i-1,j+1); if(j>0 && counts[i][j-1] != constant.LEICODE) openCell(i,j-1); if(j<19 && counts[i][j+1] != constant.LEICODE) openCell(i,j+1); if(i<19 && j>0 && counts[i+1][j-1] != constant.LEICODE) openCell(i+1,j-1); if(i<19 && counts[i+1][j] != constant.LEICODE) openCell(i+1,j); if(i<19 && j<19 && counts[i+1][j+1] != constant.LEICODE) openCell(i+1,j+1); } else { buttons[i][j].setMargin(new Insets(0,0,0,0)); buttons[i][j].setText(counts[i][j] + ""); } } void loseGame() { for(int i=0;i
常量类
public class SaoLeiConstant { final int row = 20;//行数30 final int col = 20;//列数30 final int LEICODE = 10;//定义雷下方的数字 protected int temp = 20; protected int leiCount = temp;//雷数30 }
效果图
更多精彩游戏,请参考专题《java经典小游戏》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。