|
@@ -4,19 +4,21 @@ import javax.swing.*;
|
4
|
4
|
import java.awt.*;
|
5
|
5
|
import java.awt.event.KeyEvent;
|
6
|
6
|
import java.awt.event.KeyListener;
|
|
7
|
+import java.util.Vector;
|
|
8
|
+
|
|
9
|
+import static tankGame01.Constants.Direct.*;
|
7
|
10
|
|
8
|
11
|
public class MyPanel extends JPanel implements KeyListener {
|
9
|
|
- Tank tank1;
|
10
|
|
- Tank tank2;
|
11
|
|
- Tank tank3;
|
12
|
|
- Tank tank4;
|
|
12
|
+ MyTank myTank;
|
|
13
|
+ Vector<EnemyTank> tanks = new Vector<>();
|
|
14
|
+ int initEnemyTanks = 4;
|
13
|
15
|
public MyPanel() {
|
14
|
|
- tank1 = new Tank(100, 100);
|
15
|
|
- tank2 = new Tank(200, 100);
|
16
|
|
- tank3 = new Tank(300, 100);
|
17
|
|
- tank4 = new Tank(400, 100);
|
|
16
|
+ myTank = new MyTank(100,100,UP);
|
|
17
|
+ myTank.setStep(10);
|
|
18
|
+ for (int i = 0; i < initEnemyTanks; i++) {
|
|
19
|
+ tanks.add(new EnemyTank((100+(100*i)),0,DOWN));
|
|
20
|
+ }
|
18
|
21
|
}
|
19
|
|
-
|
20
|
22
|
@Override
|
21
|
23
|
public void keyTyped(KeyEvent e) {
|
22
|
24
|
|
|
@@ -24,17 +26,19 @@ public class MyPanel extends JPanel implements KeyListener {
|
24
|
26
|
|
25
|
27
|
@Override
|
26
|
28
|
public void keyPressed(KeyEvent e) {
|
27
|
|
- int x = tank1.getX();
|
28
|
|
- int y = tank1.getY();
|
29
|
29
|
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
|
30
|
30
|
// 键盘向下 y+1
|
31
|
|
- tank1.setY(y++);
|
|
31
|
+ myTank.moveDown();
|
|
32
|
+ myTank.setDirect(DOWN);
|
32
|
33
|
} else if(e.getKeyCode() == KeyEvent.VK_UP) {
|
33
|
|
- tank1.setY(y--);
|
|
34
|
+ myTank.moveUp();
|
|
35
|
+ myTank.setDirect(UP);
|
34
|
36
|
} else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
|
35
|
|
- tank1.setX(x--);
|
|
37
|
+ myTank.moveLeft();
|
|
38
|
+ myTank.setDirect(LEFT);
|
36
|
39
|
} else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
|
37
|
|
- tank1.setX(x++);
|
|
40
|
+ myTank.moveRight();
|
|
41
|
+ myTank.setDirect(RIGHT);
|
38
|
42
|
}
|
39
|
43
|
this.repaint();
|
40
|
44
|
}
|
|
@@ -48,10 +52,57 @@ public class MyPanel extends JPanel implements KeyListener {
|
48
|
52
|
public void paint(Graphics g) {
|
49
|
53
|
super.paint(g);
|
50
|
54
|
// 填充矩形
|
51
|
|
- g.fillRect(0,0,1000,750);
|
52
|
|
- tank1.initTank(g,1,1);
|
53
|
|
- tank2.initTank(g,2,1);
|
54
|
|
- tank3.initTank(g,3,1);
|
55
|
|
- tank4.initTank(g,4,2);
|
|
55
|
+ g.fillRect(0,0, Constants.Frame.FRAME_WIDTH,Constants.Frame.FRAME_HEIGHT);
|
|
56
|
+ // 画出我方坦克
|
|
57
|
+ drawTank(g,myTank);
|
|
58
|
+ // 画出敌方坦克
|
|
59
|
+ for (EnemyTank enemyTank : tanks) {
|
|
60
|
+ drawTank(g,enemyTank);
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ private void drawTank (Graphics g,Tank tank) {
|
|
65
|
+ Color color = tank.getCamp() == Constants.Camp.MY ? Color.CYAN:Color.ORANGE;
|
|
66
|
+ // 画坦克
|
|
67
|
+ g.setColor(color);
|
|
68
|
+ int x = tank.getX();
|
|
69
|
+ int y = tank.getY();
|
|
70
|
+ switch (tank.getDirect()) {
|
|
71
|
+ // 向上
|
|
72
|
+ case 1:
|
|
73
|
+ g.fill3DRect(x,y,10,60,false);// 坦克左侧履带
|
|
74
|
+ g.fill3DRect(x+30,y,10,60,false);// 坦克右侧履带
|
|
75
|
+ g.fill3DRect(x+10,y+10,20,40,false); // 中间控制台
|
|
76
|
+ g.fillOval(x+10,y+20,20,20); // 画坦克的盖子
|
|
77
|
+ g.drawLine(x+20,y+30,x+20,y);// 画机关枪(中间的线)
|
|
78
|
+ break;
|
|
79
|
+ // 向下
|
|
80
|
+ case 2:
|
|
81
|
+ g.fill3DRect(x,y,10,60,false);// 坦克左侧履带
|
|
82
|
+ g.fill3DRect(x+30,y,10,60,false);// 坦克右侧履带
|
|
83
|
+ g.fill3DRect(x+10,y+10,20,40,false); // 中间控制台
|
|
84
|
+ g.fillOval(x+10,y+20,20,20); // 画坦克的盖子
|
|
85
|
+ g.drawLine(x+20,y+30,x+20,y+60);// 画机关枪(中间的线)
|
|
86
|
+ break;
|
|
87
|
+ // 向左
|
|
88
|
+ case 3:
|
|
89
|
+ g.fill3DRect(x,y,60,10,false);// 坦克上履带履带
|
|
90
|
+ g.fill3DRect(x,y+30,60,10,false);// 坦克下侧履带
|
|
91
|
+ g.fill3DRect(x+10,y+10,40,20,false); // 中间控制台
|
|
92
|
+ g.fillOval(x+20,y+10,20,20); // 画坦克的盖子
|
|
93
|
+ g.drawLine(x+30,y+20,x,y+20);// 画机关枪(中间的线)
|
|
94
|
+ break;
|
|
95
|
+ // 向右
|
|
96
|
+ case 4:
|
|
97
|
+ g.fill3DRect(x,y,60,10,false);// 坦克上履带履带
|
|
98
|
+ g.fill3DRect(x,y+30,60,10,false);// 坦克下侧履带
|
|
99
|
+ g.fill3DRect(x+10,y+10,40,20,false); // 中间控制台
|
|
100
|
+ g.fillOval(x+20,y+10,20,20); // 画坦克的盖子
|
|
101
|
+ g.drawLine(x+30,y+20,x+60,y+20);// 画机关枪(中间的线)
|
|
102
|
+ break;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+
|
56
|
106
|
}
|
|
107
|
+
|
57
|
108
|
}
|