服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java版坦克大战游戏源码示例

Java版坦克大战游戏源码示例

2021-01-22 11:02CSDN邓帅 Java教程

本篇文章主要介绍了Java版坦克大战游戏源码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

整理文档,搜刮出一个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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package tankwar;
 
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.event.keyevent;
import java.awt.event.keylistener;
import java.io.file;
import java.io.fileinputstream;
import java.util.arraylist;
import java.util.random;
 
import javax.swing.borderfactory;
import javax.swing.imageicon;
import javax.swing.jframe;
import javax.swing.jpanel;
 
import tankwar.tank.direction;
import barrier.enemyborn;
import barrier.gold;
import barrier.home;
import barrier.iron;
import barrier.selfborn;
import barrier.wall;
/**
 * java学习交流qq群:589809992 我们一起学java!
 */
public class tankwar implements keylistener {
  static boolean timeout = false;
  private jframe f;
  private jpanel gamepanel;
  private panelshow messgepanel;
  private mypanel p;
  private tank mytank;
  public static final int area_width = 830;
  public static final int area_height = 800;
  private arraylist<missle> missles = new arraylist<missle>();
  private arraylist<tank> alltanks = new arraylist<tank>();
  private arraylist<boom> booms = new arraylist<boom>();
  private arraylist<wall> walls = new arraylist<wall>();
  private arraylist<iron> irons = new arraylist<iron>();
  private arraylist<gold> golds = new arraylist<gold>();
  private arraylist<item> items = new arraylist<item>();
  private arraylist<enemyborn> enemyborns = new arraylist<enemyborn>();
  private selfborn selfborn;
  private home home;
  private tank enemytank;
  private random r;
  private imageicon background;
  private final string map;
  private int tankmax;
  private boolean over = false;
  private static int selfmax = 3;
  private boolean win;
  private boolean flash = false;
  private tankwar tw = this;
  static int score = 0;
 
  private final jframe mainf;
  private int style;
 
  public tankwar(string map, int tankmax, jframe mainf, int style)
      throws exception {
    this.map = map;
    this.tankmax = tankmax;
    this.mainf = mainf;
    this.style = style;
    init();
  }
 
  private void init() {
    f = new jframe("坦克大战 v3.0");
    gamepanel = new jpanel(null);
    p = new mypanel();
    p.setbackground(color.white);
    r = new random();
    messgepanel = new panelshow();
    initmap(new file("map/" + map));
 
    try {
      mytank = new tank(selfborn.getx(), selfborn.gety(), true, alltanks,
          walls, irons, golds, missles, home, booms, style);
    } catch (exception e1) {
    }
    mytank.setdir(direction.u);
 
    alltanks.add(mytank);
    addtank();
    try {
      background = new imageicon(
          tankwar.class.getresource("/pic/whiteback.jpg"));
    } catch (exception e) {
    }
 
    p.setborder(borderfactory.createetchedborder(color.black, color.white));
    p.setsize(area_width, area_height);
    messgepanel.setbounds(area_width, 0, 200, area_height);
    gamepanel.add(messgepanel);
    gamepanel.add(p);
    f.add(gamepanel);
    f.setbounds(0, 0, area_width + 200, area_height);
    f.setdefaultcloseoperation(3);
    f.setresizable(true);
    f.setfocusable(true);
    f.addkeylistener(this);
    f.setvisible(true);
 
    new thread(new runnable() {
      public void run() {
        while (!over) {
          if (!mytank.islive()) {
            selfmax--;
            if (selfmax < 0) {
              f.removekeylistener(tw);
              over = true;
              win = false;
              break;
            } else {
              mytank.setlevel(1);
              mytank.setx(selfborn.getx());
              mytank.sety(selfborn.gety());
              mytank.setdir(direction.u);
              mytank.sethp(50);
              mytank.setlive(true);
            }
          }
          if (tankmax <= 0 && alltanks.size() == 1) {
            f.removekeylistener(tw);
            over = true;
            win = true;
          }
          if (!home.islive()) {
            f.removekeylistener(tw);
            over = true;
            win = false;
          }
          p.repaint();
          mytank.move();
          for (int i = 1; i < alltanks.size(); i++) {
            alltanks.get(i).move();
            alltanks.get(i).setnofire(mytank.getnofire() + 1);
            // if(alltanks.get(i).getx()%5==0&&alltanks.get(i).gety()%5==0)
            ai(alltanks.get(i));
          }
          if (alltanks.size() <= enemyborns.size() + 1)
            addtank();
          mytank.setnofire(mytank.getnofire() + 1);
          messgepanel.setenemycount(tankmax);
          messgepanel.setselfcount(selfmax);
          messgepanel.setscore(score);
          if (score % 500 == 0) {
            score += 100;
            item item = new item(alltanks, booms, irons, home);
            items.add(item);
            item.start();
          }
          try {
            thread.sleep(30);
          } catch (interruptedexception e) {
          }
        }
 
        over();
      }
 
    }).start();
 
  }
 
  private class mypanel extends jpanel {
    /**
     *
     */
    private static final long serialversionuid = 4408440723797225328l;
 
