Java实现简单连连看游戏
本文实例为大家分享了Java实现连连看游戏的具体代码,供大家参考,具体内容如下
大二时做的Java课程设计,拿了个优秀,用了icon来模拟做了个简单的连连看,代码量不多,仅供参考。
课设要求是实现连连看最基本的功能,所以这里写了个简单的初始界面和经典模式的一个界面。
初始界面
代码如下:
public class PictureMatching { JButton classical = new JButton("经典模式"); JButton about = new JButton("关于游戏"); JButton exit= new JButton("退出游戏"); JFrame menus = new JFrame("连连看"); public PictureMatching(){ menus.setLayout(new FlowLayout(FlowLayout.CENTER,40,40));//布局 JLabel label = new JLabel("连连看"); //设置字体 Font font = new Font("黑体",Font.PLAIN,26); label.setFont(font); classical.setFont(font); about.setFont(font); exit.setFont(font); //组件放入容器中 menus.add(label); menus.add(classical); menus.add(about); menus.add(exit); Buttonlistener listener = new Buttonlistener(); classical.addActionListener(listener); about.addActionListener(listener); exit.addActionListener(listener); menus.setSize(300, 450); menus.setLocationRelativeTo(null); menus.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); menus.setVisible(true); } public class Buttonlistener implements ActionListener{//初始界面的监听器 @Override public void actionPerformed(ActionEvent e) { if((JButton)e.getSource() == classical){ new Classical(); } else if ((JButton)e.getSource() == about) { new About(); } else if((JButton)e.getSource() == exit) System.exit(0); } } //主函数 public static void main(String args[]){ try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch (Exception e) {} new PictureMatching(); } }
“关于游戏"界面可以写一些信息,这里不多赘述。
经典模式的界面如下:
代码如下:
class Classical extends JFrame implements ActionListener{ Piture piture[][]=new Piture[12][12]; Check check;JLabel label,time,score,rule,prop,win,lose; Time timer; CardLayout card = new CardLayout(); JPanel panel = new JPanel(card); JPanel addpanel1 = new JPanel();//按钮面板 JPanel addpanel2 = new JPanel();//暂停面板 JPanel addpanel3 = new JPanel();//win面板 JPanel addpanel4 = new JPanel();//lose面板 private int s = 0;//分数 private int best = 0; //最高分 JButton tip,reform,exit,stop,restart; int t = 3,r = 1;//提示次数,重置次数 Path path = Paths.get("D://课程设计//最高分.txt"); public Classical(){ setTitle("经典模式"); setLayout(null); label = new JLabel("经典模式"); Font font = new Font("黑体", Font.PLAIN, 40); label.setFont(font); tip = new JButton("提示X3"); reform = new JButton("重置X1"); exit = new JButton("返回"); stop = new JButton("暂停"); restart = new JButton("重新开始"); time = new JLabel(); Font song = new Font("宋体", Font.PLAIN, 24); time.setFont(song); score = new JLabel("分数:"+s); score.setFont(song); add(label);add(tip);add(reform);add(exit);add(stop);add(time);add(restart);add(score);add(panel); addpanel1.setLayout(null); addpanel2.setLayout(null); addpanel3.setLayout(null); addpanel4.setLayout(null); label.setBounds(410,50,200, 50); tip.setBounds(300,125,80,50); reform.setBounds(400,125,80,50); exit.setBounds(500,125,80,50); restart.setBounds(600,125, 100, 50); stop.setBounds(150,125,80,50); time.setBounds(80,70,250,50); score.setBounds(800,125,250,50); panel.setBounds(100, 210, 900,770); try(OutputStream output = Files.newOutputStream(path, StandardOpenOption.CREATE)){//读取最高分 java.io.InputStream input = Files.newInputStream(path,StandardOpenOption.READ); DataInputStream in = new DataInputStream(new BufferedInputStream(input)); best = in.readInt(); } catch (IOException e) { } tip.addActionListener(this); reform.addActionListener(this); exit.addActionListener(this); stop.addActionListener(this); restart.addActionListener(this); //初始化所有的Piture类对象 for(int i = 0;i<12;i++){ for(int j = 0;j<12;j++){ piture[i][j] = new Piture(); piture[i][j].setX(i); piture[i][j].setY(j); if(i>=1&&i<=10&&j>=1&&j<=10) piture[i][j].setK(true); } } ImageIcon icons[] = new ImageIcon[28]; for(int q = 0;q<28;q++){ icons[q] = new ImageIcon("D://课程设计//图标//图标//"+(q+1)+".png");//图标路径 } //用循环将按钮赋予图标 for(int i = 1; i < 11;i++){ for(int j = 1;j<11;j+=2){ int l = (int) (Math.random()*28); piture[i][j].setBtn(new JButton(icons[l])); piture[i][j+1].setBtn(new JButton(icons[l])); } } check = new Check(); Reform(); buttonclick listener = new buttonclick(); //用循环将按钮装上监听器 for(int i = 1; i < 11;i++){ for(int j = 1;j<11;j++){ piture[i][j].getBtn().addActionListener(listener); addpanel1.add(piture[i][j].getBtn()); piture[i][j].getBtn().setBounds(80*(j-1),70*(i-1),80,70); } } rule = new JLabel("<html>规则介绍:<br><font>连连看是一个把相同两张牌连线后<br><font>消除的益智游戏,游戏时请注意,<br></font>两张牌间连线的拐点不能超过两个。</font></html> prop = new JLabel("<html>道具介绍:<br><font>提示:自动消除一对相同的卡牌<br><font>重置:用于重新洗牌的道具"); win = new JLabel(); lose = new JLabel(); rule.setFont(song); prop.setFont(song); win.setFont(song);win.setBounds(350,200, 200, 200); lose.setFont(song);lose.setBounds(350,200, 200, 200); addpanel2.setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); addpanel2.add(rule);addpanel2.add(prop); addpanel3.add(win); addpanel4.add(lose); panel.add(addpanel1,"p1");panel.add(addpanel2,"p2");panel.add(addpanel3,"p3");panel.add(addpanel4, "p4"); setSize(1000,1000); setResizable(false); setLocationRelativeTo(null); setVisible(true);timer =new Time(); } //时间类 class Time { private int minute = 0; private int second = 0; private int totalSeconds; Timer t=new Timer(); TimerTask task; private boolean Run = true; private boolean minuteNotAlready = false; private boolean secondNotAlready = false; public Time(){ totalSeconds = 60 * 3; initData(totalSeconds); t.schedule(task = new TimerTask() { public void run() { if(Run){ if (secondNotAlready) { startCount(); } else { cancel(); best = best>s?best:s; lose.setText("<html>You are lose!<br><font>分数:"+s+"<br><font>最高分:"+best); card.show(panel, "p4"); try(OutputStream output = Files.newOutputStream(path, StandardOpenOption.CREATE); DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(output))){ dataOutputStream.writeInt(best); }catch (Exception e3) { } } } } }, 0, 1000); } //初始化赋值 private void initData(int totalSeconds) { minute = 0; second = 0; minuteNotAlready = false; secondNotAlready = false; if (totalSeconds > 0) { secondNotAlready = true; second = totalSeconds; if (second >= 60) { minuteNotAlready = true; minute = second / 60; second = second % 60; } } time.setText("剩余时间:"+minute+"分钟"+second+"秒"); } //计算各个值的变动 public void startCount() { if (secondNotAlready) { if (second > 0) { second--; if (second == 0 && !minuteNotAlready) { secondNotAlready = false; } } else { if (minuteNotAlready) { if (minute > 0) { minute--; second = 59; if (minute == 0) { minuteNotAlready = false; } } } } } time.setText("剩余时间:"+minute+"分钟"+second+"秒"); } } @Override public void actionPerformed(ActionEvent e) { if((JButton)e.getSource() == tip){ if(t>0){ Tip(); t--; tip.setText("提示X"+ t ); } } else if((JButton)e.getSource() == reform){ if(r>0){ Reform(); r--; reform.setText("重置X"+r); } } else if((JButton)e.getSource() == stop){ if(stop.getText().equals("暂停")){ timer.Run = false; stop.setText("开始"); card.show(panel, "p2");//显示暂停面板,即游戏规则 return; } else if (stop.getText().equals("开始")) { timer.Run = true; stop.setText("暂停"); card.show(panel, "p1"); return; } } else if((JButton)e.getSource() == exit){ setVisible(false); } else if((JButton)e.getSource() == restart){ setVisible(false); Classical classical = new Classical(); } } //图案的匹配消除监听器 public class buttonclick implements ActionListener{ int x1,y1,x2,y2;//分别表示第一个卡牌和第二个卡牌的坐标 public buttonclick () { x1 = -1;//初始化 x2 = -1; y1 = -1; y2 = -1; } @Override public void actionPerformed(ActionEvent e) { if(x1 == -1){//如果第一个卡牌的坐标为初始值,将此时点击的按钮的内容赋予第一个卡牌 for(int i = 1; i < 11;i++){ for(int j = 1;j<11;j++){ if((JButton)e.getSource()==piture[i][j].getBtn()){ x1 = i;y1 = j;//将卡牌坐标赋予第一个卡牌 } } } } else{//如果第一个卡牌的坐标不为初始值,将此时点击的按钮的内容赋予第er个卡牌 for(int i = 1; i < 11;i++){ for(int j = 1;j<11;j++){ if((JButton)e.getSource()==piture[i][j].getBtn()){ if(x1!=i||y1!=j){ x2 = i;y2 = j;//将 } } } } } if(x1 != -1&&x2 != -1){//当两个卡牌的值都不为初始值 if(piture[x1][y1].getBtn().getIcon() == piture[x2][y2].getBtn().getIcon()){//比较两个按钮的图标 boolean w = check.isMatch(new Point(x1, y1),new Point(x2,y2));//比较是否匹配 if(w){ addpanel1.remove(piture[x1][y1].getBtn()); piture[x1][y1].setK(false); addpanel1.remove(piture[x2][y2].getBtn()); piture[x2][y2].setK(false); x1 = -1;y1 = -1; x2 = -1;y2 = -1; s = s + 200; score.setText("分数:"+s); setVisible(false); setVisible(true); } } x1 = x2; y1 = y2; x2 = -1; y2 = -1; } try { if(isReform()) Reform(); } catch (Exception e2) { timer.Run = false; s = s + (timer.minute*60+timer.second)*100; best = best>s?best:s; win.setText("<html>You are win!<br><font>分数:"+s+"<br><font>最高分:"+best); card.show(panel, "p3");//显示win面板 //将最高分写入文件 try(OutputStream output = Files.newOutputStream(path, StandardOpenOption.CREATE); DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(output))){ dataOutputStream.writeInt(best); }catch (Exception e3) { } } } } //提示功能 public void Tip(){ int p=0,p1 = 0; for(int i = 0;i<12;i++){ for(int j = 0;j<12;j++){ if(piture[i][j].isK()){ p++; } } } Piture pit[] = new Piture[p]; for(int i = 1;i<12;i++){ for(int j = 1;j<12;j++){ if(piture[i][j].isK()){ pit[p1] = piture[i][j]; p1++; } } } //检测匹配的卡牌,消除找到的第一对卡牌 for(int m = 0;m<pit.length-1;m++){ for(int n = m+1;n<pit.length;n++){ if(pit[m].getBtn().getIcon() == pit[n].getBtn().getIcon()) if(check.isMatch(new Point(pit[m].getX(),pit[m].getY()), new Point(pit[n].getX(),pit[n].getY()))){ addpanel1.remove(pit[m].getBtn()); pit[m].setK(false); addpanel1.remove(pit[n].getBtn()); pit[n].setK(false); s = s + 200; score.setText("分数:"+s); setVisible(false); setVisible(true); return; } } } } //重置功能 public void Reform() { int p=0,p1 = 0; //将有图案的放入一个数组中 for(int i = 0;i<12;i++){ for(int j = 0;j<12;j++){ if(piture[i][j].isK()){ p++; } } } Piture pit[] = new Piture[p]; for(int i = 1;i<12;i++){ for(int j = 1;j<12;j++){ if(piture[i][j].isK()){ pit[p1] = piture[i][j]; p1++; } } } //将图案进行打乱 for(int k = 0;k<=pit.length/2;k++){ int l = (int)(Math.random()*pit.length); Piture t = new Piture(); t.getBtn().setIcon(pit[k].getBtn().getIcon()); pit[k].getBtn().setIcon(pit[l].getBtn().getIcon()); pit[l].getBtn().setIcon(t.getBtn().getIcon()); } setVisible(false); setVisible(true); try { if(isReform()) Reform(); } catch (Exception e) { } } //检测是否需要重置 public boolean isReform(){ boolean is = true; int p=0,p1 = 0; //将有图案的放入一个数组中 for(int i = 0;i<12;i++){ for(int j = 0;j<12;j++){ if(piture[i][j].isK()){ p++; } } } Piture pit[] = new Piture[p]; for(int k = 1;k<12;k++){ for(int l = 1;l<12;l++){ if(piture[k][l].isK()){ pit[p1] = piture[k][l]; p1++; } } } for(int m = 0;m<pit.length-1;m++) for(int n =m+1;n<pit.length;n++) if(check.isMatch(new Point(pit[m].getX(),pit[m].getY()), new Point(pit[n].getX(),pit[n].getY()))) is = false; return is; } class Check{ public boolean isMatch(Point a,Point b){ //检测是否匹配 boolean b1,b2,b3; b3 = this.noCorner(a,b); b1 = this.oneCorner(a, b); b2 = this.twoCorner(a, b); if(b1||b2||b3) return true; else return false; } boolean horizonMatch(Point a,Point b){//横线上的扫描 int i,j; i=a.y>b.y?a.y:b.y; j=a.y>b.y?b.y:a.y; boolean hor = true; for(int y = j+1;y<i;y++){ if(piture[a.x][y].isK()){ hor = false;break; } } return hor; } boolean verticalMatch(Point a,Point b){//竖线上的扫描 int i,j; i=a.x>b.x?a.x:b.x; j=a.x>b.x?b.x:a.x; boolean ver = true; for(int x = j+1;x<i;x++){ if(piture[x][a.y].isK()){ ver = false;break;} } return ver; } boolean noCorner(Point a,Point b){ if(a.x == b.x){ if(this.horizonMatch(a, b)){ return true; } } else if(a.y == b.y){ if(this.verticalMatch(a, b)){ return true; } } return false; } boolean oneCorner(Point a,Point b){ //一个拐点 Point c,d; boolean isMatch; c = new Point(a.x,b.y); d = new Point(b.x,a.y); if(piture[c.x][c.y].isK() == false){ isMatch = horizonMatch(a,c)&&verticalMatch(b,c); if(isMatch){ return isMatch; } } if(piture[d.x][d.y].isK() == false){ return isMatch = horizonMatch(b,d)&&verticalMatch(a,d); } else return false; } boolean twoCorner(Point a,Point b){//两个拐点 boolean v = false; //扫描a点左边的所有线 for(int y = a.y-1;y>=0;y--){ if((!piture[a.x][y].isK())&&(!piture[b.x][y].isK())&&horizonMatch(a, new Point(a.x, y))&&horizonMatch(b, new Point(b.x, y))&&verticalMatch(new Point(a.x,y),new Point(b.x,y))){ v = true;break; } } //扫描a点右边的所有线 for(int y = a.y+1;y<12;y++){ if((!piture[a.x][y].isK())&&(!piture[b.x][y].isK())&&horizonMatch(a, new Point(a.x, y))&&horizonMatch(b, new Point(b.x, y))&&verticalMatch(new Point(a.x,y),new Point(b.x,y))){ v = true;break; } } //扫描a点上面的所有线 for(int x = a.x-1;x>=0;x--){ if((!piture[x][a.y].isK())&&(!piture[x][b.y].isK())&&verticalMatch(a, new Point(x, a.y))&&verticalMatch(b,new Point(x, b.y))&&horizonMatch(new Point(x,a.y),new Point(x,b.y))){ v = true;break; } } //扫描a点下面的所有线 for(int x = a.x+1;x<12;x++){ if(!piture[x][a.y].isK()&&!piture[x][b.y].isK()&&verticalMatch(a, new Point(x, a.y))&&verticalMatch(b,new Point(x, b.y))&&horizonMatch(new Point(x,a.y),new Point(x,b.y))){ v = true;break; } } return v; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)