62,614
社区成员
发帖
与我相关
我的任务
分享
package tankwar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class MyTank {
int x;
int y;
public static final int WIDTH = 30;
public static final int HEITHT = 30;
int X_SPEED = 5;
int Y_SPEED = 5;
public MyTank(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
private boolean isLeft = false;
private boolean isUp = false;
private boolean isRight = false;
private boolean isDown = false;
enum Direction {
L, LU, U, RU, R, RD, D, LD, STOP
};
public TankClient tc = null; //这里写null了,请检查
public Direction dir = Direction.L;
Direction barrelDir = dir;
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.fillOval(this.x, this.y, WIDTH, HEITHT);
g.setColor(c);
barrelDraw(g);
move();
}
public void barrelDraw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
switch (barrelDir) {
case L:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + WIDTH/2);
break;
case LU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y);
break;
case U:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y);
break;
case RU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y);
break;
case R:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT/2);
break;
case RD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT);
break;
case D:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y + HEITHT);
break;
case LD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + HEITHT);
break;
case STOP:
break;
}
}
public void move() {
switch (dir) {
case L:
x = x - X_SPEED;
break;
case LU:
x = x - X_SPEED;
y = y - Y_SPEED;
break;
case U:
y = y - Y_SPEED;
break;
case RU:
x = x + X_SPEED;
y = y - Y_SPEED;
break;
case R:
x = x + X_SPEED;
break;
case RD:
x = x + X_SPEED;
y = y + Y_SPEED;
break;
case D:
y = y + Y_SPEED;
break;
case LD:
x = x - X_SPEED;
y = y + Y_SPEED;
break;
case STOP:
break;
}
if(this.dir != Direction.STOP) {
this.barrelDir = this.dir;
}
if(isLeft == true && isUp == false && isRight == false && isDown == false) dir = Direction.L;
if(isLeft == false && isUp == true && isRight == false && isDown == false) dir = Direction.U;
if(isLeft == false && isUp == false && isRight == true && isDown == false) dir = Direction.R;
if(isLeft == false && isUp == false && isRight == false && isDown == true) dir = Direction.D;
if(isLeft == true && isUp == true && isRight == false && isDown == false) dir = Direction.LU;
if(isLeft == false && isUp == true && isRight == true && isDown == false) dir = Direction.RU;
if(isLeft == false && isUp == false && isRight == true && isDown == true) dir = Direction.RD;
if(isLeft == true && isUp == false && isRight == false && isDown == true) dir = Direction.LD;
if(isLeft == false && isUp == false && isRight == false && isDown == false) dir = Direction.STOP;
}
public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEITHT/2 - Bullet.HEIGHT/2;
Bullet bullet = new Bullet(x, y, barrelDir);
tc.bullets.add(bullet);
return bullet;
}
public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_RIGHT:
isRight = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
break;
}
}
public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_RIGHT:
isRight = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
break;
case KeyEvent.VK_SPACE:
fire();
}
}
}