📄 bullet.java
字号:
package com.ccu.whj;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.Toolkit;
/**
* 子弹类
* @author 王洪吉
*
*/
public class Bullet {
//子弹的位置
private int bx = 120, by = 120;
//子弹的大小(静态常量)
private static final int B_WIDTH = 10,B_HEIGHT = 10;
//子弹速度(静态常量)
private static final int BSPEED = 15;
//子弹是否健在
private boolean live = true;
//子弹的移动方向
Direction bDir;
//取得大管家的引用
private TankWarClient tc;
//子弹是谁发射的,我方还是敌方,我方good == true
private boolean good = true;
//将使用Toolkit中的方法把硬盘上的图片拿到内存里来
private static Toolkit tk = Toolkit.getDefaultToolkit();
private static Image[] bulletImgs = null;
static {
bulletImgs = new Image[] {
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletL.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletLU.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletU.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletRU.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletR.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletRD.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletD.gif")),
tk.getImage(Bullet.class.getClassLoader().getResource("images/bulletLD.gif"))
};
}
public Bullet(int x, int y, Direction dir) {
this.bx = x;
this.by = y;
this.bDir = dir;
}
public Bullet(int x, int y, Direction dir, TankWarClient tc) {
this(x,y,dir);
this.tc = tc;
}
public Bullet(int x, int y, Direction dir, TankWarClient tc, boolean good) {
this(x,y,dir,tc);
this.good = good;
}
public void draw(Graphics g) {
if(!live) return;
switch(bDir) {
case L:
g.drawImage(bulletImgs[0], bx, by, null);
break;
case LU:
g.drawImage(bulletImgs[1], bx, by, null);
break;
case U:
g.drawImage(bulletImgs[2], bx, by, null);
break;
case RU:
g.drawImage(bulletImgs[3], bx, by, null);
break;
case R:
g.drawImage(bulletImgs[4], bx, by, null);
break;
case RD:
g.drawImage(bulletImgs[5], bx, by, null);
break;
case D:
g.drawImage(bulletImgs[6], bx, by, null);
break;
case LD:
g.drawImage(bulletImgs[7], bx, by, null);
break;
}
move();
}
public void move() {
switch(bDir) {
case L:
bx -= BSPEED;
break;
case LU:
by -= BSPEED;
bx -= BSPEED;
break;
case U:
by -= BSPEED;
break;
case RU:
by -= BSPEED;
bx += BSPEED;
break;
case R:
bx += BSPEED;
break;
case RD:
by += BSPEED;
bx += BSPEED;
break;
case D:
by += BSPEED;
break;
case LD:
by += BSPEED;
bx -= BSPEED;
break;
}
/*
* 子弹出界则消失
*/
if(bx<0 || bx>800 || by<0 || by>600) {
tc.bullets.remove(this);
}
}
/**
* 子弹击中坦克的实现
* @param t 被击中的坦克
* @return 击中则返回true,否则返回false
*/
public boolean hitTank(Tank t) {
/*
* 碰撞检测,并且判断双方均为live状态,且双方互为敌人
*/
if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) {
/*
* 当击中我方坦克时,扣血,直至0血死亡
*/
if(t.isGood()) {
int i = t.getLife();
i = i - 8;
t.setLife(i);
if(i <= 0) {
t.setLive(false);
}
this.live = false;
} else {
/*
* 击中敌方boss扣血,0血死亡
*/
if(t.isKing()) {
int i = t.getKingLife();
i = i - 1;
t.setKingLife(i);
if(i <= 0) {
t.setLive(false);
tc.score += 100;
}
} else {
/*
* 击中敌方普通小兵,直接打死
*/
t.setLive(false);
tc.score += 1;
}
//击中任何坦克,子弹都应死亡
this.live = false;
}
//产生爆炸
tc.explodes.add(new Explode(t.getX(), t.getY(),tc));
return true;
}
return false;
}
/**
* 用以实现使子弹能够打击所有容器内的坦克
* @param tanks 容器类的实例
* @return 击中则返回true,否则false
*/
public boolean hitTanks(List<Tank> tanks) {
for(int i = 0; i < tanks.size(); i++) {
Tank t = tanks.get(i);
if(hitTank(t)) {
return true;
}
}
return false;
}
/**
*
* @param w 被撞的墙
* @return 子弹撞墙则返回true,否则false
*/
public boolean hitWall(Wall w) {
if(this.getRect().intersects(w.getRect())) {
this.live = false;
return true;
}
return false;
}
/**
* 碰撞检测,得先把对象的矩形都拿到
* @return 子弹的矩形范围
*/
public Rectangle getRect() {
return new Rectangle(bx,by,B_WIDTH,B_HEIGHT);
}
/**
* 子弹与子弹相撞
* @param b 被击中的子弹
* @return 击中则返回true,否则false
*/
public boolean hitBullet(Bullet b) {
if(this.live && this.getRect().intersects(b.getRect()) && b.live && this.good != b.isGood()) {
this.live = false;
return true;
}
return false;
}
/**
* 用以实现与所有子弹都能相撞
* @param bullets 容器类的实例
* @return 相撞则返回true,否则false
*/
public boolean hitBullets(List<Bullet> bullets) {
for(int i = 0; i < bullets.size(); i++) {
Bullet b = bullets.get(i);
if(hitBullet(b)) {
return true;
}
}
return false;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isGood() {
return good;
}
public int getBx() {
return bx;
}
public int getBy() {
return by;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -