标签:
游戏摘自微信传的手机网页版小游戏,我拿来做成了JAVA的界面版,但是没有去做素材,,直接拿方块代替小青蛙。游戏原址就不分享了,只能在手机上打开。
下面是源码:
1 /* 2 * Main.java 3 * */ 4 5 import java.awt.Color; 6 import java.awt.Graphics; 7 import java.awt.event.KeyAdapter; 8 import java.awt.event.KeyEvent; 9 import java.awt.event.MouseAdapter; 10 import java.awt.event.MouseEvent; 11 import java.awt.event.WindowAdapter; 12 import java.awt.event.WindowEvent; 13 import java.text.DateFormat; 14 import java.text.ParseException; 15 import java.text.SimpleDateFormat; 16 import java.util.Date; 17 18 import javax.swing.JFrame; 19 20 public class Main extends JFrame { 21 /** 22 * 过关移动顺序 3、4、5、3、2、1 4、5、6、3、2、1 5、6、1 23 */ 24 public static final int GAME_WIDTH = 420; // 界面宽 25 public static final int GAME_HEIGTH = 150; // 界面高 26 public static final int GAME_X = 400; // 界面位置 27 public static final int GAME_Y = 250; 28 public DateFormat format = new SimpleDateFormat("mm:ss"); // 时间格式 29 public String StartDate = new String(); // 起始时间 30 public String EndtDate = new String(); // 当前时间 31 public long times; 32 Frog f = new Frog(this); // 青蛙类 33 34 public static void main(String[] args) { 35 Main m = new Main(); 36 m.lauchFrame(); // 初始化界面 37 } 38 39 private void lauchFrame() { 40 // TODO Auto-generated method stub 41 Date date = new Date(); 42 StartDate = format.format(date); 43 EndtDate = format.format(date); 44 this.setLocation(GAME_X, GAME_Y); // 窗口初始坐标点 45 this.setSize(GAME_WIDTH, GAME_HEIGTH); // 窗口初始大小 46 this.setTitle("Frog"); // 窗口名称 47 /* 窗口监听 */ 48 this.addWindowListener(new WindowAdapter() { 49 @Override 50 /* 点退出叉之后运行 */ 51 public void windowClosing(WindowEvent e) { 52 // TODO Auto-generated method stub 53 System.exit(0); // 退出 54 } 55 }); 56 this.addMouseListener(new Mouse()); // 添加鼠标监听事件 57 this.addKeyListener(new Key()); // 添加键盘监听事件 58 this.setVisible(true); // 设置窗口显现 59 this.setResizable(false); // 设置窗口不可改变大小 60 this.getContentPane().setBackground(Color.white); // 界面背景白色 61 new Thread(new PaintThread()).start(); // 开始运行PaintThread类run->repaint->paint 62 } 63 64 public void paint(Graphics g) { 65 super.paint(g); 66 for (int i = 1; i < 8; i++) { 67 g.drawRect(i * 50 - 5, 45, 30, 30);// 画出方框 68 } 69 f.Draw(g); // 画方块 70 /* 显示时间以及提示说明 */ 71 try { 72 times = format.parse(EndtDate).getTime() - format.parse(StartDate).getTime(); 73 g.drawString("时间:" + format.format(new Date(times)).toString(), 250, 40); 74 } catch (ParseException e) { 75 // TODO Auto-generated catch block 76 e.printStackTrace(); 77 } 78 g.drawString("F2键重新开始", 100, 40); 79 g.drawString("规则:1、点击方块跳动", 85, 100); 80 g.drawString("2、方块不能向后跳,最多只能跳过1个方块", 120, 115); 81 g.drawString("3、使左右两边方块交换位置即可胜利", 120, 130); 82 } 83 84 /* 线程主要做时间和界面刷新 */ 85 private class PaintThread implements Runnable { 86 87 @Override 88 public void run() { 89 // TODO Auto-generated method stub 90 while (true) { 91 repaint(); // 刷新界面 92 Date date = new Date(); 93 EndtDate = format.format(date); // 重新获取当前时间 94 try { 95 Thread.sleep(1000); // 时隔1s 96 } catch (Exception e) { 97 e.printStackTrace(); 98 } 99 } 100 } 101 } 102 103 /* 键盘监听;调用方块类的键盘释放监听 */ 104 public class Key extends KeyAdapter { 105 106 @Override 107 public void keyReleased(KeyEvent e) { 108 // TODO Auto-generated method stub 109 super.keyReleased(e); 110 f.keyReleased(e); 111 } 112 113 } 114 115 /* 鼠标监听;调用方块类的鼠标单击监听 */ 116 public class Mouse extends MouseAdapter { 117 118 @Override 119 public void mouseClicked(MouseEvent e) { 120 // TODO Auto-generated method stub 121 super.mouseClicked(e); 122 f.mouseClicked(e); 123 } 124 } 125 }
1 /* 2 * Frog.java 3 * */ 4 import java.awt.Color; 5 import java.awt.Graphics; 6 import java.awt.event.KeyEvent; 7 import java.awt.event.MouseEvent; 8 import java.util.ArrayList; 9 import java.util.Date; 10 import java.util.List; 11 12 import javax.swing.JOptionPane; 13 14 public class Frog { 15 /* 方块类 */ 16 private class F { 17 public int x, y; // 方块的坐标 18 public boolean good; // 判断两方阵营 19 public boolean have; // 判断是否显现这个方块(7个位置只显示6个) 20 21 public F(int x, int y, boolean good, boolean have) { 22 super(); 23 // TODO Auto-generated constructor stub 24 this.x = x; 25 this.y = y; 26 this.good = good; 27 this.have = have; 28 } 29 } 30 31 private static final int FROG_WIGTH = 20; // 方块大小 32 private static final int FROG_HEIGTH = 20; 33 private static final int GAME_FROG_NUMBER = 7; // 游戏方块数量 34 private static final int FROG_X = 50; // 位置基址 35 private static final int FROG_Y = 50; 36 List<F> frog = new ArrayList<F>(); // 方块集合 37 private Main m; 38 39 public Frog(Main m) { 40 InitFrog();// 初始化集合 41 this.m = m; 42 } 43 44 private void InitFrog() { 45 for (int i = 1; i <= GAME_FROG_NUMBER; i++) { 46 if (i <= GAME_FROG_NUMBER / 2) 47 frog.add(new F(FROG_X * i, FROG_Y, true, true)); 48 else if (i == GAME_FROG_NUMBER / 2 + 1) 49 frog.add(new F(FROG_X * i, FROG_Y, true, false)); 50 else 51 frog.add(new F(FROG_X * i, FROG_Y, false, true)); 52 } 53 } 54 55 public void Draw(Graphics g) { 56 Color c = g.getColor(); // 获取当前画笔颜色 57 for (int i = 0; i < GAME_FROG_NUMBER; i++) { 58 if (frog.get(i).have) { 59 if (frog.get(i).good) // 两边分出颜色 60 g.setColor(Color.BLUE); 61 else 62 g.setColor(Color.RED); 63 g.fillRect(frog.get(i).x, frog.get(i).y, FROG_WIGTH, FROG_HEIGTH);// 相应位置画出方块 64 } 65 } 66 g.setColor(c); 67 } 68 69 public boolean Over() { 70 for (int i = 0; i < GAME_FROG_NUMBER; i++) { 71 if (i < GAME_FROG_NUMBER / 2) { 72 if (frog.get(i).good) 73 return false; 74 } else if (i == GAME_FROG_NUMBER / 2) { 75 if (frog.get(i).have) 76 return false; 77 } else { 78 if (!frog.get(i).good) 79 return false; 80 } 81 } 82 return true; 83 } 84 85 public void reset() { 86 frog.removeAll(frog); 87 InitFrog(); 88 m.repaint(); 89 Date date = new Date(); 90 m.StartDate = m.format.format(date); 91 m.EndtDate = m.format.format(date); 92 } 93 94 /* 鼠标单击监听;方块移动算法 */ 95 public void mouseClicked(MouseEvent e) { 96 int x = e.getX(); 97 int y = e.getY(); 98 if (y > 50 && y < 70 && x % 50 <= 20) { 99 x /= 50; 100 x -= 1; 101 if (frog.get(x).have) { 102 if (frog.get(x).good) { 103 if (x < GAME_FROG_NUMBER - 1) 104 if (frog.get(x + 1).have) { 105 if (x < GAME_FROG_NUMBER - 2) { 106 if (!frog.get(x + 2).have) { 107 frog.set(x, new F(FROG_X * (x + 1), FROG_Y, true, false)); 108 frog.set(x + 2, new F(FROG_X * (x + 3), FROG_Y, true, true)); 109 } 110 } 111 } else { 112 frog.set(x, new F(FROG_X * (x + 1), FROG_Y, true, false)); 113 frog.set(x + 1, new F(FROG_X * (x + 2), FROG_Y, true, true)); 114 } 115 } else { 116 if (x > 0) 117 if (frog.get(x - 1).have) { 118 if (x > 1) { 119 if (!frog.get(x - 2).have) { 120 frog.set(x, new F(FROG_X * (x + 1), FROG_Y, false, false)); 121 frog.set(x - 2, new F(FROG_X * (x - 1), FROG_Y, false, true)); 122 } 123 } 124 } else { 125 frog.set(x, new F(FROG_X * (x + 1), FROG_Y, false, false)); 126 frog.set(x - 1, new F(FROG_X * x, FROG_Y, false, true)); 127 } 128 } 129 } 130 } 131 m.repaint(); 132 if (Over()) { 133 Object[] o = { "OK", "CANCEL" }; 134 if (JOptionPane.showOptionDialog(null, "完成游戏\n你所用时间:" + "" + m.format.format(new Date(m.times)).toString(), 135 "游戏结束", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, o, 136 o[0]) == JOptionPane.YES_OPTION) { 137 System.exit(0); 138 } else { 139 reset(); 140 } 141 } 142 143 } 144 145 /* 键盘释放监听;重置用 */ 146 public void keyReleased(KeyEvent e) { 147 int key = e.getKeyCode(); 148 switch (key) { 149 case KeyEvent.VK_F2: 150 reset(); 151 break; 152 } 153 } 154 }
标签:
原文地址:http://www.cnblogs.com/ABook/p/5744573.html