现在的位置: 首页 > 编程语言 > 正文

java实现对对碰小游戏

2020年02月13日 编程语言 ⁄ 共 10158字 ⁄ 字号 评论关闭

本文实例为大家分享了java实现对对碰的具体代码,供大家参考,具体内容如下

- 游戏实现功能:分别点击两张相邻的图像按钮进行交换(重点相邻),交换后的两个图像按钮的相邻水平或者垂直方向上,与之相同的图像超过规定个数后(这里规定为3个)就将其全部消除(置为空白按钮),上面的图像按钮也分别随之向下移动,将空白补齐(这里我们可以理解为图像按钮和空白按钮进行交换)。

- 游戏设计思路:

1.创建图像按钮数组,设置其基本属性; 2.给每个图像按钮配置相应的ID,用来标记图像信息,对于ID相同的按钮,图像也相同; 3.设置遍历全局判断是否可以建立相连按钮的函数; 4.设置遍历全局将可以建立相连的按钮图像ID置为EMPTY,将按钮置为空白按钮; 5.设置移动函数,将空白按钮与上层非空白按钮相互交换的函数,将空白按钮移动到上层; 6.设置更新函数,将移动到上层的空白按钮再随机匹配图像,继续使用; 7.设置交换按钮函数; 8.记录游戏得分,给一个确定的目标成绩,达到即可赢得游戏; 9.设置一个进度条,记录游戏进行的时间; 10.设置一个时间记录器(定时器); 11.设计游戏界面基本信息(根据个人爱好设计即可);

- 游戏代码

