package com.xingda.TankWar; import java.awt.*; import java.awt.event.*; import java.util.*; /** * 用于生成坦克 * @author xingda * */ public class Tank { /** * 坦克横向移动速度 */ public static final int XSPEED = 3; /** * 坦克纵向移动速度 */ public static final int YSPEED = 3; /** * 坦克宽度 */ public static final int WIDTH = 40; /** * 坦克高度 */ public static final int HEIGHT = 40;
private boolean live = true;//坦克是否活着 private boolean good ;//坦克是好是坏 private int life = 100;//最初生命值为100; private static Random r = new Random();//随机数生成器,static 多辆坦克只产生一个生成器 private BloodBar tankBlood = new BloodBar();//坦克血条 enum Direction { U, D, L, R, LU, RU, LD, RD, STOP }; private int step = r.nextInt(40) + 3;//最少按原来方向移动三次,最大14次 Direction dir = Direction.STOP;//坦克的方向,默认stop boolean left = false, right = false, up = false, down = false; int x, y; int oldX,oldY;//上次坦克的位置 TankClient tc = null; public Direction ptDir = Direction.U;//炮筒方向,默认向上
/** * * @param x 坦克x坐标 * @param y 坦克y坐标 * @param good 坦克的好坏 */ public Tank(int x, int y, boolean good) { this.x = x; this.y = y; this.oldX = x; this.oldY = y; this.good = good; } /** * * @param x 坦克x坐标 * @param y 坦克y坐标 * @param good 坦克的好坏 * @param dir 坦克朝向 * @param tc TankClient的一个引用 */ public Tank(int x, int y, boolean good, Direction dir, TankClient tc) { this(x, y, good); this.dir = dir; this.tc = tc; }
/** * 画坦克 * @param g */ public void draw(Graphics g) { if(!live) return; Color c = g.getColor(); if(good) g.setColor(Color.red); else g.setColor(Color.yellow); g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c);
if(good) tankBlood.draw(g); switch (ptDir) { case L: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2); break; case R: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2); break; case U: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y); break; case D: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT); break; case LU: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y); break; case RU: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y); break; case LD: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT); break; case RD: g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT); break; case STOP: break; }
move(); }
/** * 键盘按下事件 * @param e KeyEvent事件 */ public void pressed(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_F2: if(!this.live){ this.live = true; this.life = 100; } break; case KeyEvent.VK_A: fire(); break; case KeyEvent.VK_S: superFire(); break; case KeyEvent.VK_RIGHT: right = true; break; case KeyEvent.VK_LEFT: left = true; break; case KeyEvent.VK_UP: up = true; break; case KeyEvent.VK_DOWN: down = true; break; } locateDirection(); }
/** * 键盘松开事件 * @param e KeyEvent 事件 */ public void Released(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_RIGHT: right = false; break; case KeyEvent.VK_LEFT: left = false; break; case KeyEvent.VK_UP: up = false; break; case KeyEvent.VK_DOWN: down = false; break; } locateDirection(); }
/** * 坦克移动 */ public void move() { oldX = x; oldY = y; switch (dir) { case L: x -= XSPEED; break; case R: x += XSPEED; break; case U: y -= XSPEED; break; case D: y += XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case RU: x += XSPEED; y -= YSPEED; break; case LD: x -= XSPEED; y += YSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case STOP: break; }
/** * 定位方向 */ public void locateDirection() { if (left && !right && !up && !down) dir = Direction.L; else if (!left && right && !up && !down) dir = Direction.R; else if (!left && !right && up && !down) dir = Direction.U; else if (!left && !right && !up && down) dir = Direction.D; else if (left && !right && up && !down) dir = Direction.LU; else if (!left && right && up && !down) dir = Direction.RU; else if (left && !right && !up && down) dir = Direction.LD; else if (!left && right && !up && down) dir = Direction.RD; else if (!left && !right && !up && !down) dir = Direction.STOP; }
/** * 普通子弹发射 * @return 返回Missile类型 */ public Missile fire() { if(!live) return null;//如果坦克死了就不发子弹 if (dir == Direction.STOP) dir = Direction.R; int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2; int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2; Missile missile = new Missile(x, y, ptDir, good, this.tc); tc.missiles.add(missile); return missile; }
/** * 超级子弹发射 * @param dir 超级子弹发射方向 * @return 返回Missile类型 */ public Missile fire(Direction dir) { if(!live) return null;//如果坦克死了就不发子弹 if (dir == Direction.STOP) dir = Direction.R; int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2; int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2; Missile missile = new Missile(x, y, dir, good, this.tc); tc.missiles.add(missile); return missile; }
/** * 获得坦克矩形框,用于碰撞检测 * @return 返回Rectangle类型 */ public Rectangle getRect() { return new Rectangle(x, y, Tank.WIDTH, Tank.HEIGHT); }
/** * 血条类 */ private class BloodBar { public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.red); g.drawRect(x, y-5, Tank.WIDTH, 5); int w = Tank.WIDTH * life / 100; g.setColor(Color.gray); g.fillRect(x, y-5, w, 5); g.setColor(c); } } }
package com.xingda.TankWar; import java.awt.*; import java.util.List;/** * 用于生成子弹 * @author xingda * */ public class Missile { /** * 子弹横向移动速度 */ public static final int XSPEED = 5; /** * 子弹纵向移动速度 */ public static final int YSPEED = 5; /** * 子弹的宽度 */ public static final int WIDTH = 10; /** * 子弹的高度 */ public static final int HEIGHT = 10; int x,y ; Tank.Direction dir = Tank.Direction.STOP; boolean left=false,right=false,up=false,down=false; private boolean live = true;//子弹是否活着 private boolean good; //设置子弹是好是坏 TankClient tc = null;
public Missile(int x, int y, Tank.Direction dir) { this.x = x; this.y = y; this.dir = dir; }
public Missile(int x, int y, Tank.Direction dir,boolean good, TankClient tc) { this(x,y,dir); this.tc = tc; this.good = good; }
/** * 画子弹 */ public void draw(Graphics g) { if(!live) { tc.missiles.remove(this); return; }//判断子弹是否活着,死了就不画了 Color c = g.getColor(); g.setColor(Color.BLACK); g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); move(); }
/** * 子弹移动 */ public void move() { switch(dir) { case L : x-=XSPEED;break; case R : x+=XSPEED;break; case U : y-=XSPEED;break; case D : y+=XSPEED;break; case LU : x-=XSPEED;y-=YSPEED;break; case RU : x+=XSPEED;y-=YSPEED;break; case LD : x-=XSPEED;y+=YSPEED;break; case RD : x+=XSPEED;y+=YSPEED;break; case STOP : break; }
if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) { live = false; tc.missiles.remove(this); }
Color c = g.getColor(); g.setColor(Color.black); g.fillOval(x, y, diameter[step], diameter[step]); g.setColor(c); step++; } }
package com.xingda.TankWar; import java.awt.*;/** * 用于生成墙 * @author xingda * */ public class Wall { int x,y; int width ,height; TankClient tc;
public Wall(int x,int y, int width, int height, TankClient tc) { this.x = x; this.y = y; this.width =width; this.height = height; this.tc = tc; }
/** * 画墙 * @param g */ public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.green); g.fillRect(x, y, width, height); g.setColor(c); }
/** * 获得它的矩形框,用于碰撞检测 * @return Rectangle类型 */ public Rectangle getRect() { return new Rectangle(x, y, width, height); }
package com.xingda.TankWar;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;/**
* 用于生成TankWar的主界面
* @author xingda
*
*/
public class TankClient extends Frame {
/**
* TankWar游戏界面的宽度
*/
public static final int GAME_WIDTH = 800;
/**
* TankWar游戏界面的高度
*/
public static final int GAME_HEIGHT = 600; Image offScreenImage = null;
Tank myTank = new Tank(400, 300, true,Tank.Direction.STOP, this);
Wall wall = new Wall(100,100,30,200,this);
Wall wall2 = new Wall(300,500,200,30,this);
Blood blood = new Blood();
List<Tank> enemyTanks = new ArrayList<Tank>();
List<Explode> explodes = new ArrayList<Explode>();
List<Missile> missiles = new ArrayList<Missile>();
public static void main(String[] args) {
new TankClient().launchFrame();
}
/**
* 生产TankWar界面
*/
public void launchFrame() {
for(int i=0; i<10; i++) {
enemyTanks.add(new Tank(50 + 40*(i + 1), 50 ,false,Tank.Direction.D , this));
}
setBounds(300, 100, GAME_WIDTH, GAME_HEIGHT);
setBackground(Color.WHITE);
setTitle("TankWar");
setResizable(false);// 使用户不能改变窗口大小
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
/**
* 画missile,tank,explode,enemyTank
*/
public void paint(Graphics g) {
g.drawString("Missile count :" + missiles.size(), 10, 50);
g.drawString("Explode count :" + explodes.size(),10,70);
g.drawString("EnemyTank count :" + enemyTanks.size(),10,90);
g.drawString("Tank Life :" + myTank.getLife(),10,110);
if(enemyTanks.size() <= 0) {
for(int i=0; i<5; i++) {
enemyTanks.add(new Tank(50 + 40*(i + 1), 50 ,false,Tank.Direction.D , this));
}
}
myTank.draw(g);
wall.draw(g);
wall2.draw(g);
blood.draw(g);
myTank.collidesWithTanks(enemyTanks);
myTank.eatBlood(blood);
//enemyTank.draw(g);
for(int i=0; i<missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(enemyTanks);
m.hitTank(myTank);
m.hitWall(wall);
m.hitWall(wall2);
m.draw(g);
}
for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
for(int i=0; i<enemyTanks.size(); i++) {
Tank t = enemyTanks.get(i);
t.draw(g);
t.collidesWithWall(wall);
t.collidesWithWall(wall2);
t.collidesWithTanks(enemyTanks);
t.collidesWhitTank(myTank);
}
}
/**
* 用于双缓冲
*/
public void update(Graphics g) {// 可减少画面的抖动
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics offScreen = offScreenImage.getGraphics();
Color c = offScreen.getColor();
offScreen.setColor(Color.WHITE);
offScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
offScreen.setColor(c);
paint(offScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
/**
* 每隔一段时间重画一次
* @author Administrator
*
*/
class PaintThread implements Runnable { public void run() {
while (true) {
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
/**
* 键盘监听器
*/
class KeyMonitor extends KeyAdapter {
/**
* 放掉健
*/
public void keyReleased(KeyEvent e) {
myTank.Released(e);
}
/**
* 按下键
*/
public void keyPressed(KeyEvent e) {
myTank.pressed(e);
} }
}
package com.xingda.TankWar;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* 用于生成坦克
* @author xingda
*
*/
public class Tank {
/**
* 坦克横向移动速度
*/
public static final int XSPEED = 3;
/**
* 坦克纵向移动速度
*/
public static final int YSPEED = 3;
/**
* 坦克宽度
*/
public static final int WIDTH = 40;
/**
* 坦克高度
*/
public static final int HEIGHT = 40;
private boolean live = true;//坦克是否活着
private boolean good ;//坦克是好是坏
private int life = 100;//最初生命值为100;
private static Random r = new Random();//随机数生成器,static 多辆坦克只产生一个生成器
private BloodBar tankBlood = new BloodBar();//坦克血条
enum Direction {
U, D, L, R, LU, RU, LD, RD, STOP
};
private int step = r.nextInt(40) + 3;//最少按原来方向移动三次,最大14次
Direction dir = Direction.STOP;//坦克的方向,默认stop
boolean left = false, right = false, up = false, down = false;
int x, y;
int oldX,oldY;//上次坦克的位置
TankClient tc = null;
public Direction ptDir = Direction.U;//炮筒方向,默认向上
/**
*
* @param x 坦克x坐标
* @param y 坦克y坐标
* @param good 坦克的好坏
*/
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.oldX = x;
this.oldY = y;
this.good = good;
} /**
*
* @param x 坦克x坐标
* @param y 坦克y坐标
* @param good 坦克的好坏
* @param dir 坦克朝向
* @param tc TankClient的一个引用
*/
public Tank(int x, int y, boolean good, Direction dir, TankClient tc) {
this(x, y, good);
this.dir = dir;
this.tc = tc;
}
/**
* 画坦克
* @param g
*/
public void draw(Graphics g) {
if(!live) return;
Color c = g.getColor();
if(good) g.setColor(Color.red);
else g.setColor(Color.yellow);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
if(good) tankBlood.draw(g);
switch (ptDir) {
case L:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT/2);
break;
case R:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT/2);
break;
case U:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y);
break;
case D:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH/2, y + Tank.HEIGHT);
break;
case LU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y);
break;
case RU:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y);
break;
case LD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x, y + Tank.HEIGHT);
break;
case RD:
g.drawLine(x + Tank.WIDTH/2, y + Tank.HEIGHT/2, x + Tank.WIDTH, y + Tank.HEIGHT);
break;
case STOP:
break;
}
move();
}
/**
* 键盘按下事件
* @param e KeyEvent事件
*/
public void pressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_F2:
if(!this.live){
this.live = true;
this.life = 100;
}
break;
case KeyEvent.VK_A:
fire();
break;
case KeyEvent.VK_S:
superFire();
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
} locateDirection();
}
/**
* 键盘松开事件
* @param e KeyEvent 事件
*/
public void Released(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
}
locateDirection();
}
/**
* 坦克移动
*/
public void move() {
oldX = x;
oldY = y;
switch (dir) {
case L:
x -= XSPEED;
break;
case R:
x += XSPEED;
break;
case U:
y -= XSPEED;
break;
case D:
y += XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case STOP:
break;
}
if(this.dir != Direction.STOP) {
ptDir = this.dir;
}
if(x < 0) x = 0;
if(y < 25) y = 25;
if(x > TankClient.GAME_WIDTH - Tank.WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
if(y > TankClient.GAME_HEIGHT - Tank.HEIGHT) y = TankClient.GAME_HEIGHT- Tank.HEIGHT;
if(!good) {//让坦克随机改变方向
Direction[] dirs = Direction.values();
if(step == 0) {//当step等于0的时候改变方向;
int randomNum = r.nextInt(dirs.length);
dir = dirs[randomNum];
step = r.nextInt(40) + 3;
}
step--;
if(r.nextInt(40) > 38) this.fire();
}
if(this.getRect().intersects(tc.wall.getRect())) {
}
}
/**
* 定位方向
*/
public void locateDirection() {
if (left && !right && !up && !down)
dir = Direction.L;
else if (!left && right && !up && !down)
dir = Direction.R;
else if (!left && !right && up && !down)
dir = Direction.U;
else if (!left && !right && !up && down)
dir = Direction.D;
else if (left && !right && up && !down)
dir = Direction.LU;
else if (!left && right && up && !down)
dir = Direction.RU;
else if (left && !right && !up && down)
dir = Direction.LD;
else if (!left && right && !up && down)
dir = Direction.RD;
else if (!left && !right && !up && !down)
dir = Direction.STOP;
}
/**
* 普通子弹发射
* @return 返回Missile类型
*/
public Missile fire() {
if(!live) return null;//如果坦克死了就不发子弹
if (dir == Direction.STOP) dir = Direction.R;
int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
Missile missile = new Missile(x, y, ptDir, good, this.tc);
tc.missiles.add(missile);
return missile;
}
/**
* 超级子弹发射
* @param dir 超级子弹发射方向
* @return 返回Missile类型
*/
public Missile fire(Direction dir) {
if(!live) return null;//如果坦克死了就不发子弹
if (dir == Direction.STOP) dir = Direction.R;
int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2;
Missile missile = new Missile(x, y, dir, good, this.tc);
tc.missiles.add(missile);
return missile;
}
/**
* 获得坦克矩形框,用于碰撞检测
* @return 返回Rectangle类型
*/
public Rectangle getRect() {
return new Rectangle(x, y, Tank.WIDTH, Tank.HEIGHT);
}
/**
* 得到坦克是否还活着
* @return 如果活着返回true,否则返回false
*/
public boolean isLive() {
return live;
}
/**
* 设置坦克的生死
* @param live 坦克的生死
*/
public void setLive(boolean live) {
this.live = live;
} /**
* 获得坦克是好是坏
* @return 好坦克返回true,坏坦克返回false
*/
public boolean isGood() {
return good;
}
/**
* 将坦克的位置设置到前一次出现的位子
*/
public void stay() {
this.x = oldX;
this.y = oldY;
}
/**
* 坦克撞墙
* @param w 墙
* @return 撞到则返回true,否则false
*/
public boolean collidesWithWall(Wall w) {
if(this.live && this.getRect().intersects(w.getRect())) {
this.stay();
return true;
}
return false;
}
/**
* 坦克相撞
*/
public boolean collidesWhitTank(Tank t) {
if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) {
this.stay();
return true;
}
return false;
}
/**
* 坦克之间不能穿越
*/
public boolean collidesWithTanks(java.util.List<Tank> tanks) {
for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
if(this != t) {
this.collidesWhitTank(t);
}
}
return false;
}
/**
*超级子弹发射
*/
public void superFire() {
Direction dir[] = Direction.values();
for(int i=0; i<dir.length; i++) {
this.fire(dir[i]);
}
}
/**
* 设置生命值
*/
public int getLife() {
return life;
}
/**
* 获得生命值
*/
public void setLife(int life) {
this.life = life;
}
/**
* 吃血块
*/
public void eatBlood(Blood b) {
if(this.live && b.isLive() && this.getRect().intersects( b.getRect())) {
this.life = 100;
b.setLive(false);
}
}
/**
* 血条类
*/
private class BloodBar {
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.drawRect(x, y-5, Tank.WIDTH, 5);
int w = Tank.WIDTH * life / 100;
g.setColor(Color.gray);
g.fillRect(x, y-5, w, 5);
g.setColor(c);
}
}
}
package com.xingda.TankWar;
import java.awt.*;
import java.util.List;/**
* 用于生成子弹
* @author xingda
*
*/
public class Missile {
/**
* 子弹横向移动速度
*/
public static final int XSPEED = 5;
/**
* 子弹纵向移动速度
*/
public static final int YSPEED = 5;
/**
* 子弹的宽度
*/
public static final int WIDTH = 10;
/**
* 子弹的高度
*/
public static final int HEIGHT = 10;
int x,y ;
Tank.Direction dir = Tank.Direction.STOP;
boolean left=false,right=false,up=false,down=false;
private boolean live = true;//子弹是否活着
private boolean good; //设置子弹是好是坏
TankClient tc = null;
public Missile(int x, int y, Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x, int y, Tank.Direction dir,boolean good, TankClient tc) {
this(x,y,dir);
this.tc = tc;
this.good = good;
}
/**
* 画子弹
*/
public void draw(Graphics g) {
if(!live) {
tc.missiles.remove(this);
return;
}//判断子弹是否活着,死了就不画了
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
/**
* 子弹移动
*/
public void move() {
switch(dir) {
case L : x-=XSPEED;break;
case R : x+=XSPEED;break;
case U : y-=XSPEED;break;
case D : y+=XSPEED;break;
case LU : x-=XSPEED;y-=YSPEED;break;
case RU : x+=XSPEED;y-=YSPEED;break;
case LD : x-=XSPEED;y+=YSPEED;break;
case RD : x+=XSPEED;y+=YSPEED;break;
case STOP : break;
}
if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
live = false;
tc.missiles.remove(this);
}
hitWall(tc.wall);
}
/**
* 得到子弹是否活着
*/
public boolean isLive() {
return live;
}
/**
* 获得子弹的矩形框,用于碰撞检测
*/
public Rectangle getRect() {
return new Rectangle(x, y, Missile.WIDTH, Missile.HEIGHT);
}
/**
* 打坦克
*/
public boolean hitTank( Tank t) {
if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {
if(t.isGood()) {
t.setLife(t.getLife() - 20);
if(t.getLife() <= 0 ) t.setLive(false);
}else {
t.setLive(false);
}
tc.enemyTanks.remove(t);
Explode e = new Explode(x, y, tc);
tc.explodes.add(e);
this.live = false;
return true;
}
return false;
}
/**
* 打集合中的多辆坦克
*/
public boolean hitTanks(List<Tank> enemyTanks) {
for(int i=0; i<enemyTanks.size(); i++) {
Tank t = enemyTanks.get(i);
if(hitTank(t)) {
return true;
}
}
return false;
} /**
* 打墙
* @param w
* @return 打到墙则返回true,否则返回false
*/
public boolean hitWall(Wall w) {
if(this.live && this.getRect().intersects(w.getRect())) {
this.live = false;
return true;
}
return false;
}
}
package com.xingda.TankWar;
import java.awt.*;/**
* 用于生成血条
* @author xingda
*
*/
public class Blood {
int x,y,w,h;
int step = 0;
private boolean live = true;
int pos[][] = {{200,100},{210,110},{220,120},{230,130},{240,140},{250,150},
{260,160},{270,170},{280,180},{290,190},{300,200},{310,210}
};
public Blood() {
w = 15;
h = 15;
}
/**
* 画血条
* @param g
*/
public void draw(Graphics g) {
if(!live) return;
if( step == pos.length) {
step = 0;
}
x = pos[step][0];
y = pos[step][1];
Color c = g.getColor();
g.setColor(Color.green);
g.fillRect(x, y, w, h);
g.setColor(c);
step++;
}
/**
* 用于碰撞检测
* @return Rectangle类型
*/
public Rectangle getRect() {
return new Rectangle(x,y,w,h);
} /**
* 获得是否活着
*/
public boolean isLive() {
return live;
} /**
* 设置是否有血条
*/
public void setLive(boolean live) {
this.live = live;
}
}
package com.xingda.TankWar;
import java.awt.*;/**
* 用于生成爆炸
* @author xingda
*
*/
public class Explode {
int x,y;
private boolean live = true;
private TankClient tc = null;
int[] diameter = {4,7,10,15,24,36,49,30,20,10,2};
int step = 0;//记录他画到第几步
public Explode(int x, int y, TankClient tc) {
this.x = x;
this.y = y;
this.tc = tc;
}
/**
* 画爆炸
*/
public void draw(Graphics g) {
if(!live) {
tc.explodes.remove(this);
return;
}
if(step == diameter.length - 1) {
live =false;
step = 0;
return;
}
Color c = g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, diameter[step], diameter[step]);
g.setColor(c);
step++;
}
}
package com.xingda.TankWar;
import java.awt.*;/**
* 用于生成墙
* @author xingda
*
*/
public class Wall {
int x,y;
int width ,height;
TankClient tc;
public Wall(int x,int y, int width, int height, TankClient tc) {
this.x = x;
this.y = y;
this.width =width;
this.height = height;
this.tc = tc;
}
/**
* 画墙
* @param g
*/
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.fillRect(x, y, width, height);
g.setColor(c);
}
/**
* 获得它的矩形框,用于碰撞检测
* @return Rectangle类型
*/
public Rectangle getRect() {
return new Rectangle(x, y, width, height);
}
}