java实现对对碰小游戏
本文实例为大家分享了java实现对对碰的具体代码,供大家参考,具体内容如下
-游戏实现功能:分别点击两张相邻的图像按钮进行交换(重点相邻),交换后的两个图像按钮的相邻水平或者垂直方向上,与之相同的图像超过规定个数后(这里规定为3个)就将其全部消除(置为空白按钮),上面的图像按钮也分别随之向下移动,将空白补齐(这里我们可以理解为图像按钮和空白按钮进行交换)。
-游戏设计思路:
1.创建图像按钮数组,设置其基本属性;
2.给每个图像按钮配置相应的ID,用来标记图像信息,对于ID相同的按钮,图像也相同;
3.设置遍历全局判断是否可以建立相连按钮的函数;
4.设置遍历全局将可以建立相连的按钮图像ID置为EMPTY,将按钮置为空白按钮;
5.设置移动函数,将空白按钮与上层非空白按钮相互交换的函数,将空白按钮移动到上层;
6.设置更新函数,将移动到上层的空白按钮再随机匹配图像,继续使用;
7.设置交换按钮函数;
8.记录游戏得分,给一个确定的目标成绩,达到即可赢得游戏;
9.设置一个进度条,记录游戏进行的时间;
10.设置一个时间记录器(定时器);
11.设计游戏界面基本信息(根据个人爱好设计即可);
-游戏代码
---mybutton类,设置了每个按钮对象的基本信息 packagesupperzzle; importjavax.swing.Icon; importjavax.swing.JButton; publicclassMyButtonextendsJButton{ privatefinalintWidth=30;//设置按钮的宽度 privatefinalintHeight=30; privateintID;//设置按钮的ID-----ID代表每一个按钮里面存放的数据 privateintbuttonPosX=0; privateintbuttonPosY=0; publicMyButton(intid,Iconicon)//构造函数 { this.setIcon(icon); this.ID=id; this.setSize(Width,Height);//设置按钮的边框大小 this.setFocusable(true);//去掉按钮的聚焦框 this.setBorderPainted(false);//去掉边框 this.setContentAreaFilled(false);//不显示外围矩形边框 } publicintGetID() { returnID; } publicvoidSetID(intid) { this.ID=id; } }
//-----这是游戏的重点了,基本游戏界面设计--GamePanel类---对游戏的界面进行了基本的设置(写的有点挫,,有什么好的建议请尽情提的不要拘谨,哈哈) packagesupperzzle; importjava.awt.GridLayout; importjava.awt.Image; importjava.awt.Toolkit; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.util.EventListener; importjava.util.Random; importjavax.swing.ImageIcon; importjavax.swing.JOptionPane; importjavax.swing.JPanel; publicclassGamePanelextendsJPanelimplementsActionListener{ privatefinalintrow=10; privatefinalintcol=10; privatefinalintlineCount=3;//设置几连可碰消除 privateintgrade=0;//记录得分 privatefinalintscore=10;//设置每次消去一个方块获得的分数 publicMyButtonmybutton[]=newMyButton[row*col];//这里只是开辟了相应的空间 privatefinalintcountImage=7; privateImageIconimageIcon[]=newImageIcon[countImage];//设置图标数组 privatefinalintEMPTY=-1; privateRandomrandom=newRandom(); privateintposx=0; privateintposy=0;//保存第一次按钮按下去的坐标 privatebooleanIsSecond=false; publicGamePanel()//游戏面板的构造函数----实现图片加载,数组图标的加载,以及按钮的基本设置 { this.setLayout(newGridLayout(row,col,0,0));//创建一个网络布局管理格式---row行col列 for(inti=0;i=0;i--) { if(mybutton[i*col+y].GetID()==mybutton[x*col+y].GetID()) { linked++; } else { break; } } for(inti=x+1;i =lineCount) { returntrue; } //判断水平方向上面是否有里连续相同的方块 linked=1; for(inti=y-1;i>=0;i--)//判断水平向左 { if(mybutton[x*col+i].GetID()==mybutton[x*col+y].GetID()) { linked++; } else break; } for(inti=y+1;i
=lineCount)//说明满足条件建立了连线 { returntrue; } returnfalse; } publicvoidRemoveLink(intx,inty)//移除相同ID的方块设置相同的方块ID为EMPTY,并计算得分 { intlinked1=0; intlinked2=0; inttempxStart=x; inttempxEnd=x;//分别保存第一个和最后一个与该点ID相同的x坐标 inttempyStart=y; inttempyEnd=y;//分别保存第一个和最后一个与该点ID相同的y坐标 //先判断垂直方向上面的---上下行 for(inti=x-1;i>=0;i--)//判断该坐标的上面 { if(mybutton[i*col+y].GetID()==mybutton[x*col+y].GetID()) { linked1++; tempxStart=i; } else { break; } } for(inti=x+1;i =lineCount) { for(inti=tempxStart;i<=tempxEnd;i++) { mybutton[i*col+y].SetID(EMPTY); } //grade+=linked*score; grade+=linked1*score; } //判断水平方向上面的---左右列 for(inti=y-1;i>=0;i--)//判断水平向左 { if(mybutton[x*col+i].GetID()==mybutton[x*col+y].GetID()) { linked2++; tempyStart=i; } else break; } for(inti=y+1;i
=lineCount)//说明满足条件建立了连线 { for(inti=tempyStart;i<=tempyEnd;i++) { mybutton[x*col+i].SetID(EMPTY); } //grade+=score*linked; grade+=score*linked2; } grade+=score; } publicintGetGrade()//获取得分 { returngrade; } publicvoidSetGrade(intn) { this.grade=n; } publicvoidSwapElement(intx,inty)//交换元素 { if(x>=0&&x =0&&y
"+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("交换"); inttemp=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; } } } publicvoidDownButton()//将底层的空白的方块全部上移,将上面的非空白方块下移补齐 { for(inti=row-1;i>0;i--)//行--从最后一行开始 { for(intj=0;j =0;k--) { if(mybutton[k*col+j].GetID()!=EMPTY) { intid=mybutton[i*col+j].GetID(); mybutton[i*col+j].SetID(mybutton[k*col+j].GetID()); mybutton[k*col+j].SetID(id); break; } } } } } } publicbooleanGlobalSearch(intflag)//全盘扫描 { if(flag==1)//----------只是扫描所有的元素是否存在相邻的连续方块 { for(inti=0;i 8000&&GameFrame.timer.isRunning()) { JOptionPane.showConfirmDialog(null,"恭喜您过关","Win",JOptionPane.CLOSED_OPTION); for(inti=0;i
//游戏走到这里就是真正的游戏接口了,游戏开始的入口--GameFrame类,设计游戏窗口和创建游戏 packagesupperzzle; importjava.awt.BorderLayout; importjava.awt.Container; importjava.awt.FlowLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JLabel; importjavax.swing.JPanel; importjavax.swing.JProgressBar; importjavax.swing.JTextField; importjavax.swing.Timer; publicclassGameFrameextendsJFrameimplementsActionListener{ privateJPanelpaneone=newJPanel(); publicstaticJButtonbuttonstart=newJButton("游戏开始"); privateJButtonbuttonend=newJButton("游戏结束"); privateJLabellabelGrade=newJLabel("游戏得分"); privateJLabellabelTime=newJLabel("游戏时间"); publicstaticJTextFieldtexteara=newJTextField(10);//设置文本编辑域 GamePanelgamepanel=newGamePanel(); privateintgamerow=gamepanel.GetRow(); privateintgamecol=gamepanel.GetCol(); privateJProgressBarprogressbar=newJProgressBar();//创建一个进度条 publicstaticTimertimer;//创建一个时间计时器 publicGameFrame() { Containercon=this.getContentPane(); con.setLayout(newBorderLayout()); paneone.setLayout(newFlowLayout()); 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); } publicvoidactionPerformed(ActionEvente) { if(e.getSource()==buttonstart)//点击开始按钮 { gamepanel.SetGrade(0);//重新设置得分 buttonstart.setEnabled(false); progressbar.setMaximum(100);//设置进度条的最大最小范围 progressbar.setMinimum(0); progressbar.setStringPainted(true); timer=newTimer(800,newTimeListener()); timer.start(); for(inti=0;i100) { timer.stop(); for(inti=0;i -运行结果
这是运行后的游戏开始界面:
这是点击开始以后的界面,这样你就可以开始玩你的小游戏了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。