当前位置:   article > 正文

JAVA开发:打地鼠游戏_用java编写打地鼠

用java编写打地鼠

Mouse.java

  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.event.KeyEvent;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.Random;
  10. public class Mouse {
  11. private int x, y;
  12. private int tab;
  13. private int data;
  14. Random rd = new Random();
  15. private boolean live = true;
  16. private int mouseTime = 10;
  17. private MouseClient mc;
  18. private static Toolkit tk = Toolkit.getDefaultToolkit();
  19. private static Image[] imgs = null;
  20. private static Map<Image, String> imgKV = new HashMap<Image, String>();
  21. static {
  22. imgs = new Image[] {
  23. tk.getImage(Mouse.class.getClassLoader().getResource(
  24. "images/A.png")),
  25. tk.getImage(Mouse.class.getClassLoader().getResource(
  26. "images/B.png")),
  27. tk.getImage(Mouse.class.getClassLoader().getResource(
  28. "images/C.png")),
  29. tk.getImage(Mouse.class.getClassLoader().getResource(
  30. "images/D.png")),
  31. tk.getImage(Mouse.class.getClassLoader().getResource(
  32. "images/E.png")),
  33. tk.getImage(Mouse.class.getClassLoader().getResource(
  34. "images/F.png")),
  35. tk.getImage(Mouse.class.getClassLoader().getResource(
  36. "images/G.png")),
  37. tk.getImage(Mouse.class.getClassLoader().getResource(
  38. "images/H.png")),
  39. tk.getImage(Mouse.class.getClassLoader().getResource(
  40. "images/I.png")),
  41. tk.getImage(Mouse.class.getClassLoader().getResource(
  42. "images/J.png")),
  43. tk.getImage(Mouse.class.getClassLoader().getResource(
  44. "images/K.png")),
  45. tk.getImage(Mouse.class.getClassLoader().getResource(
  46. "images/L.png")),
  47. tk.getImage(Mouse.class.getClassLoader().getResource(
  48. "images/M.png")),
  49. tk.getImage(Mouse.class.getClassLoader().getResource(
  50. "images/N.png")),
  51. tk.getImage(Mouse.class.getClassLoader().getResource(
  52. "images/O.png")),
  53. tk.getImage(Mouse.class.getClassLoader().getResource(
  54. "images/P.png")),
  55. tk.getImage(Mouse.class.getClassLoader().getResource(
  56. "images/Q.png")),
  57. tk.getImage(Mouse.class.getClassLoader().getResource(
  58. "images/R.png")),
  59. tk.getImage(Mouse.class.getClassLoader().getResource(
  60. "images/S.png")),
  61. tk.getImage(Mouse.class.getClassLoader().getResource(
  62. "images/T.png")),
  63. tk.getImage(Mouse.class.getClassLoader().getResource(
  64. "images/U.png")),
  65. tk.getImage(Mouse.class.getClassLoader().getResource(
  66. "images/V.png")),
  67. tk.getImage(Mouse.class.getClassLoader().getResource(
  68. "images/W.png")),
  69. tk.getImage(Mouse.class.getClassLoader().getResource(
  70. "images/X.png")),
  71. tk.getImage(Mouse.class.getClassLoader().getResource(
  72. "images/Y.png")),
  73. tk.getImage(Mouse.class.getClassLoader().getResource(
  74. "images/Z.png")),
  75. tk.getImage(Mouse.class.getClassLoader().getResource(
  76. "images/0.png")),
  77. tk.getImage(Mouse.class.getClassLoader().getResource(
  78. "images/1.png")),
  79. tk.getImage(Mouse.class.getClassLoader().getResource(
  80. "images/2.png")),
  81. tk.getImage(Mouse.class.getClassLoader().getResource(
  82. "images/3.png")),
  83. tk.getImage(Mouse.class.getClassLoader().getResource(
  84. "images/4.png")),
  85. tk.getImage(Mouse.class.getClassLoader().getResource(
  86. "images/5.png")),
  87. tk.getImage(Mouse.class.getClassLoader().getResource(
  88. "images/6.png")),
  89. tk.getImage(Mouse.class.getClassLoader().getResource(
  90. "images/7.png")),
  91. tk.getImage(Mouse.class.getClassLoader().getResource(
  92. "images/8.png")),
  93. tk.getImage(Mouse.class.getClassLoader().getResource(
  94. "images/9.png"))
  95. };
  96. imgKV.put(imgs[0], "A");
  97. imgKV.put(imgs[1], "B");
  98. imgKV.put(imgs[2], "C");
  99. imgKV.put(imgs[3], "D");
  100. imgKV.put(imgs[4], "E");
  101. imgKV.put(imgs[5], "F");
  102. imgKV.put(imgs[6], "G");
  103. imgKV.put(imgs[7], "H");
  104. imgKV.put(imgs[8], "I");
  105. imgKV.put(imgs[9], "J");
  106. imgKV.put(imgs[10], "K");
  107. imgKV.put(imgs[11], "L");
  108. imgKV.put(imgs[12], "M");
  109. imgKV.put(imgs[13], "N");
  110. imgKV.put(imgs[14], "O");
  111. imgKV.put(imgs[15], "P");
  112. imgKV.put(imgs[16], "Q");
  113. imgKV.put(imgs[17], "R");
  114. imgKV.put(imgs[18], "S");
  115. imgKV.put(imgs[19], "T");
  116. imgKV.put(imgs[20], "U");
  117. imgKV.put(imgs[21], "V");
  118. imgKV.put(imgs[22], "W");
  119. imgKV.put(imgs[23], "X");
  120. imgKV.put(imgs[24], "Y");
  121. imgKV.put(imgs[25], "Z");
  122. imgKV.put(imgs[26], "0");
  123. imgKV.put(imgs[27], "1");
  124. imgKV.put(imgs[28], "2");
  125. imgKV.put(imgs[29], "3");
  126. imgKV.put(imgs[30], "4");
  127. imgKV.put(imgs[31], "5");
  128. imgKV.put(imgs[32], "6");
  129. imgKV.put(imgs[33], "7");
  130. imgKV.put(imgs[34], "8");
  131. imgKV.put(imgs[35], "9");
  132. }
  133. public Mouse(int x, int y, int tab, MouseClient mc) {
  134. this.x = x;
  135. this.y = y;
  136. this.tab = tab;
  137. this.mc = mc;
  138. data = rd.nextInt(36);
  139. new Thread(new MouseTime()).start();
  140. }
  141. public void draw(Graphics g) {
  142. if (!live) {
  143. mc.mouses.remove(this);
  144. mc.tab[tab] = 0;
  145. return;
  146. }
  147. g.drawImage(imgs[data], x, y, null);
  148. Color c = g.getColor();
  149. g.setColor(Color.gray);
  150. g.setFont(new Font(null, 1, 40));
  151. g.drawString("" + mouseTime, x + 50, y + 10);
  152. if (mouseTime <= 0) {
  153. this.live = false;
  154. mc.eclipseTime++;
  155. }
  156. }
  157. public boolean keyReleased(KeyEvent e) {
  158. int key = imgKV.get(imgs[data]).charAt(0);
  159. if (e.getKeyCode() == key) {
  160. System.out.println(key);
  161. mc.mouseCount++;
  162. this.live = false;
  163. return true;
  164. }
  165. return false;
  166. }
  167. private class MouseTime implements Runnable {
  168. public void run() {
  169. while (true) {
  170. mouseTime--;
  171. try {
  172. Thread.sleep(1000);
  173. } catch (InterruptedException e) {
  174. e.printStackTrace();
  175. }
  176. }
  177. }
  178. }
  179. }

