自慰android向游戏,Android实战打飞机游戏之怪物(敌机)类的实现(4)

本文详细介绍了如何在Android平台上开发一款飞行射击游戏,特别是针对敌人(敌机)的实现,包括敌机的种类、运动逻辑、绘制方法和碰撞检测。通过实例代码展示了不同类型的敌机如苍蝇和鸭子的移动轨迹,以及主角与敌机的碰撞处理,包括无敌状态和血量管理。文章提供了关键类如`Enemy`和`Player`的实现细节,帮助开发者了解游戏逻辑的构建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看看效果图:

f4580bb28ae9b19a0df5f1c5a41837d3.gif

分析:根据敌机类型区分 敌机 运动逻辑 以及绘制

/**

* 敌机

*

* @author liuml

* @time 2016-5-31 下午4:14:59

*/

public class Enemy {

// 敌机的种类标识

public int type;

// 苍蝇

public static final int TYPE_FLY = 1;

// 鸭子(从左往右运动)

public static final int TYPE_DUCKL = 2;

// 鸭子(从右往左运动)

public static final int TYPE_DUCKR = 3;

// 敌机图片资源

public Bitmap bmpEnemy;

// 敌机坐标

public int x, y;

// 敌机每帧的宽高

public int frameW, frameH;

// 敌机当前帧下标

private int frameIndex;

// 敌机的移动速度

private int speed;;

// 判断敌机是否已经出屏

public boolean isDead;

// 敌机的构造函数

public Enemy(Bitmap bmpEnemy, int enemyType, int x, int y) {

this.bmpEnemy = bmpEnemy;

frameW = bmpEnemy.getWidth() / 10;

frameH = bmpEnemy.getHeight();

this.type = enemyType;

this.x = x;

this.y = y;

// 不同种类的敌机血量不同

switch (type) {

// 苍蝇

case TYPE_FLY:

speed = 25;

break;

// 鸭子

case TYPE_DUCKL:

speed = 3;

break;

case TYPE_DUCKR:

speed = 3;

break;

}

}

// 敌机绘图函数

public void draw(Canvas canvas, Paint paint) {

canvas.save();

canvas.clipRect(x, y, x + frameW, y + frameH);

canvas.drawBitmap(bmpEnemy, x - frameIndex * frameW, y, paint);

canvas.restore();

}

// 敌机逻辑AI

public void logic() {

// 不断循环播放帧形成动画

frameIndex++;

if (frameIndex >= 10) {

frameIndex = 0;

}

// 不同种类的敌机拥有不同的AI逻辑

switch (type) {

case TYPE_FLY:

if (isDead == false) {

// 减速出现,加速返回

speed -= 1;

y += speed;

if (y <= -200) {

isDead = true;

}

}

break;

case TYPE_DUCKL:

if (isDead == false) {

// 斜右下角运动

x += speed / 2;

y += speed;

if (x > MySurfaceView.screenW) {

isDead = true;

}

}

break;

case TYPE_DUCKR:

if (isDead == false) {

// 斜左下角运动

x -= speed / 2;

y += speed;

if (x < -50) {

isDead = true;

}

}

break;

}

}

}

在MySurfaceView 中 生成敌机

public class MySurfaceView extends SurfaceView implements Callback, Runnable {

private SurfaceHolder sfh;

private Paint paint;

private Thread th;

private boolean flag;

private Canvas canvas;

// 1 定义游戏状态常量

public static final int GAME_MENU = 0;// 游戏菜单

public static final int GAMEING = 1;// 游戏中

public static final int GAME_WIN = 2;// 游戏胜利

public static final int GAME_LOST = 3;// 游戏失败

public static final int GAME_PAUSE = -1;// 游戏菜单

// 当前游戏状态(默认初始在游戏菜单界面)

public static int gameState = GAME_MENU;

// 声明一个Resources实例便于加载图片

private Resources res = this.getResources();

// 声明游戏需要用到的图片资源(图片声明)

private Bitmap bmpBackGround;// 游戏背景

private Bitmap bmpBoom;// 爆炸效果

private Bitmap bmpBoosBoom;// Boos爆炸效果

private Bitmap bmpButton;// 游戏开始按钮

private Bitmap bmpButtonPress;// 游戏开始按钮被点击

private Bitmap bmpEnemyDuck;// 怪物鸭子

private Bitmap bmpEnemyFly;// 怪物苍蝇

private Bitmap bmpEnemyBoos;// 怪物猪头Boos

private Bitmap bmpGameWin;// 游戏胜利背景

private Bitmap bmpGameLost;// 游戏失败背景

private Bitmap bmpPlayer;// 游戏主角飞机

private Bitmap bmpPlayerHp;// 主角飞机血量

private Bitmap bmpMenu;// 菜单背景

public static Bitmap bmpBullet;// 子弹

public static Bitmap bmpEnemyBullet;// 敌机子弹

public static Bitmap bmpBossBullet;// Boss子弹

public static int screenW;

public static int screenH;

// 声明一个敌机容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值