本文实例讲述了Java实现的打地鼠小游戏。分享给大家供大家参考,具体如下:
这里涉及到java线程和GUI的相关知识,一个简单的java小游戏打地鼠,有兴趣的朋友可以优化一下。先来看看运行效果:
具体代码:
Mouse.java:
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.util.HashMap; import java.util.Map; import java.util.Random; public class Mouse { private int x, y; private int tab; private int data; Random rd = new Random(); private boolean live = true; private int mouseTime = 10; private MouseClient mc; private static Toolkit tk = Toolkit.getDefaultToolkit(); private static Image[] imgs = null; private static Map<Image, String> imgKV = new HashMap<Image, String>(); static { imgs = new Image[] { tk.getImage(Mouse.class.getClassLoader().getResource( "images/A.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/B.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/C.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/D.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/E.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/F.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/G.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/H.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/I.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/J.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/K.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/L.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/M.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/N.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/O.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/P.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/Q.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/R.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/S.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/T.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/U.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/V.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/W.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/X.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/Y.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/Z.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/0.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/1.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/2.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/3.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/4.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/5.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/6.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/7.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/8.png")), tk.getImage(Mouse.class.getClassLoader().getResource( "images/9.png")) }; imgKV.put(imgs[0], "A"); imgKV.put(imgs[1], "B"); imgKV.put(imgs[2], "C"); imgKV.put(imgs[3], "D"); imgKV.put(imgs[4], "E"); imgKV.put(imgs[5], "F"); imgKV.put(imgs[6], "G"); imgKV.put(imgs[7], "H"); imgKV.put(imgs[8], "I"); imgKV.put(imgs[9], "J"); imgKV.put(imgs[10], "K"); imgKV.put(imgs[11], "L"); imgKV.put(imgs[12], "M"); imgKV.put(imgs[13], "N"); imgKV.put(imgs[14], "O"); imgKV.put(imgs[15], "P"); imgKV.put(imgs[16], "Q"); imgKV.put(imgs[17], "R"); imgKV.put(imgs[18], "S"); imgKV.put(imgs[19], "T"); imgKV.put(imgs[20], "U"); imgKV.put(imgs[21], "V"); imgKV.put(imgs[22], "W"); imgKV.put(imgs[23], "X"); imgKV.put(imgs[24], "Y"); imgKV.put(imgs[25], "Z"); imgKV.put(imgs[26], "0"); imgKV.put(imgs[27], "1"); imgKV.put(imgs[28], "2"); imgKV.put(imgs[29], "3"); imgKV.put(imgs[30], "4"); imgKV.put(imgs[31], "5"); imgKV.put(imgs[32], "6"); imgKV.put(imgs[33], "7"); imgKV.put(imgs[34], "8"); imgKV.put(imgs[35], "9"); } public Mouse(int x, int y, int tab, MouseClient mc) { this.x = x; this.y = y; this.tab = tab; this.mc = mc; data = rd.nextInt(36); new Thread(new MouseTime()).start(); } public void draw(Graphics g) { if (!live) { mc.mouses.remove(this); mc.tab[tab] = 0; return; } g.drawImage(imgs[data], x, y, null); Color c = g.getColor(); g.setColor(Color.gray); g.setFont(new Font(null, 1, 40)); g.drawString("" + mouseTime, x + 50, y + 10); if (mouseTime <= 0) { this.live = false; mc.eclipseTime++; } } public boolean keyReleased(KeyEvent e) { int key = imgKV.get(imgs[data]).charAt(0); if (e.getKeyCode() == key) { System.out.println(key); mc.mouseCount++; this.live = false; return true; } return false; } private class MouseTime implements Runnable { public void run() { while (true) { mouseTime--; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
MouseClient.java:
import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; import java.util.Random; public class MouseClient extends Frame { /* * 整个游戏界面的规格 */ public static final int GAME_WIDTH = 1000; public static final int GAME_HEIGHT = 700; public static int mouseCount; public static int count; public static int gameTime; public static int eclipseTime; List<Mouse> mouses = new ArrayList<Mouse>(); Random rd = new Random(); public int initMouseCount = 2;// 同时显示的地鼠数 public int[] tab = new int[10];// 标记坑里是否有地鼠 Image offScreenImage = null; /* * 本方法显示主窗口 */ public void launchFrame() { this.setLocation(200, 10); this.setSize(GAME_WIDTH, GAME_HEIGHT); this.setTitle("MouseClient"); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setVisible(true); this.setBackground(Color.GREEN); this.setResizable(false); this.addKeyListener(new KeyMonitor()); new Thread(new PaintThread()).start(); new Thread(new GameTime()).start(); } private class GameTime implements Runnable { public void run() { while (true) { gameTime++; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class PaintThread implements Runnable { public void run() { while (true) { repaint(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } public void update(Graphics g) { if (offScreenImage == null) { offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); } Graphics gOffScreen = offScreenImage.getGraphics(); Color c = gOffScreen.getColor(); gOffScreen.setColor(Color.GREEN); gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); gOffScreen.setColor(c); paint(gOffScreen); g.drawImage(offScreenImage, 0, 0, null); } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.gray); for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) g.fillOval(GAME_WIDTH / 4 * i - 60, GAME_HEIGHT / 4 * j - 25, 120, 50); g.setColor(c); g.drawString("击打数:" + count, 20, 40); g.drawString("击中数:" + mouseCount, 20, 60); g.drawString("逃跑数:" + eclipseTime + "", 20, 80); g.drawString("时间:" + gameTime + "s", 20, 100); if (mouseCount >= 80) initMouseCount = 8; else if (mouseCount >= 50) initMouseCount = 6; else if (mouseCount >= 20) initMouseCount = 4; int temp; while (mouses.size() < initMouseCount) { temp = rd.nextInt(9); while (tab[temp] == 1) { temp++; if (temp == 9) temp = 0; } tab[temp] = 1; mouses.add(new Mouse(GAME_WIDTH / 4 * (temp % 3 + 1) - 70, GAME_HEIGHT / 4 * (temp / 3 + 1) - 100, temp, this)); } for (int i = 0; i < mouses.size(); i++) { Mouse m = mouses.get(i); m.draw(g); } } public static void main(String[] args) { MouseClient km = new MouseClient(); km.launchFrame(); } private class KeyMonitor extends KeyAdapter { public void keyReleased(KeyEvent e) { System.out.println(e.getKeyCode()); count++; for (int i = 0; i < mouses.size(); i++) { if (mouses.get(i).keyReleased(e)) break; } } } }
附:完整实例代码点击此处本站下载。
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
本文向大家介绍javascript实现打砖块小游戏(附完整源码),包括了javascript实现打砖块小游戏(附完整源码)的使用技巧和注意事项,需要的朋友参考一下 小时候玩一天的打砖块小游戏,附完整源码 在?给个赞? 实现如图 需求分析 1、小球在触碰到大盒子上、左、右边框,以及滑块后沿另一方向反弹,在碰到底边框后游戏结束; 2、小球在触碰到方块之后,方块消失; 3、消除所有方块获得游戏胜利; 4
本文向大家介绍JavaScript版经典游戏之扫雷游戏完整示例【附demo源码下载】,包括了JavaScript版经典游戏之扫雷游戏完整示例【附demo源码下载】的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JavaScript扫雷游戏。分享给大家供大家参考,具体如下: 翻出年初写的游戏贴上来,扫雷相信大家都玩过,先上图: 源码: 完整实例代码点击此处本站下载。 更多关于JavaScri
本文向大家介绍java实现的2048游戏完整实例,包括了java实现的2048游戏完整实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了java实现的2048游戏。分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码如下: PS:这里再为大家推荐另两款本站2048游戏供大家参考(JS实现) 在线2048游戏: http://tools.jb51.net/games/game20
本文向大家介绍android利用handler实现打地鼠游戏,包括了android利用handler实现打地鼠游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了android利用handler实现打地鼠游戏的具体代码,供大家参考,具体内容如下 xml java 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
本文向大家介绍Java swing框架实现的贪吃蛇游戏完整示例,包括了Java swing框架实现的贪吃蛇游戏完整示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java swing框架实现的贪吃蛇游戏。分享给大家供大家参考,具体如下: java是门高级语言,做游戏时适合做后台,但是用它也可以做游戏。闲来无事做的时候可以用来写点小游戏,练习练习预防早衰哈哈! 闲话不说了 下面是以前练习
本文向大家介绍原生JS实现的跳一跳小游戏完整实例,包括了原生JS实现的跳一跳小游戏完整实例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了原生JS实现的跳一跳小游戏。分享给大家供大家参考,具体如下: 以下说的是闲暇编写的一个小游戏--跳一跳,类似于微信的跳一跳,大体实现功能有: 1.先随机生成地图; 2.按住按钮释放后完成动作并进行判断; 首先po一下代码; 代码如下: 代码主要分为用来绘