用java做出简单一个扫雷游戏,供大家参考,具体内容如下
1.创造窗口
//创建扫雷窗口界面 public Saolei() { frame.setSize(600,700); frame.setResizable(false);//设置窗口尺寸不能变化 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout());//分块 setHeader();//设置界面初始化 addlei(); //添加雷 setButtons();//添加按钮 timer.start(); //启动时钟; frame.setVisible(true); }
2.定义数据结构以及初始化
//数据结构start int ROW = 20; int COL = 20; int [][] data = new int[ROW][COL]; JButton[][] btns = new JButton[ROW][COL]; int LEICOUNT = 30; //雷个数 int LEICODE = -1; int unopened = ROW*COL; int opened = 0; int timeSecond =0; //三个 jlabel 状态栏 已开未开,用时 JLabel label1= new JLabel("待开:"+unopened); JLabel label2= new JLabel("已开:"+opened); JLabel label3= new JLabel("用时:"+timeSecond+"s"); Timer timer = new Timer(1000, this); //定义时间为一秒
3.窗体按钮
private void setButtons() { Container con = new Container();//container容器 con.setLayout(new GridLayout(ROW,COL));//创建方格 for(int i=0;i|
4.埋雷
private void addlei() { Random rand = new Random(); for(int i=0;i
5.计算周围雷的数量
//计算周边雷的数量 for(int i=0;i0&&j>0&&data[i-1][j-1]==LEICODE) c++; if(i>0&&data[i-1][j]==LEICODE) c++; if(i>0&&j<19&&data[i-1][j+1]==LEICODE) c++; if(j>0&&data[i][j-1]==LEICODE) c++; if(j<19&&data[i][j+1]==LEICODE) c++; if(i<19&&j>0&&data[i+1][j-1]==LEICODE) c++; if(i<19&&data[i+1][j]==LEICODE) c++; if(i<19&&j<19&&data[i+1][j+1]==LEICODE) c++; data[i][j]=c; } } }
6.Banner设置
//设置开头显示 private void setHeader() { //设置画布 Jpanel JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c1 = new GridBagConstraints(0,0,3,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0); panel.add(bannerBtn,c1); bannerBtn.addActionListener(this); label1.setOpaque(true); //设置不透明,背景色, label1.setBackground(Color.white); label1.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); label2.setOpaque(true); label2.setBackground(Color.white); label2.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); label3.setOpaque(true); label3.setBackground(Color.white); label3.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); bannerBtn.setOpaque(true); bannerBtn.setBackground(Color.white); bannerBtn.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); GridBagConstraints c2 = new GridBagConstraints(0,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0); panel.add(label1,c2); GridBagConstraints c3 = new GridBagConstraints(1,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0); panel.add(label2,c3); GridBagConstraints c4 = new GridBagConstraints(2,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0); panel.add(label3,c4); frame.add(panel,BorderLayout.NORTH); }
7.游戏胜利还是失败
//判断胜利!!! private void checkWin() { int count=0; for(int i=0;i|
8.空白级联打开(实现点击一个即可打开多个)
private void openCell(int i,int j,int Blankcount ){ JButton btn=btns[i][j]; if(!btn.isEnabled()) return ; if(Blankcount==10) //部分打开,设置数字限制 return; btn.setIcon(null); btn.setEnabled(false); btn.setOpaque(true); Blankcount++; btn.setBackground(Color.GREEN); btn.setText(data[i][j]+""); if(data[i][j]==0) { if(i>0&&j>0&&data[i-1][j-1]==0) openCell(i-1,j-1,Blankcount); if(i>0&&data[i-1][j]==0) openCell(i-1,j,Blankcount); if(i>0&&j<19&&data[i-1][j+1]==0) openCell(i-1,j+1,Blankcount); if(j>0&&data[i][j-1]==0) openCell(i,j-1,Blankcount); if(j<19&&data[i][j+1]==0) openCell(i,j+1,Blankcount); if(i<19&&j>0&&data[i+1][j-1]==0) openCell(i+1,j-1,Blankcount); if(i<19&&data[i+1][j]==0) openCell(i+1,j,Blankcount); if(i<19&&j<19&&data[i+1][j+1]==0) openCell(i+1,j+1,Blankcount); } }
9.最终游戏界面
总结
游戏界面颜色设置的有点丑,代码是跟着某站上老师做的,感谢老师!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。