您好,登錄后才能下訂單哦!
本文實例講述了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程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。