Android Canvas打飞机之被打中的敌机

上一篇文章已经是去年的事情了,去年我们把主角和敌机都已经完美的绘制到了画布,并且做了对应的移动动画和手势操作效果,敌机和主角都配置了对应的武器,但是还没能发挥效果。所以今天这里就来实现一下主角子弹打中敌机,敌机死亡并且显示爆炸效果。

device-2017-01-05-170825.png
1.既然要有死亡爆炸效果就需要实现一个爆炸的类,新建一个DrawBoom类。
package com.tangyx.game.holder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

import com.tangyx.game.factory.Animation;
import com.tangyx.game.util.ScreenUtils;

/**
 * Created by tangyx on 2017/1/5.
 *
 */

public class DrawBoom extends DrawGame {

    final static int TYPE_A=1;
    final static int TYPE_B=2;
    private float mBoomX, mBoomY;
    private float mSpeedX, mSpeedY;
    private float mAddX = 0.03f;
    private float mAddY =0.03f;
    /**
     * 爆炸效果慢镜头时长,不然可能看不见效果就消失了。
     */
    private int mBoomDuration =30;//慢劲头特效
    /**
     * 爆炸动画
     */
    private Animation mAnimation;
    /**
     * 爆炸类型
     */
    public int mBoomType;
    /**
     * 爆炸图
     */
    private Bitmap bitmap;
    private Matrix mMatrix;
    private float mMaxScale = -1;
    private boolean isEnd;
    private int mCurrentFrameIndex = 0;

    public DrawBoom(Context context, Bitmap[] deadBitmap, float x, float y, int duration, int type) {
        super(context);
        mAnimation = new Animation(deadBitmap, false);
        this.mBoomX = x;
        this.mBoomY = y;
        this.mBoomDuration = duration;
        this.mBoomType = type;
    }

    public DrawBoom(Context context, Bitmap deadBitmap, float x, float y, int duration, boolean isWidth, int type) {
        super(context);
        this.bitmap = deadBitmap;
        this.mBoomX = x;
        this.mBoomY = y;
        mMatrix = new Matrix();
        this.mBoomDuration = duration;
        if(isWidth){//最大膨胀到全屏宽度
            mMaxScale = ((float) ScreenUtils.getScreenWidth(getContext()))/deadBitmap.getWidth();
        }
        this.mBoomType = type;
    }

    @Override
    void onDraw(Canvas canvas) {
        if(mBoomType ==TYPE_A){
            mAnimation.DrawAnimation(canvas, mPaint, mBoomX, mBoomY);
        }else if(mBoomType ==TYPE_B){
            float bmw,bx,by;
            Bitmap bm = getBitmap();
            bmw= mSpeedX *100;
            if(mBoomX >=ScreenUtils.getScreenWidth(getContext())/2){
                bmw = bm.getWidth()/2;
            }
            bx = mBoomX -bmw;//偏移计算
            by = mBoomY -bm.getHeight()/2;//偏移计算
            canvas.drawBitmap(bm,bx,by,mPaint);
        }
    }

    /**
     * 图片由小变大
     * @return
     *
     */
    private Bitmap getBitmap(){
        mSpeedX += mAddX;
        mSpeedY += mAddY;
        if(mMaxScale !=-1&& mSpeedX >= mMaxScale){
            isEnd =true;
        }
        mMatrix.reset();
        mMatrix.postScale(mSpeedX, mSpeedY);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mMatrix, true);
    }

    @Override
    void updateGame() {
        if(mCurrentFrameIndex < mBoomDuration){
            mCurrentFrameIndex++;
            if(mAnimation !=null&& mAnimation.mIsend){
                mAnimation.reset();
            }
        }else{
            mCurrentFrameIndex =0;
            isEnd = true;
        }
    }

    public boolean isEnd() {
        return isEnd;
    }
}

代码不多,只提供了两种爆炸模式,一种是持续暴涨效果,一种是单一爆炸效果。

2.上面的代码用到了一个Animation的类,是一个自定义的类,主要用于持续播放爆炸效果,一帧一帧的播放。
package com.tangyx.game.factory;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

/**
 * Created by tangyx on 2017/1/5.
 */

public class Animation {
    /** 上一帧播放时间 **/
    private long mLastPlayTime = 0;
    /** 播放当前帧的ID **/
    private int mPlayID = 0;
    /** 动画frame数量 **/
    private int mFrameCount = 0;
    /** 用于储存动画资源图片 **/
    private Bitmap[] mframeBitmap = null;
    /** 是否循环播放 **/
    private boolean mIsLoop = false;
    /** 播放结束 **/
    public boolean mIsend = false;
    /** 动画播放间隙时间 **/
    private static final int ANIM_TIME = 100;

    public Animation(Context context, int [] frameBitmapID, boolean isloop) {
        mFrameCount = frameBitmapID.length;
        mframeBitmap = new Bitmap[mFrameCount];
        mIsLoop = isloop;
    }

    public Animation(Bitmap [] frameBitmap, boolean isloop) {
        mFrameCount = frameBitmap.length;
        mframeBitmap = frameBitmap;
        mIsLoop = isloop;
    }

    //重置动画
    public void reset() {
        mLastPlayTime = 0;
        mPlayID =0;
        mIsend= false;
    }

    /**
     * 绘制动画中的其中一帧
     * @param paint
     * @param x
     * @param y
     * @param frameID
     */
    public void DrawFrame(Canvas canvas, Paint paint, int x, int y, int frameID) {
        canvas.drawBitmap(mframeBitmap[frameID], x, y, paint);
    }


    /**
     * 绘制动画
     * @param paint
     * @param x
     * @param y
     */
    public void DrawAnimation(Canvas canvas, Paint paint, float x, float y) {
        //如果没有播放结束则继续播放
        if (!mIsend) {
            canvas.drawBitmap(mframeBitmap[mPlayID], x, y, paint);
            long time = System.currentTimeMillis();
            if (time - mLastPlayTime > ANIM_TIME) {
                mPlayID++;
                mLastPlayTime = time;
                if (mPlayID >= mFrameCount) {
                    //标志动画播放结束
                    mIsend = true;
                    if (mIsLoop) {//设置循环播放
                        mIsend = false;
                        mPlayID = 0;
                    }
                }
            }
        }
    }
}
3.打开敌机类DrawEnemy新增一个方法用于判断敌机是否和主角的子弹进行碰撞。
Paste_Image.png

主角每一颗子弹可以看做一个运动的对象,敌机也是一个运动对象,每次绘制敌机和子弹的时候调用该方法在屏幕的位置来判断出现坐标重叠,如果重叠表示敌机被打中,当敌机被打中时会有一个闪烁的效果(isCollision变量),表示敌机打中了但是并没有死亡(比如高级将领有10滴血)

4.回到GameView类新增一个判断敌机是否和主角碰撞的方法。
Paste_Image.png
5.循环绘制主角子弹,把每一颗子弹都传入上面的方法,循环绘制敌机判断是否和该子弹进行了碰撞。在GameView类的addPlayerBullet循环绘制方法中调用。
Paste_Image.png

主角子弹一旦和敌机碰撞,主角该子弹立刻死亡并且移除画布。
敌机被击中,敌机掉一滴血(现在敌机基本都是一滴血),如果敌机血量<=0敌机死亡并且移除画布。

6.其中有爆炸资源和一个音效,是为了保证游戏的体验,当敌机死亡时候显示爆炸效果和爆炸声音。

在GameView类的initGame方法中初始化音效。

//初始化音效
GameSoundPool.getInstance(getContext()).addMusic(GameSoundPool.ENEMYCLEARA, R.raw.enemyclear1,1);
GameSoundPool.getInstance(getContext()).addMusic(GameSoundPool.ENEMYCLEARB, R.raw.enemyclear2,1);
GameSoundPool.getInstance(getContext()).addMusic(GameSoundPool.ENEMYCLEARC, R.raw.enemydie,1);

爆炸资源的初始化

//初始化爆炸资源
mBoom01 = BitmapUtils.ReadBitMap(getContext(), R.drawable.boom_1);
Bitmap temp = BitmapUtils.ReadBitMap(getContext(), R.drawable.boom_0);
mEneyBoom01 = BitmapUtils.widthSplit(temp,6);
temp = BitmapUtils.ReadBitMap(getContext(), R.drawable.boom_2);
mEneyBoom02 = BitmapUtils.widthSplit(temp,6);
mBooms = new ArrayList<>();```

######7.在onGameDraw中绘制爆炸的效果

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/3982371-79a06d0b1e692a72.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

主角子弹和敌机碰撞的内容不是很多,主要实现逻辑和动画效果比较麻烦,如果需要更多的爆炸效果,可以自行拓展DrawBoom类。
这里敌机是可以被消灭了,但是主角还是无敌的,对于公平公正来说这明显不符合逻辑,所以下一篇就是针对主角挨炮的处理以及设置主角的血量增减。

<a href="https://github.com/tangyxgit/GameCanvas">持续更新的源码</a>


![enemy.gif](http://upload-images.jianshu.io/upload_images/3982371-e4b4eae6c952a055.gif?imageMogr2/auto-orient/strip)

<a href="http://www.jianshu.com/p/b4c2d19a4f9f">上一篇</a>     <a href="http://www.jianshu.com/p/e77701c67f47">下一篇</a>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容