Pārlūkot izejas kodu

init tank project3

zhangxiaoyu 3 gadi atpakaļ
vecāks
revīzija
5b105b2afb

+ 13 - 3
src/event_/BallMove.java

@@ -6,7 +6,7 @@ import java.awt.event.KeyEvent;
6 6
 import java.awt.event.KeyListener;
7 7
 
8 8
 /**
9
- * 小球移动
9
+ * 小球移动事件
10 10
  */
11 11
 public class BallMove  extends JFrame{
12 12
     public static void main(String[] args) {
@@ -22,6 +22,8 @@ public class BallMove  extends JFrame{
22 22
     }
23 23
 }
24 24
 class MyPanel extends JPanel implements KeyListener {
25
+    int x = 10;
26
+    int y = 10;
25 27
     @Override
26 28
     public void keyTyped(KeyEvent e) {
27 29
         
@@ -30,8 +32,16 @@ class MyPanel extends JPanel implements KeyListener {
30 32
     @Override
31 33
     public void keyPressed(KeyEvent e) {
32 34
         if(e.getKeyCode() == KeyEvent.VK_DOWN) {
33
-            System.out.println("按下了向下的按键");
35
+            // 键盘向下 y+1
36
+            y++;
37
+        } else if(e.getKeyCode() == KeyEvent.VK_UP) {
38
+            y--;
39
+        } else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
40
+            x--;
41
+        } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
42
+            x++;
34 43
         }
44
+        super.repaint();
35 45
     }
36 46
 
37 47
     @Override
@@ -42,6 +52,6 @@ class MyPanel extends JPanel implements KeyListener {
42 52
     @Override
43 53
     public void paint(Graphics g) {
44 54
         super.paint(g);
45
-        g.fillOval(10,10,20,20);
55
+        g.fillOval(x,y,20,20);
46 56
     }
47 57
 }

+ 41 - 5
src/tankGame01/MyPanel.java

@@ -2,9 +2,45 @@ package tankGame01;
2 2
 
3 3
 import javax.swing.*;
4 4
 import java.awt.*;
5
+import java.awt.event.KeyEvent;
6
+import java.awt.event.KeyListener;
5 7
 
6
-public class MyPanel extends JPanel {
8
+public class MyPanel extends JPanel implements KeyListener {
9
+    Tank tank1;
10
+    Tank tank2;
11
+    Tank tank3;
12
+    Tank tank4;
7 13
     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);
18
+    }
19
+
20
+    @Override
21
+    public void keyTyped(KeyEvent e) {
22
+
23
+    }
24
+
25
+    @Override
26
+    public void keyPressed(KeyEvent e) {
27
+        int x = tank1.getX();
28
+        int y = tank1.getY();
29
+        if(e.getKeyCode() == KeyEvent.VK_DOWN) {
30
+            // 键盘向下 y+1
31
+            tank1.setY(y++);
32
+        } else if(e.getKeyCode() == KeyEvent.VK_UP) {
33
+            tank1.setY(y--);
34
+        } else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
35
+            tank1.setX(x--);
36
+        } else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
37
+            tank1.setX(x++);
38
+        }
39
+        this.repaint();
40
+    }
41
+
42
+    @Override
43
+    public void keyReleased(KeyEvent e) {
8 44
 
9 45
     }
10 46
 
@@ -13,9 +49,9 @@ public class MyPanel extends JPanel {
13 49
         super.paint(g);
14 50
         // 填充矩形
15 51
         g.fillRect(0,0,1000,750);
16
-        Tank tank = new Tank(100, 100);
17
-        tank.initTank(g,1,1);
18
-        Tank tank2 = new Tank(200, 100);
19
-        tank2.initTank(g,1,2);
52
+        tank1.initTank(g,1,1);
53
+        tank2.initTank(g,2,1);
54
+        tank3.initTank(g,3,1);
55
+        tank4.initTank(g,4,2);
20 56
     }
21 57
 }

+ 40 - 0
src/tankGame01/Tank.java

@@ -12,6 +12,22 @@ public class Tank {
12 12
         this.y = y;
13 13
     }
14 14
 
15
+    public Integer getX() {
16
+        return x;
17
+    }
18
+
19
+    public Integer getY() {
20
+        return y;
21
+    }
22
+
23
+    public void setX(Integer x) {
24
+        this.x = x;
25
+    }
26
+
27
+    public void setY(Integer y) {
28
+        this.y = y;
29
+    }
30
+
15 31
     /**
16 32
      * 初始化坦克
17 33
      * @param g 画笔
@@ -31,6 +47,30 @@ public class Tank {
31 47
                 g.fillOval(x+10,y+20,20,20);  // 画坦克的盖子
32 48
                 g.drawLine(x+20,y+30,x+20,y);// 画机关枪(中间的线)
33 49
                 break;
50
+            // 向下
51
+            case 2:
52
+                g.fill3DRect(x,y,10,60,false);// 坦克左侧履带
53
+                g.fill3DRect(x+30,y,10,60,false);// 坦克右侧履带
54
+                g.fill3DRect(x+10,y+10,20,40,false); // 中间控制台
55
+                g.fillOval(x+10,y+20,20,20);  // 画坦克的盖子
56
+                g.drawLine(x+20,y+30,x+20,y+60);// 画机关枪(中间的线)
57
+                break;
58
+            // 向左
59
+            case 3:
60
+                g.fill3DRect(x,y,60,10,false);// 坦克上履带履带
61
+                g.fill3DRect(x,y+30,60,10,false);// 坦克下侧履带
62
+                g.fill3DRect(x+10,y+10,40,20,false); // 中间控制台
63
+                g.fillOval(x+20,y+10,20,20);  // 画坦克的盖子
64
+                g.drawLine(x+30,y+20,x,y+20);// 画机关枪(中间的线)
65
+                break;
66
+            // 向右
67
+            case 4:
68
+                g.fill3DRect(x,y,60,10,false);// 坦克上履带履带
69
+                g.fill3DRect(x,y+30,60,10,false);// 坦克下侧履带
70
+                g.fill3DRect(x+10,y+10,40,20,false); // 中间控制台
71
+                g.fillOval(x+20,y+10,20,20);  // 画坦克的盖子
72
+                g.drawLine(x+30,y+20,x+60,y+20);// 画机关枪(中间的线)
73
+                break;
34 74
         }
35 75
 
36 76
     }

+ 1 - 0
src/tankGame01/TankGame01.java

@@ -11,6 +11,7 @@ public class TankGame01 extends JFrame {
11 11
         MyPanel myPanel = new MyPanel();
12 12
         this.add(myPanel);
13 13
         this.setSize(1000,750);
14
+        this.addKeyListener(myPanel);
14 15
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15 16
         this.setVisible(true);
16 17
 //        this.getDefaultCloseOperation()