import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.swing.ImageIcon;
public class ETank {
@Override
public String toString() {
return "坦克级别:" + grade;
}
private int x, y;// 定义坦克出现的起始坐标
private int lastX, lastY;// 定义坦克上一次的位置
private Image tk = null;// 定义坦克图片
private static Image[] tkimgs = null; // 定义一个图片数组,用于存储坦克各个方向的图片
private Fight fight = null;// 持有对方的引用便于发出炮弹
private Direction movState = Direction.STOP;// 定义坦克的初始运动状态
private Direction lastState = Direction.DOWN;// 定义坦克炮口的状态
private int grade;// 定义坦克级别,级别不同,坦克的图片展示不同
private int movLength = 0;
private static final int SPEED = 6;
private Bomb b = null;
private static Map<String, Image> imgs = new HashMap<String, Image>();// 引人map,封装图片,并进行标示
static {
tkimgs = new Image[] {
// 一级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank1Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank1Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank1Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank1Left.png")).getImage(),
// 二级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank2Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank2Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank2Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank2Left.png")).getImage(),
// 三级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank3Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank3Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank3Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank3Left.png")).getImage(),
// 四级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank4Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank4Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank4Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank4Left.png")).getImage(),
// 五级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank5Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank5Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank5Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank5Left.png")).getImage(),
// 六级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank6Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank6Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank6Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank6Left.png")).getImage(),
// 七级坦克
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank7Up.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank7Down.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank7Right.png")).getImage(),
new ImageIcon(Tank.class.getClassLoader().getResource(
"img/etank/tank7Left.png")).getImage(),
};
imgs.put("1left", tkimgs[3]);
imgs.put("1right", tkimgs[2]);
imgs.put("1down", tkimgs[1]);
imgs.put("1up", tkimgs[0]);
imgs.put("2left", tkimgs[7]);
imgs.put("2right", tkimgs[6]);
imgs.put("2down", tkimgs[5]);
imgs.put("2up", tkimgs[4]);
imgs.put("3left", tkimgs[11]);
imgs.put("3right", tkimgs[10]);
imgs.put("3down", tkimgs[9]);
imgs.put("3up", tkimgs[8]);
imgs.put("4left", tkimgs[15]);
imgs.put("4right", tkimgs[14]);
imgs.put("4down", tkimgs[13]);
imgs.put("4up", tkimgs[12]);
imgs.put("5left", tkimgs[19]);
imgs.put("5right", tkimgs[18]);
imgs.put("5down", tkimgs[17]);
imgs.put("5up", tkimgs[16]);
imgs.put("6left", tkimgs[23]);
imgs.put("6right", tkimgs[22]);
imgs.put("6down", tkimgs[21]);
imgs.put("6up", tkimgs[20]);
imgs.put("7left", tkimgs[27]);
imgs.put("7right", tkimgs[26]);
imgs.put("7down", tkimgs[25]);
imgs.put("7up", tkimgs[24]);
}
/**
* 定义tank的构造方法
*/
public ETank(int x, int y) {
this.x = x;
this.y = y;
this.lastX = x;
this.lastY = y;
}
public ETank(int x, int y, int grade) {
this(x, y);
this.grade = grade;
tk = imgs.get("" + grade + "down");
}
/**
* 复写tank的构造方法
*/
public ETank(int x, int y, Fight fight) {
this(x, y);
this.fight = fight;
// tk = imgs.get("2up");// 设定坦克的初始图片
}
public ETank(int x, int y, Fight fight, int grade) {
this(x, y, fight);
this.grade = grade;
if(grade == 4){
this.grade = 7;
grade = 7;
}
tk = imgs.get("" + grade + "down");
// System.out.println("tank 级别:" + grade);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
/**
* 画出一辆坦克
*
* @param g
*/
public void draw(Graphics g) {
g.drawImage(tk, x, y, null);
this.mov();
}
public void mov() {
this.lastX = x;
this.lastY = y;
switch (movState) {
case UP:
tk = imgs.get("" + grade + "up");
this.lastY = y;
y -= SPEED;
if (y <= 30) {
y = 30;
}
break;
case DOWN:
tk = imgs.get("" + grade + "down");
y += SPEED;
// 设置边界
if (y >= 541) {
y = 541;
}
break;
case LEFT:
tk = imgs.get("" + grade + "left");
x -= SPEED;
if (x <= 6) {
x = 6;
}
break;
case RIGHT:
tk = imgs.get("" + grade + "right");
x += SPEED;
if (x >= 612) {
x = 612;
}
break;
case STOP:
switch (lastState) {
case UP:
tk = imgs.get("" + grade + "up");
break;
case DOWN:
tk = imgs.get("" + grade + "down");
break;
case LEFT:
tk = imgs.get("" + grade + "left");
break;
case RIGHT:
tk = imgs.get("" + grade + "right");
break;
}
}
if (this.isTouch()) {
this.back();
}
if (movLength == 0) {
movLength = new Random().nextInt(50);
this.movState = this.getDir();
} else {
movLength--;
if (movLength % 20 == 0) {
this.fire();
}
}
// System.out.println("坦克在移动,坐标为:x= "+x+" y = "+y);
}
public void fire() {
// System.out.println("开火");
if ((b == null) || (!b.isLive())) {
switch (this.lastState) {
case UP:
b = new Bomb(x + 11, y - 12, fight, this.lastState, false);
break;
case DOWN:
b = new Bomb(x + 11, y + 36, fight, this.lastState, false);
break;
case LEFT:
b = new Bomb(x - 12, y + 9, fight, this.lastState, false);
break;
case RIGHT:
b = new Bomb(x + 36, y + 9, fight, this.lastState, false);
}
}
}
public Rectangle getETankRectangle() {
return new Rectangle(x, y, 36, 36);
}
/**
* 定义坦克运动的方向
*
* @return
*/
public Direction getDir() {
Ra