程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> JAVA-小青蛙跳石頭游戲,java-小青蛙石頭

JAVA-小青蛙跳石頭游戲,java-小青蛙石頭

編輯:JAVA綜合教程

JAVA-小青蛙跳石頭游戲,java-小青蛙石頭


 游戲摘自微信傳的手機網頁版小游戲,我拿來做成了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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved