日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/20 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 10 Android 植物人大战僵尸-生成僵尸 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 效果

2. 需求

每隔一定的時間在5個跑道中隨機生成僵尸,并且從右往左移動

3. 開發

  • 定義一個僵尸生成管理者,負責定時生成僵尸,這里定義的是每隔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;}@Overridepublic void drawSelf(Canvas canvas, Paint paint) {if (System.currentTimeMillis() - lastBirthTime > TIME) {lastBirthTime = System.currentTimeMillis();creatZombie();}}private void creatZombie() {// 游戲圖層加入僵尸GameView.getInstance().apply4CreatZombie();} }
  • 調用僵尸生成器
  • 在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);
  • 隨機生成一個僵尸
  • 其中跑道使用隨機函數完成,僵尸的生成坐標的定義

    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;}}}
  • 可以移動的僵尸子類
  • 這里定義了僵尸的動畫和移動速度

    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;}@Overridepublic 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

    總結

    以上是生活随笔為你收集整理的10 Android 植物人大战僵尸-生成僵尸的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。