Java实现的打地鼠小游戏完整示例【附源码下载】

本文实例讲述了Java实现的打地鼠小游戏。分享给大家供大家参考,具体如下:

这里涉及到java线程和GUI的相关知识,一个简单的java小游戏打地鼠,有兴趣的朋友可以优化一下。先来看看运行效果:

Java实现的打地鼠小游戏完整示例【附源码下载】_第1张图片

具体代码:

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 imgKV = new HashMap();
  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 mouses = new ArrayList();
  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程序设计有所帮助。

你可能感兴趣的:(Java实现的打地鼠小游戏完整示例【附源码下载】)