import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.lang.invoke.SwitchPoint;
/**
* @author 许泽鑫
* @data 2019/11/10 - 11:08
*/
public class Tank {
private static final int TANK_SPEED = 5;
boolean bu = false,bd = false,bl = false,br = false;
enum Direction {u,d,l,r,lu,ld,ru,rd,stop};
private Direction dir = Direction.stop;
int x;
int y;
public void move(){
switch (dir){
case u: y -= TANK_SPEED;
break;
case d: y += TANK_SPEED;
break;
case l: x -= TANK_SPEED;
break;
case r: x += TANK_SPEED;
break;
case lu: x -= TANK_SPEED;y -= TANK_SPEED;
break;
case ru: y -= TANK_SPEED; x += TANK_SPEED;
break;
case ld: y += TANK_SPEED; x -= TANK_SPEED;
break;
case rd: x += TANK_SPEED; y += TANK_SPEED;
break;
case stop:
break;
}
}
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
move();
}
public void tankRun(KeyEvent e){
int key = e.getKeyCode();
switch (key){
case KeyEvent.VK_RIGHT: br = true;
break;
case KeyEvent.VK_LEFT: bl = true;
break;
case KeyEvent.VK_UP: bu = true;
break;
case KeyEvent.VK_DOWN: bd = true;
break;
}
locateDirection();
}
public void locateDirection(){
if(!br && !bl && !bd && bu){
dir = Direction.u;
}
else if(br && !bl && !bd && !bu){
dir = Direction.r;
}
else if(!br && bl && !bd && !bu){
dir = Direction.l;
}
else if(!br && !bl && bd && !bu){
dir = Direction.d;
}
else if(br && !bl && bd && !bu){
dir = Direction.rd;
}
else if(!br && bl && !bd && !bu){
dir = Direction.ld;
}
else if(br && !bl && !bd && bu){
dir = Direction.ru;
}
else if(!br && bl && !bd && bu){
dir = Direction.lu;
}
else if(!br && !bl && !bd && !bu){
dir = Direction.stop;
}
}
}
出现问题: bl br bu bd 赋值为true后没有还原;