diff --git a/docs/坦克大战笔记.docx b/docs/坦克大战笔记.docx index 3eaca1e..f6fce0e 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 b060e19..16a9162 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) { - new TankFrame(); + public static void main(String[] args) throws InterruptedException { + TankFrame tf = new TankFrame(); } } diff --git a/src/com/demo/tank/coruse1/TankFrame.java b/src/com/demo/tank/coruse1/TankFrame.java index ef885bf..a29bcac 100644 --- a/src/com/demo/tank/coruse1/TankFrame.java +++ b/src/com/demo/tank/coruse1/TankFrame.java @@ -9,6 +9,11 @@ import java.util.Random; public class TankFrame extends Frame { int x = 200, y = 200; + boolean bL = false; + boolean bR = false; + boolean bU = false; + boolean bD = false; + public TankFrame(){ setVisible(true); setSize(800, 600); @@ -32,8 +37,78 @@ public class TankFrame extends Frame { @Override public void keyPressed(KeyEvent e) { - x += 10; + int key = e.getKeyCode(); + switch (key){ + 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; + } + calculateDirection(); repaint(); } + + @Override + public void keyReleased(KeyEvent e) { + int key = e.getKeyCode(); + switch (key){ + 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; + } + } + } + + public void calculateDirection(){ + if(bL){ + x -= 10; + calculateVerticalDirection(); + }else if(bR){ + x += 10; + calculateVerticalDirection(); + }else if(bU){ + y -= 10; + calculateHorizontalDirection(); + }else if(bD){ + y += 10; + calculateHorizontalDirection(); + } + } + + public void calculateVerticalDirection(){ + if(bU){ + y -= 10; + }else if(bD){ + y += 10; + } + } + + public void calculateHorizontalDirection(){ + if(bL){ + x -= 10; + }else if(bR){ + x += 10; + } } }