坦克大战—day 15

爆炸效果的调整

package game;

import java.awt.*;

public class Explode {
    public static final int EXPLODE_FRAME_COUNT = 12;
    //爆炸效果的宽和高
    private static int explodeWidth;
    private static int explodeHeight;

    private static Image[] img;

    static {
        img = new Image[EXPLODE_FRAME_COUNT/4];
        for (int i = 0; i < img.length; i++) {
            img[i] = Toolkit.getDefaultToolkit().createImage("res/boom_" + i + ".png");
        }
        explodeWidth = img[0].getWidth(null)/2;
        explodeHeight = img[0].getHeight(null);
    }
    //爆炸的效果的坐标
    private int x, y;
    //当前播放的帧的下标0-11
    private int index;
    //是否可见
    private boolean visible=true;

    public Explode(int x,int y) {
        this.x = x;
        this.y = y;
    }

    public void draw(Graphics g) {
        if (explodeHeight < 0){
            explodeHeight = img[0].getHeight(null);
            explodeWidth = img[0].getWidth(null)>>1;
        }
        if (!visible) return;
        g.drawImage(img[index/4],x - explodeWidth,y- explodeHeight,null);
        index++;
        //播放完最后一帧,设置不可见
         if(index>=EXPLODE_FRAME_COUNT)
             visible=false;
    }

    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 getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
}
//坦克和子弹碰撞的方法
    public void collideBullets(List<Bullet> bullets)
    {
        for (Bullet bullet:bullets)
        {
            int bulletX = bullet.getX();
            int bulletY = bullet.getY();
            if(MyUtil.isCollide(x,y,RADIUS,bullet.getX(),bullet.getY()))
            {
                //子弹消失
                bullet.setVisible(false);
                //坦克受到伤害
                //添加爆炸效果
                 explodes.add(new Explode(x,y+RADIUS));
            }
        }
    }

 

上一篇:一个vue2.0+vuex+vue-router搭建的单页潮流购物网站


下一篇:回到顶部带动画