当前位置:   article > 正文

Java 编写打地鼠游戏 窗体程序 完整源码_java打地鼠游戏

java打地鼠游戏

今天为大家分享打地鼠游戏的开发与制作,目前是单机版游戏,后续时间空了,会进一步完善。整个系统界面漂亮,有完整的源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行DaDiS文件即可以。同时带有详细得设计文档。

主要功能

本程序主要用来真实模仿路口的交通灯的运行机制,和现实版的交通灯运行原理是一样的。

启动方法

对DaDiS.java点右键,run as Application,启动打地鼠游戏。

一 . 游戏说明:

1. 游戏操作简单,用锤子击打(鼠标点击)地鼠即可。

2. 您的最高得分将被系统保存,若在所有玩家中排前三

您将登上高分榜,可返回大厅查看.(有待后续完成)

二 . 版权声明:

1. 游戏中的图片均由网络图片经本人整合、修改而成

2. 背景音乐来源网络

3. 如需源代码,评论或者私信

三. 若您在游戏中发现影响游戏的bug请务必联系本人改进

主要功能点

1 游戏最长时间为60秒,击打陈工一个地鼠,分数+1

2窗口现实当前的最新分数和剩余时间

3 窗口最上面有4个按钮,重新开始、暂停、说明、退出游戏

游戏运行效果

