服务器之家

服务器之家 > 正文

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

时间:2021-05-12 15:13     来源/作者:竹有节

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

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

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

具体代码:

mouse.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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程序设计有所帮助。

原文链接:https://download.csdn.net/download/u014307823/9160981

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部