java飞机大战敌机损毁代码
时间: 2024-01-22 21:02:56 浏览: 122
JAVA飞机大战代码
根据提供的引用内容,Java飞机大战敌机损毁代码应该包括以下几个部分:
1. 定义敌机类(Enemy)和爆炸类(Explode)。
```java
public class Enemy {
// 敌机的属性和方法
}
public class Explode {
private int x;
private int y;
private boolean active;
private List<Image> destroyImages;
public Explode(int x, int y, List<Image> destroyImages) {
this.x = x;
this.y = y;
this.active = true;
this.destroyImages = destroyImages;
}
public void draw(Graphics g) {
if (active) {
// 绘制爆炸效果
}
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
```
2. 在敌机类中添加碰撞检测方法,当敌机与子弹碰撞时,创建一个爆炸对象,并将其添加到爆炸效果列表中。
```java
public class Enemy {
private int x;
private int y;
private int width;
private int height;
private int speed;
private int blood;
private boolean active; private List<Image> images;
private Explode explode;
public Enemy(int x, int y, int width, int height, int speed, int blood, List<Image> images) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.blood = blood;
this.active = true;
this.images = images;
}
public void draw(Graphics g) {
if (active) {
// 绘制敌机
} else {
explode.draw(g);
}
}
public void move() {
// 敌机移动的方法
}
public boolean isCollideWith(Bullet bullet) {
// 判断敌机是否与子弹碰撞
}
public void hit() {
blood--;
if (blood <= 0) {
active = false;
explode = new Explode(x, y, destroyImages);
}
}
}
```
3. 在游戏主循环中,遍历爆炸效果列表,绘制所有的爆炸效果,并将已经播放完毕的爆炸效果从列表中移除。
```java
public class Game {
private List<Enemy> enemies;
private List<Explode> explodes;
public void run() {
while (true) {
// 游戏主循环
for (Enemy enemy : enemies) {
for (Bullet bullet : bullets) {
if (enemy.isCollideWith(bullet)) {
enemy.hit();
}
}
}
for (Explode explode : explodes) {
explode.draw(g);
if (!explode.isActive()) {
explodes.remove(explode);
}
}
}
}
}
```
阅读全文