📄 mytank.java
字号:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class MyTank {
int x,y;
static final int XSPEED = 1;
static final int YSPEED = 1;
private boolean bU = false,bR = false,bD = false,bL = false;
enum Direction {U,RU,R,RD,D,LD,L,LU,STOP};
private Direction dir =Direction.STOP ;
public MyTank(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
move();
}
public void keypressed(KeyEvent e){
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
case KeyEvent.VK_LEFT:
bL = true;
break;
}
Direct();
}
public void Direct() {
if(!bU && !bR && !bD && !bL) dir = Direction.STOP;
else if(bU && bR && bD && bL) dir = Direction.U;
else if(!bU && !bR && bD && !bL) dir = Direction.D;
else if(!bU && !bR && !bD && bL) dir = Direction.L;
else if(!bU && bR && !bD && !bL) dir = Direction.R;
else if(bU && !bR && !bD && bL) dir = Direction.LU;
else if(!bU && !bR && bD && bL) dir = Direction.LD;
else if(!bU && bR && bD && !bL) dir = Direction.RD;
else if(bU && bR && !bD && !bL) dir = Direction.RU;
}
public void move(){
switch(dir){
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case STOP:
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -