10 Android 植物人大战僵尸-生成僵尸

1. 效果

僵尸

2. 需求

每隔一定的时间在5个跑道中随机生成僵尸,并且从右往左移动

3. 开发

  1. 定义一个僵尸生成管理者,负责定时生成僵尸,这里定义的是每隔15秒生成僵尸
package com.su.botanywarzombies.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.su.botanywarzombies.model.BaseModel;
import com.su.botanywarzombies.view.GameView;

/**
 * 僵尸生成器
 */
public class ZombieManager extends BaseModel {

    private int TIME = 15 * 1000;
    private long lastBirthTime = 0l;

    public ZombieManager() {
        this.isLive = true;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (System.currentTimeMillis() - lastBirthTime > TIME) {
            lastBirthTime = System.currentTimeMillis();
            creatZombie();
        }
    }

    private void creatZombie() {
        // 游戏图层加入僵尸
        GameView.getInstance().apply4CreatZombie();
    }
}
  1. 调用僵尸生成器

在onDrawing事件调用,但是要不要生成,根据是否满足15秒的定时周期

package com.su.botanywarzombies.view;

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    // 僵尸生成器
    private ZombieManager mZombieManager;

        private void onDrawing(Canvas mCanvas) {

            // 定时生成僵尸
            mZombieManager.drawSelf(mCanvas, mPaint);
  1. 随机生成一个僵尸

其中跑道使用随机函数完成,僵尸的生成坐标的定义

package com.su.botanywarzombies.view;

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    // 生产僵尸
    public void apply4CreatZombie() {
        synchronized (mSurfaceHolder) {
            int raceWay = 0;
            // 生成 0~4的跑道,向下强转取整
            raceWay = (int) (Math.random() * 5);

            switch (raceWay) {
            case 0:
                // Config.screenWidth + Config.zombieFlames[0].getWidth()
                // 为了僵尸提前走
                gameLayout4zombie0.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[0], raceWay));
                break;
            case 1:
                gameLayout4zombie1.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[1], raceWay));

                break;
            case 2:
                gameLayout4zombie2.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[2], raceWay));

                break;
            case 3:
                gameLayout4zombie3.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[3], raceWay));

                break;
            case 4:
                gameLayout4zombie4.add(new Zombie(Config.screenWidth + Config.zombieFlames[0].getWidth(), Config.racWayYpoint[4], raceWay));

                break;

            default:
                break;
            }
        }
    }
  1. 可以移动的僵尸子类

这里定义了僵尸的动画和移动速度

package com.su.botanywarzombies.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.su.botanywarzombies.constant.Config;
import com.su.botanywarzombies.model.BaseModel;

/**
 * 僵尸类
 */
public class Zombie extends BaseModel {

    // 跑道,有碰撞监测
    private int raceWay;

    // 移动的动画帧
    private int farmeIndex;

    // 僵尸移动的速度
    private int seepX = 1;

    public Zombie(int locationX, int locationY, int raceWay) {
        this.locationX = locationX;
        this.locationY = locationY;
        this.raceWay = raceWay;
        this.isLive = true;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        if (isLive) {
            canvas.drawBitmap(Config.zombieFlames[farmeIndex], locationX, locationY, paint);
            farmeIndex = (++farmeIndex) % 7;

            locationX = locationX - seepX;
        }
    }
}

4. Demo下载

https://github.com/sufadi/BotanyWarZombies

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值