Java.坦克大战小游戏【1.7】

任务

代码
新增 Explode 类

import java.awt.Color;
import java.awt.Graphics;

public class Explode {
    int x,y;
    int[] diameter = {4 ,7 ,12 ,18 ,26 ,32 ,49 ,30 ,14 ,6};
    private boolean live = true;
    int step = 0;
    private TankClient tc;

    public Explode(int x, int y, TankClient tc) {
        super();
        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) {
            step = 0;
            live = false;
            return;
        }
        Color c = g.getColor();
        g.setColor(Color.ORANGE);
        g.fillOval(x, y, diameter[step], diameter[step]);
        g.setColor(c);
        step++;
    }

}

在 TankClient 类中创建 Explode 类实例集合,并在 paint() 方法调用 draw() 方法让爆炸自己把自己画出来

List<Explode> explodes = new ArrayList<Explode>();
for(int i=0;i<explodes.size();i++) {
            Explode e = explodes.get(i);
            e.draw(g);
}

击毙坦克后往爆炸集合中添加爆炸,在 Missile 类的 hitTank() 中新增,当击中坦克,new 一个爆炸

tc.explodes.add(new Explode(x, y, tc));

猜你喜欢

转载自blog.csdn.net/liyuanyue2017/article/details/80260188
今日推荐