---mybutton类,设置了每个按钮对象的基本信息package supperzzle;import javax.swing.Icon;import javax.swing.JButton;public class MyButton extends JButton{ private final int Width = 30;//设置按钮的宽度 private final int Height = 30; private int ID;//设置按钮的ID-----ID代表每一个按钮里面存放的数据 private int buttonPosX = 0; private int buttonPosY = 0; public MyButton(int id, Icon icon)//构造函数 { this.setIcon(icon); this.ID = id; this.setSize(Width, Height);//设置按钮的边框大小 this.setFocusable(true);//去掉按钮的聚焦框 this.setBorderPainted(false);//去掉边框 this.setContentAreaFilled(false);//不显示外围矩形边框 } public int GetID() { return ID; } public void SetID(int id) { this.ID = id; }}

//-----这是游戏的重点了,基本游戏界面设计--GamePanel类---对游戏的界面进行了基本的设置(写的有点挫,,有什么好的建议请尽情提的不要拘谨,哈哈)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;//设置几连可碰消除 private int grade = 0;//记录得分 private final int score = 10;//设置每次消去一个方块获得的分数 public MyButton mybutton[] = new MyButton[row*col];//这里只是开辟了相应的空间 private final int countImage = 7; private ImageIcon imageIcon[] = new ImageIcon[countImage];//设置图标数组 private final int EMPTY = -1; private Random random = new Random(); private int posx = 0; private int posy = 0;//保存第一次按钮按下去的坐标 private boolean IsSecond = false; public GamePanel()//游戏面板的构造函数----实现图片加载,数组图标的加载,以及按钮的基本设置 { this.setLayout(new GridLayout(row,col,0,0));//创建一个网络布局管理格式---row行col列 for(int i = 0; i < countImage; i++)//图标数组初始化 {// 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);//每一个数组元素都得到了相应的图标 } for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { int index = random.nextInt(countImage);//随机生成一个数作为图标数组的下标 mybutton[i*col+j] = new MyButton(index,imageIcon[index]);//给每个元素都进行了初始化 mybutton[i*col+j].addActionListener(this);//按钮添加监听机制 mybutton[i*col+j].setEnabled(false);//设置按钮为无效 this.add(mybutton[i*col+j]);//将按钮加载在该面板中 } } } public boolean YesOrNoThreeLink(int x, int y)//判断该坐标同一个方向上是否可以建立连续相同id的方块按钮 { int linked = 1; //判断垂直方向上面是否有连续相同的 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++)//判断该坐标的下面 { if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID()) { linked++; } else { break; } } if(linked >= lineCount) { return true; } //判断水平方向上面是否有里连续相同的方块 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的方块设置相同的方块ID为EMPTY,并计算得分 { int linked1 = 0; int linked2 = 0; int tempxStart = x; int tempxEnd = x;//分别保存第一个和最后一个与该点ID相同的x坐标 int tempyStart = y; int tempyEnd = y;//分别保存第一个和最后一个与该点ID相同的y坐标 //先判断垂直方向上面的---上下行 for(int i = x-1; i >= 0; i--)//判断该坐标的上面 { if(mybutton[i*col+y].GetID() == mybutton[x*col+y].GetID()) { linked1++; tempxStart = i; } else { break; } } for(int i = x+1; i < row; i++)//判断该坐标的下面 { 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)//第一次点击按钮 { posx = x; posy = y; IsSecond = true; System.out.println("第一次点击:posx->"+posx+" posy->"+posy); } else//第二次点击按钮 { //判断是否是相邻的两个方块 System.out.println("第2次点击:x->"+x+" y->"+y); if(1 == Math.abs(posx - x) && posy == y || 1 == Math.abs(posy - y) && posx == x)//判断是否是相邻的坐标 { //先交换 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()//将底层的空白的方块全部上移,将上面的非空白方块下移补齐 { for(int i = row-1; i > 0; i--)//行--从最后一行开始 { for(int j = 0; j < col; j++)//列---从第一列开始 { if(mybutton[i*col+j].GetID() == EMPTY)//交换每个按钮决定图像的ID号即可 { //交换同列上面非EMPTY的结点 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)//----------只是扫描所有的元素是否存在相邻的连续方块 { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(YesOrNoThreeLink(i,j)) { return true; } } } } else if(flag == 0)//-------扫描加消除该节点置为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()//将所有的按钮的图标重新绘制一次 { 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()//更新按钮里面的坐标id-----让EMPTY的按钮重新获得随机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)//监听机制 { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(e.getSource() == mybutton[i*col+j])//交换元素对象 { SwapElement(i,j); GameFrame.texteara.setText(Integer.toString(GetGrade())); break; } } } if(grade > 8000 && GameFrame.timer.isRunning()) { JOptionPane.showConfirmDialog(null, "恭喜您过关", "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类,设计游戏窗口和创建游戏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("游戏结束"); private JLabel labelGrade = new JLabel("游戏得分"); private JLabel labelTime = new JLabel("游戏时间"); public static JTextField texteara = new JTextField(10);//设置文本编辑域 GamePanel gamepanel = new GamePanel(); private int gamerow = gamepanel.GetRow(); private int gamecol = gamepanel.GetCol(); private JProgressBar progressbar = new JProgressBar();//创建一个进度条 public static Timer timer;//创建一个时间计时器 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);//添加时间编辑框 paneone.add(progressbar);//添加一个进度条 paneone.add(buttonend);//添加结束按钮 con.add(paneone,BorderLayout.NORTH); con.add(gamepanel,BorderLayout.CENTER); this.setBounds(300, 0, 600, 700); this.setTitle("对对碰游戏"); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭 buttonstart.addActionListener(this); buttonend.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource() == buttonstart)//点击开始按钮 { gamepanel.SetGrade(0);//重新设置得分 buttonstart.setEnabled(false); progressbar.setMaximum(100);//设置进度条的最大最小范围 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//创建了一个自定义的时间监听机制 { 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(); }}

- 运行结果

这是运行后的游戏开始界面:

这是点击开始以后的界面,这样你就可以开始玩你的小游戏了!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: java实现对对碰小游戏

以上就上有关java实现对对碰小游戏的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.