-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTankOfGame4.java
More file actions
401 lines (336 loc) · 6.7 KB
/
TankOfGame4.java
File metadata and controls
401 lines (336 loc) · 6.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
/*
* 功能:坦克游戏
* 1、画出坦克
* 2、坦克上下左右移动
* 3、发子弹
*
*/
package com.bj.game4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
public class TankOfGame4 extends JFrame {
//定义变量
MyPaneles mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
TankOfGame4 tank=new TankOfGame4();
}
public TankOfGame4(){
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;
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);
//加入坦克
et.add(etk);
}
}
//重写paint
public void paint(Graphics g){
super.paint(g);
drawTank(hero.getX(), hero.getY(), g, hero.direct, 0);
//画出子弹
if(hero.shot!=null&&hero.shot.isLive==true){
g.draw3DRect(hero.shot.x, hero.shot.y, 2, 2,false);
}
for(int i=0;i<et.size();i++){
this.drawTank(et.get(i).getX(), et.get(i).getY(), g, et.get(i).getDirect(), et.get(i).getColor());
}
}
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){
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();
}
//重绘
this.repaint();
}
}
}
//子弹类
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{
public EnemyTank(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
}
//我的坦克
class Hero extends Tank{
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);
break;
case 1:
shot=new Shot(x+30, y+10,this.direct);
break;
case 2:
shot=new Shot(x+10, y+30,this.direct);
break;
case 3:
shot=new Shot(x, y+10,this.direct);
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;
}
}