java连连看小游戏代码详解,java 连连看
本文实例为大家分享了爪哇实现连连看游戏的具体代码,供大家参考,具体内容如下
大二时做的爪哇课程设计,拿了个优秀,用了图标来模拟做了个简单的连连看,代码量不多,仅供参考。
课设要求是实现连连看最基本的功能,所以这里写了个简单的初始界面和经典模式的一个界面。
初始界面
代码如下:
public class PictureMatching { JButton classic=new JButton(经典模式);JButton about=new JButton(关于游戏);JButton exit=new JButton(退出游戏);窗口菜单=新JFrame(连连看);公共图片匹配(){菜单。设置布局(新建流动布局(流动布局.中心,40,40));//布局JLabel label=new JLabel(连连看);//设置字体字体font=新字体(黑体,字体。平原,26);label.setFont(字体);classical.setFont(字体);about.setFont(字体);exit.setFont(字体);//组件放入容器中menus.add(标签);menus.add(古典);menus.add(关于);menus.add(退出);按钮监听器listener=new按钮监听器();经典。addactionlistener(监听器);about.addActionListener(监听器);exit.addActionListener(监听器);menus.setSize(300,450);菜单。setlocationrelativeto(null);菜单。setdefaultcloseoperation(JFrame .EXIT _ ON _ CLOSE);菜单。设置可见(真);}公共类按钮监听器实现ActionListener{//初始界面的监听器@ Override public void action performed(action event e){ if((JButton)e . getsource()==classic){ new classic();} else if((JButton)e . getsource()==About){ new About();} else if((JButton)e . getsource()==exit)系统。退出(0);} } //主函数public static void main(String args[]){ try { ui manager。setlookandfeel(ui管理器。getsystemlookandfeelclassname());}catch(异常e){ }新图片匹配();}}"关于游戏界面可以写一些信息,这里不多赘述。
经典模式的界面如下:
代码如下:
经典类扩展JFrame实现操作侦听器{ Piture Piture[][]=new Piture[12][12];检查检查;组件标签,时间,分数,规则,道具,赢,输;钛
me 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!=iy1!=j){ x2 = i;y2 = j;//将 } } } } &
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。