master
Jian Hu 3 years ago
parent 72ec0ca26b
commit d3c149d8b8

Binary file not shown.

@ -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();
}
}

@ -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;
}
}
}

Loading…
Cancel
Save