-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTankOfGame5.java
More file actions
625 lines (526 loc) · 10.7 KB
/
TankOfGame5.java
File metadata and controls
625 lines (526 loc) · 10.7 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* 功能:坦克游戏
* 1、画出坦克
* 2、hero坦克上下左右移动
* 3、hero发子弹
* 4、敌人坦克,炸弹爆炸
*
*/
package com.bj.game5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
public class TankOfGame5 extends JFrame {
//定义变量
MyPaneles mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
TankOfGame5 tank=new TankOfGame5();
}
public TankOfGame5(){
mp=new MyPaneles();
this.add(mp);
this.addKeyListener(mp);
//创建子弹线程重绘
Thread t=new Thread(mp);
t.start();
this.setSize(400,400);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//我的面板
class MyPaneles extends JPanel implements KeyListener,Runnable{
//定义一个我的坦克
Hero hero=null;
//定义敌人坦克组
Vector<EnemyTank> et=new Vector<EnemyTank>();
int enSize=3;
//定义炸弹组
Vector<Bomb> vb=new Vector<Bomb>();
//定义三张图片
Image image1=null;
Image image2=null;
Image image3=null;
public MyPaneles(){
hero=new Hero(100,100);
//初始化敌人坦克
for(int i=0;i<enSize;i++){
//创建一辆坦克
EnemyTank etk=new EnemyTank((i+1)*50, 0);
etk.setColor(1);
etk.setDirect(2);
Thread thread=new Thread(etk);
thread.start();
//加入坦克
et.add(etk);
}
//初始化图片
image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
}
//重写paint
public void paint(Graphics g){
super.paint(g);
drawTank(hero.getX(), hero.getY(), g, hero.direct, 0);
//从vs中取出每颗子弹
for(int i=0;i<hero.vs.size();i++){
Shot myShot=hero.vs.get(i);
//画出每一个子弹
if(myShot!=null&&myShot.isLive==true){
g.draw3DRect(myShot.x, myShot.y, 2, 2,false);
}
if(myShot.isLive==false){
//从vs中删除该子弹
hero.vs.remove(myShot);
}
}
//炸弹效果绘画
for(int i=0;i<vb.size();i++){
System.out.println("Bomb size"+vb.size());
Bomb b=vb.get(i);
if(b.life>6){
//g.drawImage(image1, b.x, b.y,30,30,this);
g.drawImage(image1, b.x, b.y,30,30,this);
}else if(b.life>3){
g.drawImage(image2, b.x, b.y,30,30,this);
}else{
g.drawImage(image3, b.x, b.y,30,30,this);
}
//减少生命值
b.BombDown();
//从向量Vector<Bomb>中去除生命死亡炸弹
if(b.isLive==false){
vb.remove(b);
}
}
for(int i=0;i<et.size();i++){
EnemyTank e=et.get(i);
//检查每一辆敌人坦克是否活着
if(e.isLive==true){
this.drawTank(e.getX(), e.getY(), g, e.getDirect(), e.getColor());
}
}
}
//判断坦克是否被子弹击中
public void hitTank(Shot s,EnemyTank et){
//判断坦克的方向
switch(et.direct){
//敌人坦克上下方向
case 0:
case 2:
if(s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30){
//击中
//子弹死亡
s.isLive=false;
//敌人坦克死亡
et.isLive=false;
//产生炸弹对象
Bomb b=new Bomb(et.x,et.y);
//加入到向量中
vb.add(b);
}
break;
case 1:
case 3:
if(s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20){
//击中
//击中
//子弹死亡
s.isLive=false;
//敌人坦克死亡
et.isLive=false;
//产生炸弹对象
Bomb b=new Bomb(et.x,et.y);
//加入到向量中
vb.add(b);
}
break;
}
}
public void drawTank(int x,int y,Graphics g,int direction,int type){
//
switch(type){
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.yellow);
break;
default:
break;
}
//判断方向
switch(direction){
//up
case 0:
//g.setColor(Color.CYAN);
//画出我的坦克,(到时候再封装成一个函数)
//1、画出左边矩形
g.fill3DRect(x, y, 5, 30,false);
//2、画出右面矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3、中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4、画出圆形
g.fillOval(x+5, y+10, 10, 10);
//5、画线条
g.drawLine(x+10, y-5, x+10, y+10);
break;
case 1:
//System.out.println("->");
//画出我的坦克,(到时候再封装成一个函数)
//1、画出左边矩形
g.fill3DRect(x, y, 30, 5,false);
//2、画出右面矩形
g.fill3DRect(x, y+15, 30, 5,false);
//3、中间矩形
g.fill3DRect(x+5, y+5, 20, 10,false);
//4、画出圆形
g.fillOval(x+10, y+5, 10, 10);
//5、画线条
g.drawLine(x+20, y+10, x+35, y+10);
break;
case 2:
//g.setColor(Color.CYAN);
//画出我的坦克,(到时候再封装成一个函数)
//1、画出左边矩形
g.fill3DRect(x, y, 5, 30,false);
//2、画出右面矩形
g.fill3DRect(x+15, y, 5, 30,false);
//3、中间矩形
g.fill3DRect(x+5, y+5, 10, 20,false);
//4、画出圆形
g.fillOval(x+5, y+10, 10, 10);
//5、画线条
g.drawLine(x+10, y+20, x+10, y+35);
break;
case 3:
//画出我的坦克,(到时候再封装成一个函数)
//1、画出左边矩形
g.fill3DRect(x, y, 30, 5,false);
//2、画出右面矩形
g.fill3DRect(x, y+15, 30, 5,false);
//3、中间矩形
g.fill3DRect(x+5, y+5, 20, 10,false);
//4、画出圆形
g.fillOval(x+10, y+5, 10, 10);
//5、画线条
g.drawLine(x+10, y+10, x-5, y+10);
break;
}
}
//
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
//按键按下W(左)、S(下)、A(上)、D(右)
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W){
//设置我的坦克方向
//向上
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D){
//向右
this.hero.setDirect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S){
//向下
this.hero.setDirect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A){
//向左
this.hero.setDirect(3);
this.hero.moveLeft();
}
if(e.getKeyCode()==KeyEvent.VK_J){
//判断发射子弹个数小于5
if(this.hero.vs.size()<=4){
this.hero.shotEnemy();
}
}
//重新绘制
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// 重绘子弹
//每隔200毫秒
while(true){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//不停检测子弹和敌人坦克是否碰撞,
//以及相应子弹和敌人坦克消失处理
for(int i=0;i<hero.vs.size();i++){
//取出每一颗子弹
Shot s=hero.vs.get(i);
//每一颗子弹匹配所有敌人坦克
for(int j=0;j<et.size();j++){
if(et.get(j).isLive==true){
hitTank(s, et.get(j));
}
}
}
//重绘
this.repaint();
}
}
}
//炸弹类
class Bomb{
int x=0;
int y=0;
//生命值
int life=9;
boolean isLive=true;
public Bomb(int x,int y){
this.x=x;
this.y=y;
}
//生命值减少
public void BombDown(){
if(this.life>0){
this.life--;
}else{
this.isLive=false;
}
}
}
//子弹类
class Shot implements Runnable{
int x=0;
int y=0;
int direct;
int speed=10;
//是否活着
boolean isLive=true;
public Shot(int x,int y,int direct){
this.x=x;
this.y=y;
this.direct=direct;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(direct){
//向上
case 0:
y-=speed;
break;
//向右
case 1:
x+=speed;
break;
//向下
case 2:
y+=speed;
break;
//向左
case 3:
x-=speed;
break;
}
//子弹死亡,判断子弹是否碰到边界
if(x<0||x>400||y<0||y>400){
this.isLive=false;
break;
}
System.out.println("子弹发射 坐标 x="+x+" Y="+y);
}
}
}
//坦克类
class Tank{
//坦克的横坐标
int x=0;
//坦克纵坐标
int y=0;
//坦克方向
//0表示上,1表示右,2表示下,3表示左
int direct=0;
//坦克速度
int speed=1;
//颜色
int color=0;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Tank(int x,int y){
this.x=x;
this.y=y;
}
}
//敌人坦克
class EnemyTank extends Tank implements Runnable{
boolean isLive=true;
Vector<Shot> vs=new Vector<Shot>();
Shot shot=null;
public EnemyTank(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
switch(this.direct){
//向上
case 0:
for(int i=0;i<30;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(y>0){
y-=speed;
}
}
break;
//向右
case 1:
for(int i=0;i<30;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(x<400){
x+=speed;
}
}
break;
//向下
case 2:
for(int i=0;i<30;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(y<400){
y+=speed;
}
}
break;
//向左
case 3:
for(int i=0;i<30;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(x>0){
x-=speed;
}
}
break;
}
//随机改变方向
this.direct=(int)(Math.random()*4);
}
}
}
//我的坦克
class Hero extends Tank{
//Shot shot=null;
Vector<Shot> vs=new Vector<Shot>();
Shot shot=null;
public Hero(int x,int y){
super(x, y);
}
public void shotEnemy(){
switch(this.direct){
case 0:
shot=new Shot(x+10, y,this.direct);
vs.add(shot);
break;
case 1:
shot=new Shot(x+30, y+10,this.direct);
vs.add(shot);
break;
case 2:
shot=new Shot(x+10, y+30,this.direct);
vs.add(shot);
break;
case 3:
shot=new Shot(x, y+10,this.direct);
vs.add(shot);
break;
}
//创建线程
Thread thread=new Thread(shot);
thread.start();
}
//坦克向上移动
public void moveUp(){
this.y-=speed;
}
//坦克向右移动
public void moveRight(){
this.x+=speed;
}
//坦克向下移动
public void moveDown(){
this.y+=speed;
}
//坦克向左移动
public void moveLeft(){
this.x-=speed;
}
}