MouseClient.java

  1. import java.awt.Color;
  2. import java.awt.Frame;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Random;
  12. public class MouseClient extends Frame {
  13. /*
  14. * 整个游戏界面的规格
  15. */
  16. public static final int GAME_WIDTH = 1000;
  17. public static final int GAME_HEIGHT = 700;
  18. public static int mouseCount;
  19. public static int count;
  20. public static int gameTime;
  21. public static int eclipseTime;
  22. List<Mouse> mouses = new ArrayList<Mouse>();
  23. Random rd = new Random();
  24. public int initMouseCount = 2;// 同时显示的地鼠数
  25. public int[] tab = new int[10];// 标记坑里是否有地鼠
  26. Image offScreenImage = null;
  27. /*
  28. * 本方法显示主窗口
  29. */
  30. public void launchFrame() {
  31. this.setLocation(200, 10);
  32. this.setSize(GAME_WIDTH, GAME_HEIGHT);
  33. this.setTitle("MouseClient");
  34. this.addWindowListener(new WindowAdapter() {
  35. public void windowClosing(WindowEvent e) {
  36. System.exit(0);
  37. }
  38. });
  39. this.setVisible(true);
  40. this.setBackground(Color.GREEN);
  41. this.setResizable(false);
  42. this.addKeyListener(new KeyMonitor());
  43. new Thread(new PaintThread()).start();
  44. new Thread(new GameTime()).start();
  45. }
  46. private class GameTime implements Runnable {
  47. public void run() {
  48. while (true) {
  49. gameTime++;
  50. try {
  51. Thread.sleep(1000);
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. }
  58. private class PaintThread implements Runnable {
  59. public void run() {
  60. while (true) {
  61. repaint();
  62. try {
  63. Thread.sleep(10);
  64. } catch (InterruptedException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. }
  70. public void update(Graphics g) {
  71. if (offScreenImage == null) {
  72. offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
  73. }
  74. Graphics gOffScreen = offScreenImage.getGraphics();
  75. Color c = gOffScreen.getColor();
  76. gOffScreen.setColor(Color.GREEN);
  77. gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
  78. gOffScreen.setColor(c);
  79. paint(gOffScreen);
  80. g.drawImage(offScreenImage, 0, 0, null);
  81. }
  82. public void paint(Graphics g) {
  83. Color c = g.getColor();
  84. g.setColor(Color.gray);
  85. for (int i = 1; i <= 3; i++)
  86. for (int j = 1; j <= 3; j++)
  87. g.fillOval(GAME_WIDTH / 4 * i - 60, GAME_HEIGHT / 4 * j - 25,
  88. 120, 50);
  89. g.setColor(c);
  90. g.drawString("击打数:" + count, 20, 40);
  91. g.drawString("击中数:" + mouseCount, 20, 60);
  92. g.drawString("逃跑数:" + eclipseTime + "", 20, 80);
  93. g.drawString("时间:" + gameTime + "s", 20, 100);
  94. if (mouseCount >= 80)
  95. initMouseCount = 8;
  96. else if (mouseCount >= 50)
  97. initMouseCount = 6;
  98. else if (mouseCount >= 20)
  99. initMouseCount = 4;
  100. int temp;
  101. while (mouses.size() < initMouseCount) {
  102. temp = rd.nextInt(9);
  103. while (tab[temp] == 1) {
  104. temp++;
  105. if (temp == 9)
  106. temp = 0;
  107. }
  108. tab[temp] = 1;
  109. mouses.add(new Mouse(GAME_WIDTH / 4 * (temp % 3 + 1) - 70,
  110. GAME_HEIGHT / 4 * (temp / 3 + 1) - 100, temp, this));
  111. }
  112. for (int i = 0; i < mouses.size(); i++) {
  113. Mouse m = mouses.get(i);
  114. m.draw(g);
  115. }
  116. }
  117. public static void main(String[] args) {
  118. MouseClient km = new MouseClient();
  119. km.launchFrame();
  120. }
  121. private class KeyMonitor extends KeyAdapter {
  122. public void keyReleased(KeyEvent e) {
  123. System.out.println(e.getKeyCode());
  124. count++;
  125. for (int i = 0; i < mouses.size(); i++) {
  126. if (mouses.get(i).keyReleased(e))
  127. break;
  128. }
  129. }
  130. }
  131. }

运行结果:

获取图片资源地址:Java打地鼠游戏配套图片-Java文档类资源-CSDN下载。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/746397
推荐阅读
相关标签
  

闽ICP备14008679号