主要代码

  1. public class DaDiS extends JFrame implements Runnable, ActionListener {
  2. private static boolean isContinue = true;
  3. private static Thread t1;
  4. private boolean interrupt = false;
  5. private boolean isPause = false;
  6. private boolean oneceAgrin = true;
  7. private JFrame jf;
  8. private JLabel back;
  9. private ImageIcon imgMouse;
  10. private ImageIcon imgMouse1;
  11. private JLabel[] mouses;
  12. private JLabel scoreL;
  13. private JMenuBar bar = new JMenuBar();
  14. private JButton[] bts = new JButton[4];
  15. private Font font = new Font("楷体", 1, 18);
  16. private int score = 0;
  17. private int time;
  18. private Sound qiao = new Sound("jida.WMA");
  19. private Sound bgm = new Sound("LoveYourself.wav");
  20. public DaDiS() {
  21. jf = new JFrame("小僵尸的派对之--打地鼠");
  22. jf.setResizable(false);
  23. jf.getContentPane().setLayout(null);
  24. Container con = jf.getContentPane();
  25. back = new JLabel();
  26. back.setBounds(0, 0, 620, 420);
  27. jf.setBounds(300, 100, 620, 475);
  28. Toolkit kit = Toolkit.getDefaultToolkit();
  29. Image img = new ImageIcon(this.getClass().getResource("1.png")).getImage();
  30. Image img2 = new ImageIcon(this.getClass().getResource("11.png")).getImage();
  31. final Cursor myCursor = kit.createCustomCursor(img, new Point(3, 3), "光标名字");
  32. final Cursor myCursor2 = kit.createCustomCursor(img2, new Point(3, 3), "光标名字");
  33. // 创建一个自定义光标对象。 若要隐藏光标,可将热点Point设为0,0
  34. // 参数Point 功能未知
  35. jf.setCursor(myCursor);// setCursor()设置鼠标样式
  36. // 注意,多帧图像是无效的,可能造成此方法被挂起。
  37. ImageIcon icon = new ImageIcon(this.getClass().getResource("3.jpg"));
  38. // this.getClass()返回运行时的类,此处为DaDiS
  39. back.setIcon(icon);
  40. imgMouse = new ImageIcon(this.getClass().getResource("2.png"));
  41. // 类名.getResource("名字")查找带有给定名字的资源package_name/name
  42. // 返回一个URL对象,否则null
  43. imgMouse1 = new ImageIcon(this.getClass().getResource("22.png"));
  44. mouses = new JLabel[9];
  45. for (int i = 0; i < 9; i++) {
  46. mouses[i] = new JLabel();
  47. mouses[i].setSize(imgMouse.getIconWidth(), imgMouse.getIconHeight());
  48. // mouses[i].setIcon(imgMouse);//位置测试
  49. mouses[i].addMouseListener(new MouseAdapter() {
  50. public void mouseClicked(MouseEvent e) {
  51. Object ob = e.getSource();
  52. if (ob instanceof JLabel) {// ob属于JLabel的实例
  53. JLabel label = (JLabel) ob;
  54. if (label.getIcon() != null && label.getIcon() != imgMouse1) {
  55. qiao.play();// 添加击打音效
  56. label.setIcon(imgMouse1);
  57. score += 10;
  58. scoreL.setText("您的得分:" + score + "分 时间剩余:" + time + "s");
  59. }
  60. }
  61. }
  62. public void mousePressed(MouseEvent e) {
  63. jf.setCursor(myCursor2);
  64. }
  65. public void mouseReleased(MouseEvent e) {
  66. jf.setCursor(myCursor);
  67. }
  68. });
  69. con.add(mouses[i]);
  70. }
  71. scoreL = new JLabel();
  72. scoreL.setBounds(20, 0, 430, 50);
  73. scoreL.setFont(new Font("楷体", 1, 20));
  74. scoreL.setForeground(new Color(250, 120, 10));
  75. scoreL.setText("您的得分:" + score + "分 时间剩余:" + time + "s");
  76. con.add(scoreL);
  77. mouses[0].setLocation(105, 85);
  78. mouses[1].setLocation(255, 92);
  79. mouses[2].setLocation(409, 93);
  80. mouses[3].setLocation(80, 172);
  81. mouses[4].setLocation(252, 172);
  82. mouses[5].setLocation(408, 171);
  83. mouses[6].setLocation(75, 257);
  84. mouses[7].setLocation(257, 261);
  85. mouses[8].setLocation(425, 261);
  86. String[] name = { " 重 新 开 始 ", " 暂 停/继 续 ", " 说 明 ", " 退 出 游 戏 " };
  87. for (int i = 0; i < 4; i++) {
  88. bts[i] = new JButton(name[i]);
  89. // bts[i].setSize(150,21);
  90. bts[i].setFont(font);
  91. bts[i].setForeground(new Color(10, 120, 250));
  92. bts[i].addActionListener(this);
  93. /*
  94. * bts[i].addMouseMotionListener(new MouseMotionAdapter() { public void
  95. mouseMoveed(MouseEvent e) {
  96. **/bts[i].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  97. bar.add(bts[i]);
  98. }
  99. jf.setJMenuBar(bar);
  100. con.add(back);
  101. con.addMouseListener(new MouseAdapter() {
  102. public void mousePressed(MouseEvent e) {
  103. jf.setCursor(myCursor2);
  104. }
  105. public void mouseReleased(MouseEvent e) {
  106. jf.setCursor(myCursor);
  107. }
  108. });
  109. con.setLayout(null);
  110. jf.setResizable(false);// 窗口大小不可变
  111. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  112. jf.setVisible(true);
  113. }
  114. public static void main(String[] args) {
  115. while (isContinue) {
  116. isContinue = false;
  117. DaDiS d1 = new DaDiS();
  118. t1 = new Thread(d1);
  119. t1.start();
  120. }
  121. }
  122. public void winMessage(String str) {// 提示窗口,有多个地方调用
  123. JOptionPane.showMessageDialog(null, str, "友情提示", 1);
  124. }
  125. @Override
  126. public void actionPerformed(ActionEvent e) {
  127. if (e.getSource() == this.bts[0]) {// 重新开始
  128. isPause = true;
  129. winMessage("新局已开!\n来吧!加油!");
  130. bgm.loopPlay();
  131. isPause = false;
  132. oneceAgrin = true;
  133. } else if (e.getSource() == this.bts[1]) {// 暂停
  134. if (!isPause) {
  135. isPause = true;
  136. bgm.stop();
  137. winMessage("游戏已暂停!");
  138. } else {
  139. bgm.loopPlay();
  140. isPause = false;
  141. }
  142. } else if (e.getSource() == this.bts[2]) {// 说明应包括暂停功能
  143. isPause = true;
  144. winMessage("一 . 游戏说明:\n\n"
  145. + " 1. 游戏操作简单,用锤子击打(鼠标点击)地鼠即可。\n\n"
  146. + " 2. 您的最高得分将被系统保存,若在所有玩家中排前三\n\n"
  147. + " 您将登上高分榜,可返回大厅查看.(有待后续完成)\n\n"
  148. + "二 . 版权声明:\n\n"
  149. + " 1. 游戏中的图片均由网络图片经本人整合、修改而成;\n\n"
  150. + " 2. 背景音乐来源网络;\n\n"
  151. + " 3. 如需源代码,可联系2497737951@qq.com\n\n"
  152. + "三. 若您在游戏中发现影响游戏的bug请务必联系本人改进");
  153. isPause = false;
  154. } else {// 退出
  155. System.exit(0);
  156. }
  157. }
  158. @Override
  159. public void run() {
  160. do {
  161. bgm.loopPlay();
  162. oneceAgrin = false;
  163. this.time = 60;// 60s限时
  164. //TODO
  165. interrupt = false;
  166. this.score = 0;
  167. scoreL.setText("您的得分:" + score + "分 时间剩余:" + time + "s");
  168. int i, j, k = 0;
  169. i = (int) (Math.random() * 9);
  170. j = (int) (Math.random() * 9);
  171. while (true) {
  172. if (oneceAgrin)
  173. break;
  174. if (isPause) {
  175. System.out.println(isPause);
  176. if (oneceAgrin)
  177. break;
  178. continue;
  179. }
  180. try {
  181. Thread.sleep(200);
  182. if (mouses[j].getIcon() == null)
  183. mouses[j].setIcon(imgMouse);
  184. if (mouses[i].getIcon() == null && k % 3 == 0)
  185. mouses[i].setIcon(imgMouse);
  186. Thread.sleep(800);
  187. if (mouses[j].isShowing()) {
  188. mouses[j].setIcon(null);
  189. j = (int) (Math.random() * 9);
  190. }
  191. if (mouses[i].isShowing()) {
  192. mouses[i].setIcon(null);
  193. i = (int) (Math.random() * 9);
  194. }
  195. } catch (InterruptedException e) {// interrupted被打断
  196. e.printStackTrace();
  197. }
  198. time--;
  199. scoreL.setText("您的得分:" + score + "分 时间剩余:" + time + "s");
  200. k++;
  201. if (time <= 0)
  202. break;
  203. if (interrupt) {
  204. Thread.interrupted();
  205. }
  206. }
  207. if (mouses[j].isShowing())
  208. mouses[j].setIcon(null);
  209. if (mouses[i].isShowing())
  210. mouses[i].setIcon(null);
  211. bgm.stop();
  212. String judge;
  213. if (score <= 300)
  214. judge = "\n还要多加练习消灭地鼠哦!\n\n";
  215. else if (score <= 350)
  216. judge = "\n真不懒!地鼠都怕你了!\n\n";
  217. else if (score <= 400)
  218. judge = "\n地鼠:兄弟们!快逃啊!!!~\n\n";
  219. else if (score <= 450)
  220. judge = "\n什么情况!\n地鼠家族都要覆灭了!\n\n";
  221. else
  222. judge = "\n地鼠族长:苍天呐~\n给我们留个后代吧~(仰天大哭)\n\n";
  223. if (!oneceAgrin) {
  224. winMessage("您的时间已经用完了!\n您的得分为" + score + "\n" + judge);
  225. }
  226. while (!oneceAgrin) {
  227. //System.out.println(oneceAgrin);
  228. }
  229. } while (oneceAgrin);
  230. }
  231. }
  232. // 背景音乐
  233. class Sound {
  234. private URL sound;
  235. private AudioClip music;
  236. public Sound(String ad) {
  237. sound = this.getClass().getResource(ad);
  238. music = JApplet.newAudioClip(sound);
  239. }
  240. public void play() {
  241. music.play();
  242. }
  243. public void loopPlay() {
  244. music.loop();
  245. }
  246. public void stop() {
  247. music.stop();
  248. }
  249. }

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

闽ICP备14008679号