diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index f6fce0e..3334933 100644 Binary files a/docs/坦克大战笔记.docx and b/docs/坦克大战笔记.docx differ diff --git a/src/com/demo/tank/coruse1/Main.java b/src/com/demo/tank/coruse1/Main.java index 16a9162..00c3690 100644 --- a/src/com/demo/tank/coruse1/Main.java +++ b/src/com/demo/tank/coruse1/Main.java @@ -1,7 +1,7 @@ package com.demo.tank.coruse1; public class Main { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args){ TankFrame tf = new TankFrame(); } } diff --git a/src/com/demo/tank/coruse2/Direction.java b/src/com/demo/tank/coruse2/Direction.java new file mode 100644 index 0000000..a8a2526 --- /dev/null +++ b/src/com/demo/tank/coruse2/Direction.java @@ -0,0 +1,5 @@ +package com.demo.tank.coruse2; + +public enum Direction { + LEFT, RIGHT, UP, DOWN; +} diff --git a/src/com/demo/tank/coruse2/MainV2.java b/src/com/demo/tank/coruse2/MainV2.java new file mode 100644 index 0000000..2b7ceb6 --- /dev/null +++ b/src/com/demo/tank/coruse2/MainV2.java @@ -0,0 +1,10 @@ +package com.demo.tank.coruse2; + +import com.demo.tank.coruse1.TankFrame; + +public class MainV2 { + public static void main(String[] args){ + TankFrame tf = new TankFrame(); + tf.repaint(); + } +} diff --git a/src/com/demo/tank/coruse2/Tank.java b/src/com/demo/tank/coruse2/Tank.java new file mode 100644 index 0000000..f0ff6a0 --- /dev/null +++ b/src/com/demo/tank/coruse2/Tank.java @@ -0,0 +1,56 @@ +package com.demo.tank.coruse2; + +import java.awt.*; + +public class Tank { + private int x,y; + private Direction dir; + private static final int SPEED = 10; + + + public Tank(int x, int y, Direction dir) { + this.x = x; + this.y = y; + this.dir = dir; + } + + public void paint(Graphics g) { + g.fillRect(x, y,50,50); + switch (dir){ + case UP: y -= SPEED; + break; + case DOWN: y += SPEED; + break; + case LEFT: x -= SPEED; + break; + case RIGHT: x += SPEED; + break; + default: + break; + } + } + + 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 Direction getDir() { + return dir; + } + + public void setDir(Direction dir) { + this.dir = dir; + } +} diff --git a/src/com/demo/tank/coruse2/TankFrame.java b/src/com/demo/tank/coruse2/TankFrame.java new file mode 100644 index 0000000..bbb9351 --- /dev/null +++ b/src/com/demo/tank/coruse2/TankFrame.java @@ -0,0 +1,86 @@ +package com.demo.tank.coruse2; + +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +public class TankFrame extends Frame { + Tank tank = new Tank(500, 500, Direction.UP); + + public TankFrame(){ + setVisible(true); + setSize(800, 600); + setResizable(false); + setTitle("tank war"); + this.addKeyListener(new MyKeyListener()); + this.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + System.exit(0); + } + }); + } + + @Override + public void paint(Graphics g){ + tank.paint(g); + } + + class MyKeyListener extends KeyAdapter{ + boolean bL = false; + boolean bR = false; + boolean bU = false; + boolean bD = false; + + @Override + public void keyPressed(KeyEvent e) { + switch (e.getKeyCode()){ + case KeyEvent.VK_A: + bL = true; + break; + case KeyEvent.VK_D: + bR = true; + break; + case KeyEvent.VK_W: + bU = true; + break; + case KeyEvent.VK_S: + bD = true; + break; + default: + break; + } + setTankDirection(); + } + + @Override + public void keyReleased(KeyEvent e) { + switch (e.getKeyCode()){ + case KeyEvent.VK_A: + bL = false; + break; + case KeyEvent.VK_D: + bR = false; + break; + case KeyEvent.VK_W: + bU = false; + break; + case KeyEvent.VK_S: + bD = false; + break; + default: + break; + } + setTankDirection(); + } + + public void setTankDirection(){ + if(bL) tank.setDir(Direction.LEFT); + if(bR) tank.setDir(Direction.RIGHT); + if(bU) tank.setDir(Direction.UP); + if(bD) tank.setDir(Direction.DOWN); + } + } +}