    @override
    public void paintcomponent(graphics g) {
      super.paintcomponent(g);
      g.drawimage(background.getimage(), 0, 0, null);
      for (int j = 0; j < walls.size(); j++) {
        walls.get(j).draw(g);
      }
      for (int j = 0; j < irons.size(); j++) {
        irons.get(j).draw(g);
      }
      for (int j = 0; j < golds.size(); j++) {
        golds.get(j).draw(g);
      }
      for (int j = 0; j < enemyborns.size(); j++) {
        enemyborns.get(j).draw(g);
      }
      home.draw(g);
      selfborn.draw(g);
 
      for (int j = 0; j < alltanks.size(); j++) {
        alltanks.get(j).drawtank(g);
      }
      for (int j = 0; j < irons.size(); j++) {
        irons.get(j).draw(g);
      }
 
      for (int i = 0; i < missles.size(); i++) {
        missles.get(i).drawmissle(g);
        if (!missles.get(i).islive())
          missles.remove(i);
      }
      for (int i = 0; i < booms.size(); i++) {
        if (booms.get(i).islive())
          booms.get(i).drawboom(g);
        else
          booms.remove(i);
      }
      for (int j = 0; j < items.size(); j++) {
        if (!items.get(j).islive()) {
          items.remove(j);
          continue;
        }
        items.get(j).draw(g);
      }
      if (over)
        drawover(g);
      messgepanel.repaint();
    }
  }
 
  @override
  public void keytyped(keyevent e) {
    // todo auto-generated method stub
 
  }
 
  @override
  public void keypressed(keyevent e) {
    if (over) {
      if (e.getkeycode() == keyevent.vk_f1) {
        over = false;
        missles.clear();
        alltanks.clear();
        booms.clear();
        walls.clear();
        irons.clear();
        golds.clear();
        enemyborns.clear();
        try {
          init();
        } catch (exception e1) {
        }
      } else {
        f.setvisible(false);
        mainf.setsize(800, 800);
        mainf.setvisible(true);
      }
    } else if (e.getkeycode() == keyevent.vk_space) {
      mytank.fire();
    } else {
      mytank.keypress(e);
    }
  }
 
  @override
  public void keyreleased(keyevent e) {
 
    mytank.keyreleased(e);
  }
 
  public void ai(tank tank) {
    if (timeout) {
      tank.setup(false);
      tank.setleft(false);
      tank.setdown(false);
      tank.setright(false);
      return;
    }
 
    if (r.nextint(40) == 0)
      tank.fire();
    if (r.nextint(10) == 0) {
      if (tank.getx() >= mytank.getx()
          && tank.getx() <= mytank.getx() + tank.size
          && tank.gety() > mytank.gety()) {
        tank.setup(true);
        tank.setleft(false);
        tank.setdown(false);
        tank.setright(false);
        tank.setdir(direction.u);
        return;
      } else if (tank.getx() >= mytank.getx()
          && tank.getx() <= mytank.getx() + tank.size
          && tank.gety() < mytank.gety()) {
        tank.setup(false);
        tank.setleft(false);
        tank.setdown(true);
        tank.setright(false);
        tank.setdir(direction.d);
        return;
      }
 
      else if (tank.getx() > mytank.getx()
          && tank.gety() >= mytank.gety()
          && tank.gety() <= mytank.gety() + tank.size) {
        tank.setup(false);
        tank.setleft(true);
        tank.setdown(false);
        tank.setright(false);
        tank.setdir(direction.l);
        return;
      } else if (tank.getx() < mytank.getx()
          && tank.gety() >= mytank.gety()
          && tank.gety() <= mytank.gety() + tank.size) {
        tank.setup(false);
        tank.setleft(false);
        tank.setdown(false);
        tank.setright(true);
        tank.setdir(direction.r);
        return;
      }
    }
    if (tank.getx() <= 0) {
      tank.setup(false);
      tank.setleft(false);
      tank.setdown(false);
      tank.setright(true);
      tank.setdir(direction.r);
    }
    if (tank.gety() <= 0) {
      tank.setup(false);
      tank.setleft(false);
      tank.setdown(true);
      tank.setright(false);
      tank.setdir(direction.d);
    }
    if (tank.getx() >= area_width - tank.size) {
      tank.setup(false);
      tank.setleft(true);
      tank.setdown(false);
      tank.setright(false);
      tank.setdir(direction.l);
    }
    if (tank.gety() >= area_height - tank.size) {
      tank.setup(true);
      tank.setleft(false);
      tank.setdown(false);
      tank.setright(false);
      tank.setdir(direction.u);
    } else if (r.nextint(300) == 1) {
      tank.setup(true);
      tank.setleft(false);
      tank.setdown(false);
      tank.setright(false);
      tank.setdir(direction.u);
    }
 
    else if (r.nextint(300) == 2) {
      tank.setup(false);
      tank.setleft(true);
      tank.setdown(false);
      tank.setright(false);
      tank.setdir(direction.l);
    } else if (r.nextint(300) == 3) {
      tank.setup(false);
      tank.setleft(false);
      tank.setdown(true);
      tank.setright(false);
      tank.setdir(direction.d);
    } else if (r.nextint(300) == 4) {
      tank.setup(false);
      tank.setleft(false);
      tank.setdown(false);
      tank.setright(true);
      tank.setdir(direction.r);
    }
  }
 
