用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)文章
java Collection 之List學(xué)習(xí)介紹
本篇文章小編為大家介紹,java Collection 之List學(xué)習(xí)介紹。需要的朋友參考下2013-04-04
基于Springboot實現(xiàn)JWT認證的示例代碼
本文主要介紹了基于Springboot實現(xiàn)JWT認證,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Java中使用Thread類和Runnable接口實現(xiàn)多線程的區(qū)別
這篇文章主要介紹了使用Thread類和Runnable接口實現(xiàn)多線程的區(qū)別,本文給大家介紹了兩種實現(xiàn)方式的步驟,除了以上兩種多線程實現(xiàn)方式,還可以使用 Callable 接口實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07
java 中設(shè)計模式(裝飾設(shè)計模式)的實例詳解
這篇文章主要介紹了java 中設(shè)計模式(裝飾設(shè)計模式)的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09

