用java實現(xiàn)掃雷游戲
用java做出簡單一個掃雷游戲,供大家參考,具體內(nèi)容如下
1.創(chuàng)造窗口
//創(chuàng)建掃雷窗口界面 ? ?? ?public Saolei() { ?? ??? ? ?? ??? ??? ?frame.setSize(600,700); ?? ??? ??? ?frame.setResizable(false);//設(shè)置窗口尺寸不能變化 ?? ??? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?? ??? ??? ?frame.setLayout(new BorderLayout());//分塊 ?? ??? ??? ?setHeader();//設(shè)置界面初始化 ?? ??? ??? ?addlei(); ? //添加雷 ?? ??? ??? ?setButtons();//添加按鈕 ?? ??? ??? ?timer.start(); ? //啟動時鐘; ?? ??? ??? ?frame.setVisible(true); ?? ??? ?}
2.定義數(shù)據(jù)結(jié)構(gòu)以及初始化
//數(shù)據(jù)結(jié)構(gòu)start ?? ?int ROW = 20; ?? ?int COL = 20; ?? ?int [][] data = new int[ROW][COL]; ?? ?JButton[][] btns = new JButton[ROW][COL]; ?? ?int LEICOUNT = 30; ? //雷個數(shù) ?? ?int LEICODE = -1; ?? ?int unopened = ROW*COL; ?? ?int opened = 0; ?? ?int timeSecond =0; ?? ?//三個 jlabel 狀態(tài)欄 已開未開,用時 ?? ?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));//創(chuàng)建方格 ?? ? ? ?? ? ?for(int i=0;i<ROW;i++) { ?? ??? ? ?for(int j=0;j<COL;j++) { ?? ??? ??? ? ?JButton btn = new JButton(); ?? ??? ??? ? ?btn.setOpaque(true); ?? ??? ??? ? ?btn.setBackground(new Color(200,183,113)); ?//設(shè)置掃雷背景顏色 ?? ??? ??? ? ?btn.addActionListener(this); ?//Btn添加按鈕監(jiān)聽事件 ?? ??? ??? ? ?btn.setMargin(new Insets(0,0,0,0)); ?//button數(shù)字顯示不出來,加上該語句即可顯示 ?? ??? ??? ? ?con.add(btn); ?? ??? ??? ? ?btns[i][j] = btn; ?? ??? ? ?} ?? ? ?} ?? ? ?frame.add(con,BorderLayout.CENTER);//中間位置 ?? ? ? ?? ?}
4.埋雷
private void addlei() { ?? ??? ?Random rand = new Random(); ?? ??? ?for(int i=0;i<LEICOUNT;) { ?? ??? ??? ?int r = rand.nextInt(ROW); ?? ??? ??? ?int c= rand.nextInt(COL); ?? ??? ??? ?if(data[r][c]!=LEICODE) { ?? ??? ??? ??? ?data[r][c] = LEICODE; ?? ??? ??? ??? ?i++; //?? ??? ??? ??? ?System.out.println(r+" ?"+c+" "+data[r][c]); ?? ??? ??? ?} ?? ??? ?}
5.計算周圍雷的數(shù)量
//計算周邊雷的數(shù)量 ?? ??? ? ?for(int i=0;i<ROW;i++) { ?? ??? ??? ? ?for(int j=0;j<COL;j++) { ?? ??? ??? ??? ? ?if(data[i][j]!=LEICODE) { ?? ??? ??? ??? ??? ? ? int ?c=0; ?? ??? ??? ??? ??? ? ? if(i>0&&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設(shè)置
//設(shè)置開頭顯示 ?? ?private void setHeader() { ?? ??? ?//設(shè)置畫布 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); ? ?//設(shè)置不透明,背景色, ?? ??? ?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<ROW;i++) { ?? ??? ??? ??? ?for(int j=0;j<COL;j++) { ?? ??? ??? ??? ??? ?if(btns[i][j].isEnabled()) { ?? ??? ??? ??? ??? ??? ?count++; ?? ??? ??? ??? ??? ??? ? ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?unopened = count; ?? ??? ??? ?opened = ?ROW*COL-count; ?? ??? ??? ? ?? ??? ??? ?label1.setText("待開:"+ unopened); ?? ??? ??? ?label2.setText("已開:"+ opened); ?? ??? ??? ?if(count==LEICOUNT) { ?? ??? ??? ??? ?timer.stop();? ?? ??? ??? ??? ?bannerBtn.setText("游戲勝利?。?!"); ?? ??? ??? ??? ?for(int i=0;i<ROW;i++) { ?? ??? ??? ??? ??? ?for(int j=0;j<COL;j++) { ?? ??? ??? ??? ??? ??? ?if(btns[i][j].isEnabled()) { ?? ??? ??? ??? ??? ??? ??? ? ?btns[i][j].setBackground(new Color(100,183,0)); ? ? ? ? ? ? ? ? ? ? ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?bannerBtn.setText("Banner:restart!"); ?? ??? ??? ??? ?JOptionPane.showMessageDialog(frame, "恭喜你!游戲勝利啦!\n 可以點擊上面的Banner重新開始!","游戲結(jié)束!",JOptionPane.PLAIN_MESSAGE); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ? ?? ?} ?? ?//踩雷成功,游戲結(jié)束! ?? ?private void lose() { ?? ??? ?timer.stop(); ?? ??? ? bannerBtn.setText("很遺憾,踩雷成功,游戲結(jié)束?。?!"); ?? ??? ?for(int i=0;i<ROW;i++) { ?? ??? ??? ?for(int j=0;j<COL;j++) { ?? ??? ??? ??? ?if(btns[i][j].isEnabled()) { ?? ??? ??? ??? ??? ?JButton btn = btns[i][j]; ?? ??? ??? ??? ??? ?if(data[i][j]==LEICODE) { //?? ??? ??? ??? ??? ?btns[i][j].setEnabled(false);?? ?btns[i][j].setIcon(bombIcon); btns[i][j].setDisabledIcon(bombIcon); ?? ??? ??? ??? ? ?? ?? ??? ??? ??? ??? ??? ?btn.setBackground(Color.red);?? ? //置為紅色 ?? ??? ??? ??? ??? ??? ?btn.setText(data[i][j]+""); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?else { ?? ??? ??? ??? ??? ??? ?btn.setIcon(null); ?? ??? ??? ??? ??? ??? ?btn.setEnabled(false); ? //btn已經(jīng)打開不可點擊 ?? ??? ??? ??? ??? ??? ?btn.setOpaque(true); ?? ??? ? ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?bannerBtn.setText("Banner:restart!"); ?? ??? ?JOptionPane.showMessageDialog(frame, "很遺憾,暴雷成功,游戲結(jié)束!??!\n 可以點擊上面的Banner重新開始!","游戲結(jié)束!",JOptionPane.PLAIN_MESSAGE); ?? ??? ? ?? ?}
8.空白級聯(lián)打開(實現(xiàn)點擊一個即可打開多個)
private void openCell(int i,int j,int Blankcount ){ ?? ??? ?JButton btn=btns[i][j]; ?? ??? ?if(!btn.isEnabled()) return ; ?? ??? ?if(Blankcount==10) ? //部分打開,設(shè)置數(shù)字限制 ?? ??? ??? ?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.最終游戲界面
總結(jié)
游戲界面顏色設(shè)置的有點丑,代碼是跟著某站上老師做的,感謝老師!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中使用spring-retry 解決失敗重試調(diào)用
本文主要介紹了SpringBoot中使用spring-retry 解決失敗重試調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java8 Stream對兩個 List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作
這篇文章主要介紹了Java8 Stream對兩個 List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08使用log4j2打印mybatis的sql執(zhí)行日志方式
這篇文章主要介紹了使用log4j2打印mybatis的sql執(zhí)行日志方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09基于SpringBoot和Vue3的博客平臺文章詳情與評論功能實現(xiàn)
在前面的教程中,我們已經(jīng)實現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能以及文章列表與分頁功能。本教程將引導(dǎo)您實現(xiàn)博客平臺的文章詳情與評論功能,需要的朋友可以參考一下2023-04-04