欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-15 08:55:46
本文实例讲述了java实现的打地鼠小游戏。分享给大家供大家参考,具体如下: 这里涉及到java线程和gui的相关知识,一个简单的java小游戏打地鼠,有兴趣的朋友可以优化...

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

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

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程序设计有所帮助。