  public void initmap(file file) {
    try {
      fileinputstream read = new fileinputstream(file);
      for (int i = 0; i < area_height / 50; i++) {
        for (int j = 0; j < area_width / 50; j++) {
          switch (read.read()) {
          case 1:
            walls.add(new wall(j * 50, i * 50));
            break;
          case 2:
            irons.add(new iron(j * 50, i * 50));
            break;
          case 3:
            golds.add(new gold(j * 50, i * 50));
            break;
          case 4:
            selfborn = new selfborn(j * 50, i * 50);
            break;
          case 5:
            enemyborns.add(new enemyborn(j * 50, i * 50));
            break;
          case 6:
            home = new home(j * 50, i * 50);
            break;
          }
        }
      }
      read.close();
    } catch (exception e) {
    }
    ;
  }
 
  public void addtank() {
    if (tankmax <= 0)
      return;
    for (int i = alltanks.size(); i < enemyborns.size() + 1; i++) {
      try {
        int temp = r.nextint(enemyborns.size());
        enemytank = new tank(enemyborns.get(temp).getx(), enemyborns
            .get(temp).gety(), false, alltanks, walls, irons,
            golds, missles, home, booms, r.nextint(3) + 1);
      } catch (exception e) {
        e.printstacktrace();
      }
      enemytank.setdir(direction.d);
      enemytank.setdown(true);
      alltanks.add(enemytank);
      tankmax--;
      if (tankmax <= 0)
        return;
    }
  }
 
  public static void addmytanklives() {
    selfmax++;
  }
 
  private void over() {
 
    for (int i = 0; i < area_height / 50; i++) {
      for (int j = 0; j < area_width / 50; j++) {
        irons.add(new iron(j * 50, i * 50));
        p.repaint();
        try {
          thread.sleep(5);
        } catch (interruptedexception e) {
        }
      }
    }
    while (true) {
      flash = !flash;
      p.repaint();
      try {
        thread.sleep(1000);
      } catch (interruptedexception e) {
      }
      f.addkeylistener(this);
    }
  }
 
  private void drawover(graphics g) {
    p.repaint();
    g.setcolor(color.red);
    g.setfont(new font("arial", 1, 100));
    g.drawstring("game over", 100, 200);
    g.setfont(new font("arial", 2, 50));
    if (win)
      g.drawstring("congratulation! you win!", 100, 400);
    else
      g.drawstring("so sorry, you lose!", 100, 400);
    if (flash) {
      g.setfont(new font("arial", 2, 30));
      g.setcolor(color.black);
      g.drawstring("press f1 to try again...,", 150, 500);
      g.drawstring("press the other key to return the title...,", 150,
          600);
    }
  }
}

代码运行效果截图

Java版坦克大战游戏源码示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://geek.csdn.net/news/detail/240549

延伸 · 阅读

精彩推荐
  • Java教程二进制中1的个数

    二进制中1的个数

    这篇文章介绍了二进制中1的个数,有需要的朋友可以参考一下 ...

    java之家2662019-10-15
  • Java教程Java开发常见异常及解决办法详解

    Java开发常见异常及解决办法详解

    这篇文章主要介绍了java程序常见异常及处理汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考...

    cutercorley12252021-12-18
  • Java教程mybatis批量新增、删除、查询和修改方式

    mybatis批量新增、删除、查询和修改方式

    这篇文章主要介绍了mybatis批量新增、删除、查询和修改方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    xuforeverlove7492022-01-24
  • Java教程Spring 6.0 将停止支持 Freemarker 和 JSP

    Spring 6.0 将停止支持 Freemarker 和 JSP

    Spring Framework 6.0 第一个里程碑版本已经发布,目前已经可以从Spring Repo获取。这里有一些新变更我们可以提前了解一下。...

    码农小胖哥12642021-12-31
  • Java教程mybatis调用存储过程的实例代码

    mybatis调用存储过程的实例代码

    这篇文章主要介绍了mybatis调用存储过程的实例,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    动力节点11732021-01-25
  • Java教程浅谈java 中equals和==的区别

    浅谈java 中equals和==的区别

    这篇文章主要介绍了java 中equals和==的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小...

    独特润许多人5982021-07-21
  • Java教程Spring Cloud Gateway 如何修改HTTP响应信息

    Spring Cloud Gateway 如何修改HTTP响应信息

    这篇文章主要介绍了Spring Cloud Gateway 修改HTTP响应信息的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    帷幄庸者13712021-10-13
  • Java教程浅谈sql_@SelectProvider及使用注意说明

    浅谈sql_@SelectProvider及使用注意说明

    这篇文章主要介绍了sql_@SelectProvider及使用注意说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    icecoola_6892021-11-04