package com.lining;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
/**
* 坦克类
* @author LiNing
*
*/
public class Tank {
public static final int X_SPEED=5;
public static final int Y_SPEED=5;
private int x,y;//坦克当前的坐标
private int life=100;//坦克生命值
private BloodBar bb=new BloodBar();//血条类
/**
* 点击life右键source-》setters and getters
* @return
*/
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
private int oldX,oldY;//解除坦克黏贴的x,y
private static Random r=new Random();//产生随机数,设置坦克设计方向和坦克前行步数
private boolean good;//设置敌我坦克信号量
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
private boolean live=true;//设置坦克生死信号量
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
TankClient tc;//添加对方引用
private boolean bL=false,bU=false,bD=false,bR=false;//4个方向键
//enum Direction{L,LU,U,RU,R,RD,D,LD,STOP}
private Direction dir=Direction.STOP;//坦克前进方向
private Direction ptDir=Direction.STOP;//坦克射击方向
private int step=r.nextInt(12)+5;//坦克前进步数
private static Toolkit tk =Toolkit.getDefaultToolkit();
/**
* 使用反射来取文件,获取Explode的类装载器getclassloder,用它获取资源,
* 他会找遍我们设置的classpath路径来找文件,images在classpath里面
* explode.class就是class类的一个对象
* 由于异步读取会出现bug
*/
/**
* 下面是静态代码区,用来给变量初始化
* 静态函数必须访问静态变量
*/
private static Image[] tankImages=null;
private static Map<String,Image> imgs=new HashMap<String,Image>();
static{tankImages=new Image[]{
tk.getImage(Tank.class.getClassLoader().getResource("images/tankL.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankLU.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankU.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankRU.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankR.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankRD.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankD.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankLD.gif"))
};
imgs.put("L", tankImages[0]);
imgs.put("LU", tankImages[1]);
imgs.put("U", tankImages[2]);
imgs.put("RU", tankImages[3]);
imgs.put("R", tankImages[4]);
imgs.put("RD", tankImages[5]);
imgs.put("D", tankImages[6]);
imgs.put("LD", tankImages[7]);
};
/**
* 坦克的长度和宽度
*/
public static final int WIDTH =30;
public static final int HEIGHT=30;
public Tank(int x, int y,boolean g) {
super();
this.x = x;
this.y = y;
this.oldX=x;//可要可不要
this.oldY=y;//可要可不要
this.good=g;
}
public Tank(int x,int y,boolean g,Direction dir,TankClient tc){
this(x,y,g);
this.tc=tc;
this.dir=dir;
}
public void draw(Graphics g){
/**
* 如果坦克被打死则不用再将其画出
*/
if(!live)
{
if(!good)//如果使敌方坦克则将其从集合中移除
{
tc.tanks.remove(this);
}
return;
}
/*Color c=g.getColor();*/
if(good)//如果是我防坦克则要将其生命值画出
{
bb.draw(g);
}
/*g.setColor(Color.RED);
}
else
{
g.setColor(Color.BLUE);
}
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(Color.BLUE);*/
/**
* 根据方向参数,画出不同方向的坦克
*/
switch(ptDir)
{
case L:
g.drawImage(imgs.get("L"), x, y, null);//左上和右下的坐标
break;
case LU:
g.drawImage(imgs.get("LU"), x, y, null);
break;
case U:
g.drawImage(imgs.get("U"), x, y, null);
break;
case RU:
g.drawImage(imgs.get("RU"), x, y, null);
break;
case R:
g.drawImage(imgs.get("R"), x, y, null);
break;
case RD:
g.drawImage(imgs.get("RD"), x, y, null);
break;
case D:
g.drawImage(imgs.get("D"), x, y, null);
break;
case LD:
g.drawImage(imgs.get("LD"), x, y, null);
break;
case STOP:
break;
}
//g.setColor(c);
move();//调用坦克移动函数
if(this.dir!=Direction.STOP)
{
this.ptDir=dir;
}
if(x<0)//坦克出x界问题
x=0;
if(y<30)//坦克出y界问题,并且由于上面的标题栏缘故掩盖坦克
y=30;
if(x+Tank.WIDTH>TankClient.GAME_WIDTH)//坦克出右边界
x=TankClient.GAME_WIDTH-Tank.WIDTH;
if(y+Tank.HEIGHT>TankClient.GAME_HEIGHT)//坦克出左边界
y=TankClient.GAME_HEIGHT-Tank.HEIGHT;
/**
* 设置敌方坦克的移动,包括方向和每次移动步数
*/
if(!good){
Direction[] dirs=Direction.values();
if(step==0)
{
step=r.nextInt(12)+5;
int rn=r.nextInt(dirs.length);//产生一个随机整数
dir=dirs[rn];
}
step--;
/**
* 解决坦克移动太快问题
*/
if(r.nextInt(40)>35)
this.fire();
}
}
/**
* 根据坦克的方向进行移动
*/
public void move(){
this.oldX=x;
this.oldY=y;
switch(dir)
{
case L:
x-=X_SPEED;
break;
case LU:
x-=X_SPEED;
y-=Y_SPEED;
break;
case U:
y-=Y_SPEED;
break;
case RU:
x+=X_SPEED;
y-=Y_SPEED;
break;
case R:
x+=X_SPEED;
break;
case RD:
x+=X_SPEED;
y+=Y_SPEED;
break;
case D:
y+=Y_SPEED;
break;
case LD:
x-=X_SPEED;
y+=Y_SPEED;
break;
case STOP:
break;
}
}
/**
* 键盘按下事件监听器
* @param e
*/
public void keyPressed(KeyEvent e){
int key=e.getKeyCode();
switch(key)
{
case KeyEvent.VK_RIGHT:
bR=true;
break;
case KeyEvent.VK_LEFT:
bL=true;
break;
case KeyEvent.VK_UP:
bU=true;
break;
case KeyEvent.VK_DOWN:
bD=true;
break;
}
locatedDirection();
}
/**
* 根据键盘按键的四个方向决定他控制的八个方向
*/
private void locatedDirection(){
if(bL&&!bU&&!bR&&!bD)
dir=Direction.L;
else if(bL&&bU&&!bR&&!bD)
dir=Direction.LU;
else if(!bL&&bU&&!bR&&!bD)
dir=Direction.U;
else if(!bL&&bU&&bR&&!bD)
dir=Direction.RU;
else if(!bL&&!bU&&bR&&!bD)
dir=Direction.R;
else if(!bL&&!bU&&bR&&bD)
dir=Direction.RD;
else if(!bL&&!bU&&!bR&&bD)
dir=Direction.D;
else if(bL&&!bU&&!bR&&bD)
dir=Direction.LD;
else if(!bL&&!bU&&!bR&&!bD)
dir=Direction.STOP;
}
/**
* 设置键盘方向的释放事件
* @param e
*/
public void keyReleased(KeyEvent e) {
int key=e.getKeyCode();
switch(key)
{
case KeyEvent.VK_F2:
if(!this.isLive())
{
this.live=true;
this.life=100;
}
case KeyEvent.VK_A:
superFire();
break;
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_RIGHT:
bR=false;
break;
case KeyEvent.VK_LEFT:
bL=false;
break;
case KeyEvent.VK_UP:
bU=false;
break;
case KeyEvent.VK_DOWN:
bD=false;
break;
}
locatedDirection();
}
/**
* 开火函数,子弹数量增加
* @return
*/
public Missile fire(){
if(!live)
return null;
int x=this.x+Tank.WIDTH/2-Missile.WIDTH/2;
int y=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2;
Missile m=new Missile(x,y,good,ptDir,this.tc);
tc.missiles.add(m);
return m;
}
/**
* 用于判断相撞的函数调用
* @return
*/
public Rectangle getRect(){
return new Rectangle(x,y,WIDTH,HEIGHT);
}
/**
*
* @param 显示撞墙结果true or false
* @return
*/
public boolean collidesWithWall(Wall w){
if(this.isLive()&&this.getRect().intersects(w.getRect()))
{
this.stay();
return true;
}
return false;
}
/**
* 设置坦克撞墙,或相撞后转向
*/
private void stay(){
x=this.oldX;
y=this.oldY;
}
/**
* 调用this.getRect().intersects(tank.getRect()判断坦克是否相撞
* @param tanks
* @return
*/
public boolean collidesWithTank(List<Tank>
- 1
- 2
- 3
前往页