import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.List;
public class Tank {
private int x, y; // 坦克的位置
public int getX() {
return x;
}
public int getY() {
return y;
}
private int oidx, oidy; // 坦克的位置
private boolean bl = false, br = false, bu = false, bd = false; // 四个方向键是否按下
private boolean good;
public int liveNum = 100;
private TankClient tc;
private boolean live = true;
private TankFX tankDir; // 坦克默认方向停止
private TankFX paoDir = TankFX.D; // 坦克炮筒方向
private static final int XSPEED = 5; // X速度
private static final int YSPEED = 5; // Y速度
private static Random r = new Random() ; // X速度
private LiveBar myBar = new LiveBar();
public static final int TANK_W = 35; // 坦克宽
public int Sore = 0;
public static final int TANK_H = 35; // 坦克高
private int step = r.nextInt(12)+4;
private boolean in = false;
private static Toolkit t = Toolkit.getDefaultToolkit();
private static Image[] tank1 = {
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1L.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1LU.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1LD.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1R.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1RU.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1RD.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1U.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/1D.gif"))};
private static Image[] tank2 = {
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2L.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2LU.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2LD.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2R.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2RU.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2RD.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2U.gif")),
t.getImage(BaoZha.class.getClassLoader()
.getResource("images/2D.gif"))};
public boolean isGood() {
return good;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public Tank(int x, int y ,boolean good,TankFX tankDir,TankClient tc) {
this.x = x;
this.y = y;
this.oidx = x;
this.oidy = y;
this.good = good;
this.tankDir = tankDir;
this.tc = tc;
}
public Rectangle getTanRect(){
return new Rectangle(x,y,TANK_W,TANK_H);
}
public boolean zhuangQiang(Qiang t){
if(getTanRect().intersects(t.getRect()) && live){
stay();
return true;
}
return false;
}
public boolean zhuangMyTank(Tank ts){
if(this.getTanRect().intersects(ts.getTanRect()) && this.isLive() && ts.isLive() && this!=ts){
stay();
return true;
}
return false;
}
public boolean zhuangTanks(List<Tank> ts){
if(ts!=null){
for(int i=0;i<ts.size();i++){
Tank t = ts.get(i);
if(this.getTanRect().intersects(t.getTanRect()) && this.isLive() && t.isLive() && this!=t){
stay();
return true;
}
}
}
return false;
}
private void stay() {
x = this.oidx;
y = this.oidy;
}
public void paint(Graphics g) {
if (!in) {
in = true;
for (int i = 0; i < tank1.length; i++) {
g.drawImage(tank1[i], -500, -500, null);
}
for (int i = 0; i < tank2.length; i++) {
g.drawImage(tank2[i], -500, -500, null);
}
}
if(!live){
if(!good){
tc.diTanks.remove(this);
}
return;
}
switch (paoDir) {
case L:
if(this.isGood()){
g.drawImage(tank1[0], x , y , null);
}else{
g.drawImage(tank2[0], x , y , null);
}
break;
case LU:
if(this.isGood()){
g.drawImage(tank1[1], x , y , null);
}else{
g.drawImage(tank2[1], x , y , null);
}
break;
case LD:
if(this.isGood()){
g.drawImage(tank1[2], x , y , null);
}else{
g.drawImage(tank2[2], x , y , null);
}
break;
case R:
if(this.isGood()){
g.drawImage(tank1[3], x , y , null);
}else{
g.drawImage(tank2[3], x , y , null);
}
break;
case RU:
if(this.isGood()){
g.drawImage(tank1[4], x , y , null);
}else{
g.drawImage(tank2[4], x , y , null);
}
break;
case RD:
if(this.isGood()){
g.drawImage(tank1[5], x , y , null);
}else{
g.drawImage(tank2[5], x , y , null);
}
break;
case U:
if(this.isGood()){
g.drawImage(tank1[6], x , y , null);
}else{
g.drawImage(tank2[6], x , y , null);
}
break;
case D:
if(this.isGood()){
g.drawImage(tank1[7], x , y , null);
}else{
g.drawImage(tank2[7], x , y , null);
}
break;
}
myBar.paint(g);
move();
}
public void move() { // 根据坦克方向移动
this.oidx = x;
this.oidy = y;
switch (tankDir) {
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case R:
x += XSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case U:
y -= YSPEED;
break;
case D:
y += YSPEED;
break;
case STOP:
break;
}
if(!good){
if(step == 0){
TankFX[] dtDir = TankFX.values();
int rn = r.nextInt(dtDir.length);
tankDir = dtDir[rn];
step = r.nextInt(12)+4;
}
step--;
int num = r.nextInt(50);
if(num>40){
tc.MyMissiles.add(play());
}
}
if(this.tankDir != TankFX.STOP){
paoDir = this.tankDir;
}
if (x < 50 | y < 85 | x > TankClient.GAME_WIDTH-83
| y > TankClient.GAME_HEIGHT-83) {
stay();
}
}
public void KeyPressed(KeyEvent e) { // 根据按键设置坦克方向
if(!isLive()){
return;
}
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_A:
bl = true;
break;
case KeyEvent.VK_D:
br = true;
break;
case KeyEvent.VK_W:
bu = true;
break;
case KeyEvent.VK_S:
bd = true;
break;
}
lookTankFX();
}
private Missile play() {
if(!live){
return null;
}
Missile m = null;
if(good){
m = new Missile(x+((TANK_W-Missile.MISSILE_W)/2),y+((TANK_H-Missile.MISSILE_H)/2),paoDir,this.tc,true);
}else {
m = new Missile(x+((TANK_W-Missile.MISSILE_W)/2),y+((TANK_H-Missile.MISSILE_H)/2),paoDir,this.tc,false);
}
return m;
}
private Missile superPlay(TankFX dir) {
if(!live){
return null;
}
Missile m = new Missile(x+((TANK_W-Missile.MISSILE_W)/2),y+((TANK_H-Missile.MISSILE_H)/2),dir,this.tc,true);
return m;
}
public void keyReleased(KeyEvent e) { // 根据按键设置坦克方向
if(!isLive()){
return;
}
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_A:
bl = false;
break;
case KeyEvent.VK_D:
br = false;
break;
case KeyEvent.VK_W:
bu = false;
break;
case KeyEvent.VK_S:
bd = false;
break;
case KeyEvent.VK_J:
Thread t1 = new Thread(new SheMid());
t1.start();
tc.MyMissiles.add(play());
break;
case KeyEvent.VK_K:
Thread t2 = new Thread(new SheMid());
t2.start();
TankFX[] superDir = TankFX.values();
for(int i=0;i<8;i++){
tc.MyMissiles.add(superPlay(superDir[i]));
}
break;
}
lookTankFX();
}
public void lookTankFX() { // 根据按下的键确定坦克是8个方向中的哪个
if (bl && !br && !bu && !bd) {
tankDir = TankFX.L;
} else if (bl && !br && bu && !bd) {
tankDir = TankFX.LU;
} else if (bl && !br && !bu && bd) {