|
|
@ -16,6 +16,8 @@ public class TankFrame extends Frame {
|
|
|
|
|
|
|
|
|
|
|
|
private int x = 200;
|
|
|
|
private int x = 200;
|
|
|
|
private int y = 200;
|
|
|
|
private int y = 200;
|
|
|
|
|
|
|
|
private DirEnum dir = DirEnum.RIGHT;
|
|
|
|
|
|
|
|
private static final int SPEED = 10;
|
|
|
|
|
|
|
|
|
|
|
|
public TankFrame() {
|
|
|
|
public TankFrame() {
|
|
|
|
|
|
|
|
|
|
|
@ -40,7 +42,14 @@ public class TankFrame extends Frame {
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
public void paint(Graphics g) {
|
|
|
|
super.paint(g);
|
|
|
|
super.paint(g);
|
|
|
|
g.fillRect(x, y, 50, 50);
|
|
|
|
g.fillRect(x, y, 50, 50);
|
|
|
|
x += 10;
|
|
|
|
|
|
|
|
|
|
|
|
switch (dir) {
|
|
|
|
|
|
|
|
case UP: y -= SPEED; break;
|
|
|
|
|
|
|
|
case DOWN: y += SPEED; break;
|
|
|
|
|
|
|
|
case LEFT: x -= SPEED; break;
|
|
|
|
|
|
|
|
case RIGHT: x += SPEED; break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//这里使用内部内的原因是,只有TankFrame用到
|
|
|
|
//这里使用内部内的原因是,只有TankFrame用到
|
|
|
@ -54,51 +63,35 @@ public class TankFrame extends Frame {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
super.keyPressed(e);
|
|
|
|
super.keyPressed(e);
|
|
|
|
/*x += 10;
|
|
|
|
|
|
|
|
repaint();*/
|
|
|
|
|
|
|
|
switch (e.getKeyCode()) {
|
|
|
|
switch (e.getKeyCode()) {
|
|
|
|
case KeyEvent.VK_LEFT : {
|
|
|
|
case KeyEvent.VK_LEFT : bL = true; break;
|
|
|
|
bL = true;
|
|
|
|
case KeyEvent.VK_RIGHT : bR = true; break;
|
|
|
|
break;
|
|
|
|
case KeyEvent.VK_UP : bU = true; break;
|
|
|
|
}
|
|
|
|
case KeyEvent.VK_DOWN : bD = true; break;
|
|
|
|
case KeyEvent.VK_RIGHT : {
|
|
|
|
|
|
|
|
bR = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case KeyEvent.VK_UP : {
|
|
|
|
|
|
|
|
bU = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case KeyEvent.VK_DOWN : {
|
|
|
|
|
|
|
|
bD = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setDir();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
public void keyReleased(KeyEvent e) {
|
|
|
|
super.keyReleased(e);
|
|
|
|
super.keyReleased(e);
|
|
|
|
switch (e.getKeyCode()) {
|
|
|
|
switch (e.getKeyCode()) {
|
|
|
|
case KeyEvent.VK_LEFT : {
|
|
|
|
case KeyEvent.VK_LEFT : bL = false; break;
|
|
|
|
bL = false;
|
|
|
|
case KeyEvent.VK_RIGHT : bR = false; break;
|
|
|
|
break;
|
|
|
|
case KeyEvent.VK_UP : bU = false; break;
|
|
|
|
}
|
|
|
|
case KeyEvent.VK_DOWN : bD = false; break;
|
|
|
|
case KeyEvent.VK_RIGHT : {
|
|
|
|
|
|
|
|
bR = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case KeyEvent.VK_UP : {
|
|
|
|
|
|
|
|
bU = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case KeyEvent.VK_DOWN : {
|
|
|
|
|
|
|
|
bD = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setDir(); //通过做了实验,现阶段就算不用这个方法,也不影响,但是按键抬起,坦克应该停止,如果还按照原来的方向移动就不对了
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setDir() {
|
|
|
|
|
|
|
|
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
|
|
|
|
|
|
|
|
if(bL) dir = DirEnum.LEFT;
|
|
|
|
|
|
|
|
if(bR) dir = DirEnum.RIGHT;
|
|
|
|
|
|
|
|
if(bU) dir = DirEnum.UP;
|
|
|
|
|
|
|
|
if(bD) dir = DirEnum.DOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|