这里我们处理一下坦克的击中效果,光击中坦克然后坦克消失总觉得缺了点意思,于是想到加个爆炸效果,实现方式也不复杂
爆炸是一个过程,我们可以创建一个爆炸类,每次”播放“不同的照片,照片数量足够时,就能形成爆炸效果
Boom类:
public class Boom {
// 位置
int x;
int y;
// 大小
int width = 20;
int height = 20;
// 爆炸数量
int boomNum = 8;
// 爆炸对象是否死亡
boolean is_Dead;
// 爆炸图片
Image BoomImage;
// set get
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getBoomNum() {
return boomNum;
}
public void setBoomNum(int boomNum) {
this.boomNum = boomNum;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public Image getBoomImage() {
return BoomImage;
}
public void setBoomImage(Image boomImage) {
BoomImage = boomImage;
}
// 构造函数
public Boom(int x, int y) {
super();
this.x = x;
this.y = y;
}
public void draw() {
switch (this.boomNum--) {
case 8:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast1.gif"));
break;
case 7:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast2.gif"));
break;
case 6:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast3.gif"));
break;
case 5:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast4.gif"));
break;
case 4:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast5.gif"));
break;
case 3:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast6.gif"));
break;
case 2:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast7.gif"));
break;
case 1:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast8.gif"));
break;
default:
setIs_Dead(true);// 数字走完则死亡
}
}
}
回到TankPanel类中,创建Boom的ArryList,每次有坦克死亡时,向里面添加爆炸
ArrayList booms = new ArrayList<>();// 爆炸
在绘制敌方坦克时,若死亡,则
booms.add(new Boom(etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y()));//添加爆炸
etanks.remove(i);
绘制爆炸
// 绘制爆炸
for (int i = 0; i < booms.size(); i++) {
if (booms.get(i).isIs_Dead()) {
booms.remove(i);
} else {
Boom boom = booms.get(i);
boom.draw();
g.drawImage(boom.getBoomImage(), boom.getX(), boom.getY(), boom.getWidth(), boom.getHeight(), this);
}
}
改动过的源码:
TankPanel:
public class TankPanel extends JPanel implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
MyTank mtank = new MyTank(100, 100, false);// 我方坦克
ArrayList etanks = new ArrayList<>();// 敌方坦克
ArrayList booms = new ArrayList<>();// 爆炸
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, TankFrame.Frame_Width, TankFrame.Frame_Height);
// 绘制我方坦克
mtank.draw();
g.drawImage(mtank.getTankImage(), mtank.getLocal_x(), mtank.getLocal_y(), mtank.getWidth(), mtank.getHeight(),
this);
mtank.move();
// 绘制我方子弹
for (int i = 0; i < mtank.getBullets().size(); i++) {
Bullet b = mtank.getBullets().get(i);
for (int j = 0; j < etanks.size(); j++) {
if (b.is_CollisonTank(etanks.get(j))) {
System.out.println("敌机被击中");
}
}
if (b.isIs_Dead() == true) {
mtank.getBullets().remove(i);// 死亡则移除
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height, this);
b.move();
}
}
// 绘制敌方坦克及子弹
for (int i = 0; i < etanks.size(); i++) {
if (etanks.get(i).isIs_Dead()) {
booms.add(new Boom(etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y()));//添加爆炸
etanks.remove(i);
} else {
etanks.get(i).draw();
g.drawImage(etanks.get(i).getTankImage(), etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y(),
etanks.get(i).getWidth(), etanks.get(i).getHeight(), this);
etanks.get(i).move();
// 绘制子弹
for (int j = 0; j < etanks.get(i).getBullets().size(); j++) {
Bullet b = etanks.get(i).getBullets().get(j);
if (b.is_CollisonTank(mtank)) {
System.out.println("我方被击中");
}
if (b.isIs_Dead()) {
etanks.get(i).getBullets().remove(j);
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height,
this);
b.move();
}
}
}
}
// 绘制爆炸
for (int i = 0; i < booms.size(); i++) {
if (booms.get(i).isIs_Dead()) {
booms.remove(i);
} else {
Boom boom = booms.get(i);
boom.draw();
g.drawImage(boom.getBoomImage(), boom.getX(), boom.getY(), boom.getWidth(), boom.getHeight(), this);
}
}
}
@Override
public void run() {
while (true) {
// 控制生成六只坦克
if (etanks.size() < 6) {
etanks.add(new EnemyTank());
}
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Boom:
public class Boom {
// 位置
int x;
int y;
// 大小
int width = 20;
int height = 20;
// 爆炸数量
int boomNum = 8;
// 爆炸对象是否死亡
boolean is_Dead;
// 爆炸图片
Image BoomImage;
// set get
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getBoomNum() {
return boomNum;
}
public void setBoomNum(int boomNum) {
this.boomNum = boomNum;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public Image getBoomImage() {
return BoomImage;
}
public void setBoomImage(Image boomImage) {
BoomImage = boomImage;
}
// 构造函数
public Boom(int x, int y) {
super();
this.x = x;
this.y = y;
}
public void draw() {
switch (this.boomNum--) {
case 8:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast1.gif"));
break;
case 7:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast2.gif"));
break;
case 6:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast3.gif"));
break;
case 5:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast4.gif"));
break;
case 4:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast5.gif"));
break;
case 3:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast6.gif"));
break;
case 2:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast7.gif"));
break;
case 1:
setBoomImage(Toolkit.getDefaultToolkit().getImage("blast8.gif"));
break;
default:
setIs_Dead(true);// 数字走完则死亡
}
}
}