package dzx.dong.minigame.tankwar;
import dzx.dong.minigame.barrier.Gold;
import dzx.dong.minigame.barrier.Home;
import dzx.dong.minigame.barrier.Iron;
import dzx.dong.minigame.barrier.Wall;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
public class Tank{
public static final int SIZE=50;
public int hpMax=50; //基地最高血量为50
private int x;
private int y;
private int hp=50; //设置血量为50
private int level=1; //初始坦克的星级为一级
private int style; //̹不同的坦克形式
private int power; //̹
private int speed=5; //初始速度为5
private Direction dir; //
private ImageIcon myPlane_U; //ImageIcon位于javax.swing包中。可以根据Image绘制Icon
private ImageIcon myPlane_R; //
private ImageIcon myPlane_D; //
private ImageIcon myPlane_L; //
private boolean up,left,right,down; //u,l,r,d为布诺型变量
private boolean self; //独自,布尔类型,默认值为false,这里默认为独自
private boolean live; //存活,布尔类型,默认值为false,这里默认为存活
private ArrayList<Tank> allTank;//ArrayList数组队列,提供了相关的添加、删除、修改、遍历等功能。
private final ArrayList<Wall> walls;
private final ArrayList<Iron> irons;
private final ArrayList<Gold> golds;
private final ArrayList<Missle> missles;
private final ArrayList<Boom> booms;
private final Home home;
private long noFire; //返回noFire的long值作为值
private long fireTime; //
private boolean isProtect; //
public Direction getDir() {
return dir;
}//获得方向的属性,并开始对这些以下这些对象进行序列化,序列化的
public void setUp(boolean up) {
this.up = up;
}//给布诺型变量赋值
public void setLeft(boolean left) {
this.left = left;
}
public void setRight(boolean right) {
this.right = right;
}
public void setDown(boolean down) {
this.down = down;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isSelf() {
return self;
}
public void setHp(int hp){
this.hp=hp;
}
public int getHp(){
return hp;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isLive() {
return live;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setNoFire(long noFire) {
this.noFire = noFire;
}
public long getNoFire()
{
return noFire;
}
public int getPower()
{
return power;
}
public int getLevel()
{
return level;
}
public void setLevel(int level)
{
this.level = level;
}
public void setProtect(boolean isProtect)
{
this.isProtect = isProtect;
}
public boolean isProtect()
{
return isProtect;
}//序列化结束
protected static enum Direction{U,L,D,R} //U,R,D,L被标注为静态的属性也没有办法被序列化
public Tank(int x,int y,boolean self,ArrayList<Tank> allTank,ArrayList<Wall> walls,ArrayList<Iron> irons,ArrayList<Gold> golds,ArrayList<Missle> missles,Home home,ArrayList<Boom>booms,int style) throws Exception{
this.missles = missles;
this.booms = booms;
this.setX(x);
this.setY(y);
this.self=self;
this.style=style;
this.allTank = allTank;
this.walls = walls;
this.irons = irons;
this.golds = golds;
this.home = home;
dir=Direction.R;
setLive(true);
if(!self) level=1+new Random().nextInt(3);//生成[0,3]的任意数集
re(); //坦克类型的定义
}
//定义不同类型的坦克
public void re()
{
switch (style) {
case 1:
switch (level)
{
case 1:
power=10;
speed=3;
hp=80;
hpMax=80;
fireTime=30l;
break;
case 2:
power=12;
speed=4;
hp=100;
hpMax=100;
fireTime=25l;
break;
case 3:
power=14;
speed=5;
hp=120;
hpMax=120;
fireTime=20l;
break;
}
break;
case 2:
switch (level)
{
case 1:
power=12;
speed=4;
hp=50;
hpMax=50;
fireTime=30l;
break;
case 2:
power=16;
speed=5;
hp=60;
hpMax=60;
fireTime=25l;
break;
case 3:
power=20;
speed=6;
hp=70;
hpMax=70;
fireTime=20l;
break;
}
break;
case 3:
switch (level)
{
case 1:
power=2;
speed=6;
hp=40;
hpMax=40;
fireTime=15l;
break;
case 2:
power=3;
speed=8;
hp=50;
hpMax=50;
fireTime=10l;
break;
case 3:
power=4;
speed=10;
hp=60;
hpMax=60;
fireTime=5l;
break;
}
break;
case 4:
switch (level)
{
case 1:
power=999999999;
speed=15;
hp=9999999;
hpMax=999999999;
fireTime=5l;
break;
case 2:
power=9999999;
speed=15;
hp=9999999;
hpMax=9999999;
fireTime=5l;
break;
case 3:
power=9999999;
speed=15;
hp=9999999;
hpMax=9999999;
fireTime=5l;
break;
}
break;
}
//误差判断
if(self)
{
if (style==4) {
myPlane_U = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + (style-1) + "_self_up_" + level + ".png"));
myPlane_R = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + (style-1) + "_self_right_" + level + ".png"));
myPlane_D = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + (style-1) + "_self_down_" + level + ".png"));
myPlane_L = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + (style-1) + "_self_left_" + level + ".png"));
}else {
myPlane_U = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + style + "_self_up_" + level + ".png"));
myPlane_R = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + style + "_self_right_" + level + ".png"));
myPlane_D = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + style + "_self_down_" + level + ".png"));
myPlane_L = new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" + style + "_self_left_" + level + ".png"));
}
}
//�����Լ��ͼ��صз�̹�������ͼƬ
else
{
myPlane_U=new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" +style+"_enemy_up_"+level+".png"));
myPlane_R=new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" +style+"_enemy_right_"+level+".png"));
myPlane_D=new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" +style+"_enemy_down_"+level+".png"));
myPlane_L=new ImageIcon(Tank.class.getResource("/dzx/dong/minigame/img/TANK" +style+"_enemy_left_"+level+".png"));
hp/=2;
hpMax/=2;
}
}
//̹坦克正常移动的情况
public void move() {
//
if(up&&getY()>0&&noMove(getX(), getY())&&noMove(getX()+SIZE-1,getY()-speed))//从新值中get到一个新的属性
setY(getY() - speed);//就将新的属性赋值给Y
else if(right&&getX()<TankWar.AREA_WIDTH-50&&noMove(getX()+SIZE, getY())&&noMove(getX()+speed+SIZE, getY()+SIZE-1))
setX(getX() + speed);
else if(down&&getY()<TankWar.AREA_HEIGHT-80&&noMove(getX(), getY()+speed+SIZE)&&noMove(getX()+SIZE-1, getY()+speed+SIZE))
setY(getY() + speed);
else if (left&&getX()>0&&noMove(getX(), getY())&&noMove(getX(), getY()+SIZE-1))
setX(getX() - speed);
}
//判断键盘输入的正确与否
//ture
public void keyPress(KeyEvent e)//在源组件上按下一个键后被调用
{
switch (e.getKeyCode()) {//返回这个事件中和这个键相关的
case KeyEvent.VK_UP:
up=true;
dir=Direction.U;
break;
case KeyEvent.VK_R
评论0