您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java怎么實(shí)現(xiàn)對(duì)對(duì)碰小游戲”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“java怎么實(shí)現(xiàn)對(duì)對(duì)碰小游戲”吧!
- 游戲?qū)崿F(xiàn)功能:分別點(diǎn)擊兩張相鄰的圖像按鈕進(jìn)行交換(重點(diǎn)相鄰),交換后的兩個(gè)圖像按鈕的相鄰水平或者垂直方向上,與之相同的圖像超過規(guī)定個(gè)數(shù)后(這里規(guī)定為3個(gè))就將其全部消除(置為空白按鈕),上面的圖像按鈕也分別隨之向下移動(dòng),將空白補(bǔ)齊(這里我們可以理解為圖像按鈕和空白按鈕進(jìn)行交換)。
- 游戲設(shè)計(jì)思路:
1.創(chuàng)建圖像按鈕數(shù)組,設(shè)置其基本屬性; 2.給每個(gè)圖像按鈕配置相應(yīng)的ID,用來標(biāo)記圖像信息,對(duì)于ID相同的按鈕,圖像也相同; 3.設(shè)置遍歷全局判斷是否可以建立相連按鈕的函數(shù); 4.設(shè)置遍歷全局將可以建立相連的按鈕圖像ID置為EMPTY,將按鈕置為空白按鈕; 5.設(shè)置移動(dòng)函數(shù),將空白按鈕與上層非空白按鈕相互交換的函數(shù),將空白按鈕移動(dòng)到上層; 6.設(shè)置更新函數(shù),將移動(dòng)到上層的空白按鈕再隨機(jī)匹配圖像,繼續(xù)使用; 7.設(shè)置交換按鈕函數(shù); 8.記錄游戲得分,給一個(gè)確定的目標(biāo)成績,達(dá)到即可贏得游戲; 9.設(shè)置一個(gè)進(jìn)度條,記錄游戲進(jìn)行的時(shí)間; 10.設(shè)置一個(gè)時(shí)間記錄器(定時(shí)器); 11.設(shè)計(jì)游戲界面基本信息(根據(jù)個(gè)人愛好設(shè)計(jì)即可);
- 游戲代碼
---mybutton類,設(shè)置了每個(gè)按鈕對(duì)象的基本信息package supperzzle;import javax.swing.Icon;import javax.swing.JButton;public class MyButton extends JButton{ private final int Width = 30;//設(shè)置按鈕的寬度 private final int Height = 30; private int ID;//設(shè)置按鈕的ID-----ID代表每一個(gè)按鈕里面存放的數(shù)據(jù) private int buttonPosX = 0; private int buttonPosY = 0; public MyButton(int id, Icon icon)//構(gòu)造函數(shù) { this.setIcon(icon); this.ID = id; this.setSize(Width, Height);//設(shè)置按鈕的邊框大小 this.setFocusable(true);//去掉按鈕的聚焦框 this.setBorderPainted(false);//去掉邊框 this.setContentAreaFilled(false);//不顯示外圍矩形邊框 } public int GetID() { return ID; } public void SetID(int id) { this.ID = id; }}
//-----這是游戲的重點(diǎn)了,基本游戲界面設(shè)計(jì)--GamePanel類---對(duì)游戲的界面進(jìn)行了基本的設(shè)置(寫的有點(diǎn)挫,,有什么好的建議請(qǐng)盡情提的不要拘謹(jǐn),哈哈)package supperzzle;import java.awt.GridLayout;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.EventListener;import java.util.Random;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.JPanel;public class GamePanel extends JPanel implements ActionListener{ private final int row = 10; private final int col = 10; private final int lineCount = 3;//設(shè)置幾連可碰消除 private int grade = 0;//記錄得分 private final int score = 10;//設(shè)置每次消去一個(gè)方塊獲得的分?jǐn)?shù) public MyButton mybutton[] = new MyButton[row*col];//這里只是開辟了相應(yīng)的空間 private final int countImage = 7; private ImageIcon imageIcon[] = new ImageIcon[countImage];//設(shè)置圖標(biāo)數(shù)組 private final int EMPTY = -1; private Random random = new Random(); private int posx = 0; private int posy = 0;//保存第一次按鈕按下去的坐標(biāo) private boolean IsSecond = false; public GamePanel()//游戲面板的構(gòu)造函數(shù)----實(shí)現(xiàn)圖片加載,數(shù)組圖標(biāo)的加載,以及按鈕的基本設(shè)置 { this.setLayout(new GridLayout(row,col,0,0));//創(chuàng)建一個(gè)網(wǎng)絡(luò)布局管理格式---row行col列 for(int i = 0; i < countImage; i++)//圖標(biāo)數(shù)組初始化 {// Image image = Toolkit.getDefaultToolkit().getImage("F:/Image/supperzzle/angrybird"+i+".png"); Image image = Toolkit.getDefaultToolkit().getImage("F:/Image/LinkGame/pic"+i+".png"); imageIcon[i] = new ImageIcon(image);//每一個(gè)數(shù)組元素都得到了相應(yīng)的圖標(biāo) } for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { int index = random.nextInt(countImage);//隨機(jī)生成一個(gè)數(shù)作為圖標(biāo)數(shù)組的下標(biāo) mybutton[i*col+j] = new MyButton(index,imageIcon[index]);//給每個(gè)元素都進(jìn)行了初始化 mybutton[i*col+j].addActionListener(this);//按鈕添加監(jiān)聽機(jī)制 mybutton[i*col+j].setEnabled(false);//設(shè)置按鈕為無效 this.add(mybutton[i*col+j]);//將按鈕加載在該面板中 } } } public boolean YesOrNoThreeLink(int x, int y)//判斷該坐標(biāo)同一個(gè)方向上是否可以建立連續(xù)相同id的方塊按鈕 { int linked = 1; //判斷垂直方向上面是否有連續(xù)相同的 for(int i = x-1; i >= 0; i--) { if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID()) { linked++; } else { break; } } for(int i = x+1; i < row; i++)//判斷該坐標(biāo)的下面 { if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID()) { linked++; } else { break; } } if(linked >= lineCount) { return true; } //判斷水平方向上面是否有里連續(xù)相同的方塊 linked = 1; for(int i = y-1; i >= 0; i--)//判斷水平向左 { if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID()) { linked++; } else break; } for(int i = y+1; i < col; i++)//判斷水平向右 { if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID()) { linked++; } else break; } if(linked >= lineCount)//說明滿足條件建立了連線 { return true; } return false; } public void RemoveLink(int x, int y)//移除相同ID的方塊設(shè)置相同的方塊ID為EMPTY,并計(jì)算得分 { int linked1 = 0; int linked2 = 0; int tempxStart = x; int tempxEnd = x;//分別保存第一個(gè)和最后一個(gè)與該點(diǎn)ID相同的x坐標(biāo) int tempyStart = y; int tempyEnd = y;//分別保存第一個(gè)和最后一個(gè)與該點(diǎn)ID相同的y坐標(biāo) //先判斷垂直方向上面的---上下行 for(int i = x-1; i >= 0; i--)//判斷該坐標(biāo)的上面 { if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID()) { linked1++; tempxStart = i; } else { break; } } for(int i = x+1; i < row; i++)//判斷該坐標(biāo)的下面 { if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID()) { linked1++; tempxEnd = i; } else { break; } } if(linked1 + 1 >= lineCount) { for(int i = tempxStart; i <= tempxEnd; i++) { mybutton[i*col+y].SetID(EMPTY); }// grade += linked*score; grade += linked1*score; } //判斷水平方向上面的---左右列 for(int i = y-1; i >= 0; i--)//判斷水平向左 { if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID()) { linked2++; tempyStart = i; } else break; } for(int i = y+1; i < col; i++)//判斷水平向右 { if(mybutton[x*col+i].GetID() == mybutton[x*col+y].GetID()) { linked2++; tempyEnd = i; } else break; } if(linked2+1 >= lineCount)//說明滿足條件建立了連線 { for(int i = tempyStart; i <= tempyEnd; i++) { mybutton[x*col+i].SetID(EMPTY); }// grade += score*linked; grade += score*linked2; } grade += score; } public int GetGrade()//獲取得分 { return grade; } public void SetGrade(int n) { this.grade = n; } public void SwapElement(int x, int y)//交換元素 { if(x >= 0 && x < row && y >= 0 && y < col) { if(!IsSecond)//第一次點(diǎn)擊按鈕 { posx = x; posy = y; IsSecond = true; System.out.println("第一次點(diǎn)擊:posx->"+posx+" posy->"+posy); } else//第二次點(diǎn)擊按鈕 { //判斷是否是相鄰的兩個(gè)方塊 System.out.println("第2次點(diǎn)擊:x->"+x+" y->"+y); if(1 == Math.abs(posx - x) && posy == y || 1 == Math.abs(posy - y) && posx == x)//判斷是否是相鄰的坐標(biāo) { //先交換 System.out.println("交換"); int temp = mybutton[posx*col+posy].GetID(); mybutton[posx*col+posy].SetID(mybutton[x*col+y].GetID()); mybutton[x*col+y].SetID(temp);// showImageButton(); mybutton[x*col+y].setIcon(imageIcon[temp]); mybutton[posx*col+posy].setIcon(imageIcon[mybutton[posx*col+posy].GetID()]); //再判斷 if(YesOrNoThreeLink(x,y) || YesOrNoThreeLink(posx,posy)) { if(YesOrNoThreeLink(x,y)) { RemoveLink(x,y); GameFrame.texteara.setText(Integer.toString(GetGrade())); } if(YesOrNoThreeLink(posx,posy)) { RemoveLink(posx,posy); GameFrame.texteara.setText(Integer.toString(GetGrade())); } //開始掉方塊,全盤處理所有下面的空白方塊 DownButton(); //更新 UpDateButton(); showImageButton(); GameFrame.texteara.setText(Integer.toString(GetGrade())); while(GlobalSearch(1))//掃描全盤 { GlobalSearch(0);//消除 DownButton(); UpDateButton(); showImageButton(); GameFrame.texteara.setText(Integer.toString(GetGrade())); } } else { //沒有相同的方塊就再換回來 temp = mybutton[posx*col+posy].GetID(); mybutton[posx*col+posy].SetID(mybutton[x*col+y].GetID()); mybutton[x*col+y].SetID(temp);// showImageButton(); mybutton[x*col+y].setIcon(imageIcon[temp]); mybutton[posx*col+posy].setIcon(imageIcon[mybutton[posx*col+posy].GetID()]); } } IsSecond = false; } } } public void DownButton()//將底層的空白的方塊全部上移,將上面的非空白方塊下移補(bǔ)齊 { for(int i = row-1; i > 0; i--)//行--從最后一行開始 { for(int j = 0; j < col; j++)//列---從第一列開始 { if(mybutton[i*col+j].GetID() == EMPTY)//交換每個(gè)按鈕決定圖像的ID號(hào)即可 { //交換同列上面非EMPTY的結(jié)點(diǎn) for(int k = i-1; k >= 0; k--) { if(mybutton[k*col+j].GetID() != EMPTY) { int id = mybutton[i*col+j].GetID(); mybutton[i*col+j].SetID(mybutton[k*col+j].GetID()); mybutton[k*col+j].SetID(id); break; } } } } } } public boolean GlobalSearch(int flag)//全盤掃描 { if(flag == 1)//----------只是掃描所有的元素是否存在相鄰的連續(xù)方塊 { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(YesOrNoThreeLink(i,j)) { return true; } } } } else if(flag == 0)//-------掃描加消除該節(jié)點(diǎn)置為EMPTY { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { RemoveLink(i,j); } } return true; } return false; } public void showImageButton()//將所有的按鈕的圖標(biāo)重新繪制一次 { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { mybutton[i*col+j].setIcon(imageIcon[mybutton[i*col+j].GetID()]); } } } public void UpDateButton()//更新按鈕里面的坐標(biāo)id-----讓EMPTY的按鈕重新獲得隨機(jī)ID { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(mybutton[i*col+j].GetID() == EMPTY) { int index = random.nextInt(countImage); mybutton[i*col+j].SetID(index); } } } } public int GetRow() { return row; } public int GetCol() { return col; } public void actionPerformed(ActionEvent e)//監(jiān)聽機(jī)制 { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(e.getSource() == mybutton[i*col+j])//交換元素對(duì)象 { SwapElement(i,j); GameFrame.texteara.setText(Integer.toString(GetGrade())); break; } } } if(grade > 8000 && GameFrame.timer.isRunning()) { JOptionPane.showConfirmDialog(null, "恭喜您過關(guān)", "Win", JOptionPane.CLOSED_OPTION); for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { mybutton[i*col+j].setEnabled(false); GameFrame.buttonstart.setEnabled(true); GameFrame.timer.stop(); } } } }}
//游戲走到這里就是真正的游戲接口了,游戲開始的入口--GameFrame類,設(shè)計(jì)游戲窗口和創(chuàng)建游戲package supperzzle;import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax.swing.JTextField;import javax.swing.Timer;public class GameFrame extends JFrame implements ActionListener{ private JPanel paneone = new JPanel(); public static JButton buttonstart = new JButton("游戲開始"); private JButton buttonend = new JButton("游戲結(jié)束"); private JLabel labelGrade = new JLabel("游戲得分"); private JLabel labelTime = new JLabel("游戲時(shí)間"); public static JTextField texteara = new JTextField(10);//設(shè)置文本編輯域 GamePanel gamepanel = new GamePanel(); private int gamerow = gamepanel.GetRow(); private int gamecol = gamepanel.GetCol(); private JProgressBar progressbar = new JProgressBar();//創(chuàng)建一個(gè)進(jìn)度條 public static Timer timer;//創(chuàng)建一個(gè)時(shí)間計(jì)時(shí)器 public GameFrame() { Container con = this.getContentPane(); con.setLayout(new BorderLayout()); paneone.setLayout(new FlowLayout()); paneone.add(buttonstart);//添加開始按鈕 paneone.add(labelGrade);//添加得分編輯框 paneone.add(texteara);//添加文本域 paneone.add(labelTime);//添加時(shí)間編輯框 paneone.add(progressbar);//添加一個(gè)進(jìn)度條 paneone.add(buttonend);//添加結(jié)束按鈕 con.add(paneone,BorderLayout.NORTH); con.add(gamepanel,BorderLayout.CENTER); this.setBounds(300, 0, 600, 700); this.setTitle("對(duì)對(duì)碰游戲"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗口關(guān)閉 buttonstart.addActionListener(this); buttonend.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == buttonstart)//點(diǎn)擊開始按鈕 { gamepanel.SetGrade(0);//重新設(shè)置得分 buttonstart.setEnabled(false); progressbar.setMaximum(100);//設(shè)置進(jìn)度條的最大最小范圍 progressbar.setMinimum(0); progressbar.setStringPainted(true); timer = new Timer(800,new TimeListener()); timer.start(); for(int i = 0; i < gamerow; i++) { for(int j = 0; j < gamecol; j++) { gamepanel.mybutton[i*gamecol+j].setEnabled(true); } } initGame(); texteara.setText(Integer.toString(gamepanel.GetGrade())); } if(e.getSource() == buttonend) { System.exit(1); } } public void initGame() { while(gamepanel.GlobalSearch(1)) { gamepanel.GlobalSearch(0); gamepanel.DownButton(); gamepanel.UpDateButton(); gamepanel.showImageButton(); } } class TimeListener implements ActionListener//創(chuàng)建了一個(gè)自定義的時(shí)間監(jiān)聽機(jī)制 { int times = 0; public void actionPerformed(ActionEvent e) { progressbar.setValue(times++); if(times > 100) { timer.stop(); for(int i = 0; i < gamerow; i++) { for(int j = 0; j < gamecol; j++) { gamepanel.mybutton[i*gamecol+j].setEnabled(false); } } buttonstart.setEnabled(true); } } } public static void main(String[] args) { GameFrame gameframe = new GameFrame(); }}
到此,相信大家對(duì)“java怎么實(shí)現(xiàn)對(duì)對(duì)碰小游戲”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。