Android实战 * 游戏之怪物(敌机)类的实现(4)
作者:liudao7994 发布时间:2021-07-26 09:13:41
标签:Android, , ,游戏,敌机
先看看效果图:
分析: 根据敌机类型区分 敌机 运动逻辑 以及绘制
/**
* 敌机
*
* @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;
// 声明一个敌机容器
private Vector<Enemy> vcEnemy;
// 每次生成敌机的时间(毫秒)
private int createEnemyTime = 50;
private int count;// 计数器
// 敌人数组:1和2表示敌机的种类,-1表示Boss
// 二维数组的每一维都是一组怪物
private int enemyArray[][] = { { 1, 2 }, { 1, 1 }, { 1, 3, 1, 2 },
{ 1, 2 }, { 2, 3 }, { 3, 1, 3 }, { 2, 2 }, { 1, 2 }, { 2, 2 },
{ 1, 3, 1, 1 }, { 2, 1 }, { 1, 3 }, { 2, 1 }, { -1 } };
// 当前取出一维数组的下标
private int enemyArrayIndex;
// 是否出现Boss标识位
private boolean isBoss;
// 随机库,为创建的敌机赋予随即坐标
private Random random;
//
private GameMenu gameMenu;
private GameBg gameBg;
private Player player;
/**
* SurfaceView初始化函数
*/
public MySurfaceView(Context context) {
super(context);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
setFocusable(true);
}
/**
* SurfaceView视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
initGame();
flag = true;
// 实例线程
th = new Thread(this);
// 启动线程
th.start();
}
/**
* 加载游戏资源
*/
private void initGame() {
// 加载游戏资源
bmpBackGround = BitmapFactory
.decodeResource(res, R.drawable.background);
bmpBoom = BitmapFactory.decodeResource(res, R.drawable.boom);
bmpBoosBoom = BitmapFactory.decodeResource(res, R.drawable.boos_boom);
bmpButton = BitmapFactory.decodeResource(res, R.drawable.button);
bmpButtonPress = BitmapFactory.decodeResource(res,
R.drawable.button_press);
bmpEnemyDuck = BitmapFactory.decodeResource(res, R.drawable.enemy_duck);
bmpEnemyFly = BitmapFactory.decodeResource(res, R.drawable.enemy_fly);
bmpEnemyBoos = BitmapFactory.decodeResource(res, R.drawable.enemy_pig);
bmpGameWin = BitmapFactory.decodeResource(res, R.drawable.gamewin);
bmpGameLost = BitmapFactory.decodeResource(res, R.drawable.gamelost);
bmpPlayer = BitmapFactory.decodeResource(res, R.drawable.player);
bmpPlayerHp = BitmapFactory.decodeResource(res, R.drawable.hp);
bmpMenu = BitmapFactory.decodeResource(res, R.drawable.menu);
bmpBullet = BitmapFactory.decodeResource(res, R.drawable.bullet);
bmpEnemyBullet = BitmapFactory.decodeResource(res,
R.drawable.bullet_enemy);
bmpBossBullet = BitmapFactory
.decodeResource(res, R.drawable.boosbullet);
// 菜单类实例化
gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
// 实例游戏背景
gameBg = new GameBg(bmpBackGround);
// 实例主角
player = new Player(bmpPlayer, bmpPlayerHp);
// 实例敌机容器
vcEnemy = new Vector<Enemy>();
// 实例随机库
random = new Random();
}
/**
* 游戏绘图
*/
public void myDraw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.WHITE);
// 绘图函数根据游戏状态不同进行不同绘制
switch (gameState) {
case GAME_MENU:
gameMenu.draw(canvas, paint);
break;
case GAMEING:
gameBg.draw(canvas, paint);
player.draw(canvas, paint);
if (isBoss == false) {
// 敌机绘制
for (int i = 0; i < vcEnemy.size(); i++) {
vcEnemy.elementAt(i).draw(canvas, paint);
}
} else {
// boss 绘制
}
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
case GAME_PAUSE:
break;
default:
break;
}
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
}
/**
* 触屏事件监听
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (gameState) {
case GAME_MENU:
gameMenu.onTouchEvent(event);
break;
case GAMEING:
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
case GAME_PAUSE:
break;
}
return true;
}
/**
* 按键事件监听
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (gameState) {
case GAME_MENU:
break;
case GAMEING:
player.onKeyDown(keyCode, event);
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
case GAME_PAUSE:
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (gameState) {
case GAME_MENU:
break;
case GAMEING:
player.onKeyUp(keyCode, event);
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
case GAME_PAUSE:
break;
}
return super.onKeyUp(keyCode, event);
}
/**
* 游戏逻辑
*/
private void logic() {
switch (gameState) {
case GAME_MENU:
break;
case GAMEING:
gameBg.logic();
player.logic();
// 敌机逻辑
if (isBoss == false) {
// 敌机逻辑
for (int i = 0; i < vcEnemy.size(); i++) {
Enemy en = vcEnemy.elementAt(i);
// 因为容器不断添加敌机 ,那么对敌机isDead判定,
// 如果已死亡那么就从容器中删除,对容器起到了优化作用;
if (en.isDead) {
vcEnemy.removeElementAt(i);
} else {
en.logic();
}
}
// 生成敌机
count++;
if (count % createEnemyTime == 0) {
for (int i = 0; i < enemyArray[enemyArrayIndex].length; i++) {
// 苍蝇
if (enemyArray[enemyArrayIndex][i] == 1) {
int x = random.nextInt(screenW - 100) + 50;
vcEnemy.addElement(new Enemy(bmpEnemyFly, 1, x, -50));
// 鸭子左
} else if (enemyArray[enemyArrayIndex][i] == 2) {
int y = random.nextInt(20);
vcEnemy.addElement(new Enemy(bmpEnemyDuck, 2, -50,
y));
// 鸭子右
} else if (enemyArray[enemyArrayIndex][i] == 3) {
int y = random.nextInt(20);
vcEnemy.addElement(new Enemy(bmpEnemyDuck, 3,
screenW + 50, y));
}
}
// 这里判断下一组是否为最后一组(Boss)
if (enemyArrayIndex == enemyArray.length - 1) {
isBoss = true;
} else {
enemyArrayIndex++;
}
}
}
break;
case GAME_WIN:
break;
case GAME_LOST:
break;
case GAME_PAUSE:
break;
}
}
@Override
public void run() {
while (flag) {
long start = System.currentTimeMillis();
myDraw();
logic();
long end = System.currentTimeMillis();
try {
if (end - start < 50) {
Thread.sleep(50 - (end - start));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* SurfaceView视图状态发生改变,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
/**
* SurfaceView视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
}
碰撞检测
修改Player类
package com.gsf;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.KeyEvent;
public class Player {
private int playerHp = 3;
private Bitmap bmpPlayerHP;
// 主角坐标以及位图
private int x, y;
private Bitmap bmpPlayer;
// 主角移动速度
private int speed = 5;
// 主角移动标识
private boolean isUp, isDown, isLeft, isRight;
// 主角的构造函数
public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp) {
this.bmpPlayer = bmpPlayer;
this.bmpPlayerHP = bmpPlayerHp;
// 飞机初始位置
x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;
y = MySurfaceView.screenH - bmpPlayer.getHeight();
}
// 主角游戏绘制方法
public void draw(Canvas canvas, Paint paint) {
// 绘制主角
canvas.drawBitmap(bmpPlayer, x, y, paint);
// 绘制血量
for (int i = 0; i < playerHp; i++) {
canvas.drawBitmap(bmpPlayerHP, i * bmpPlayerHP.getWidth(),
MySurfaceView.screenH - bmpPlayerHP.getHeight(), paint);
}
}
/**
* 按键事件监听
*/
public void onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
isUp = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
isDown = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
isLeft = true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
isRight = true;
}
}
public void onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
isUp = false;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
isDown = false;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
isLeft = false;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
isRight = false;
}
}
/**
* 游戏逻辑
*/
public void logic() {
if (isUp) {
y -= speed;
}
if (isDown) {
y += speed;
}
if (isLeft) {
x -= speed;
}
if (isRight) {
x += speed;
}
// 判断屏幕X边界
if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
x = MySurfaceView.screenW - bmpPlayer.getWidth();
} else if (x <= 0) {
x = 0;
}
// 判断屏幕Y边界
if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
y = MySurfaceView.screenH - bmpPlayer.getHeight();
} else if (y <= 0) {
y = 0;
}
}
//设置主角血量
public void setPlayerHp(int hp) {
this.playerHp = hp;
}
//获取主角血量
public int getPlayerHp() {
return playerHp;
}
//判断碰撞(敌机与主角 * 碰撞)
public boolean isCollsionWith(Enemy bullet) {
int x2 = bullet.x;
int y2 = bullet.y;
int w2 = bullet.frameW;
int h2 = bullet.frameH;
if (x >= x2 && x >= x2 + w2) {
return false;
} else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
return false;
} else if (y >= y2 && y >= y2 + h2) {
return false;
} else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
return false;
}
//发生碰撞,让其死亡
//isDead = true;
return true;
}
}
在MySurface中 加上碰撞逻辑
/**
* 游戏逻辑
*/
private void logic() {
switch (gameState) {
case GAME_MENU:
break;
case GAMEING:
gameBg.logic();
player.logic();
// 敌机逻辑
if (isBoss == false) {
// 敌机逻辑
for (int i = 0; i < vcEnemy.size(); i++) {
Enemy en = vcEnemy.elementAt(i);
// 因为容器不断添加敌机 ,那么对敌机isDead判定,
// 如果已死亡那么就从容器中删除,对容器起到了优化作用;
if (en.isDead) {
vcEnemy.removeElementAt(i);
} else {
en.logic();
}
}
// 生成敌机
count++;
if (count % createEnemyTime == 0) {
for (int i = 0; i < enemyArray[enemyArrayIndex].length; i++) {
// 苍蝇
if (enemyArray[enemyArrayIndex][i] == 1) {
int x = random.nextInt(screenW - 100) + 50;
vcEnemy.addElement(new Enemy(bmpEnemyFly, 1, x, -50));
// 鸭子左
} else if (enemyArray[enemyArrayIndex][i] == 2) {
int y = random.nextInt(20);
vcEnemy.addElement(new Enemy(bmpEnemyDuck, 2, -50,
y));
// 鸭子右
} else if (enemyArray[enemyArrayIndex][i] == 3) {
int y = random.nextInt(20);
vcEnemy.addElement(new Enemy(bmpEnemyDuck, 3,
screenW + 50, y));
}
}
// 这里判断下一组是否为最后一组(Boss)
if (enemyArrayIndex == enemyArray.length - 1) {
isBoss = true;
} else {
enemyArrayIndex++;
}
}
//处理敌机与主角的碰撞
for (int i = 0; i < vcEnemy.size(); i++) {
if (player.isCollsionWith(vcEnemy.elementAt(i))) {
//发生碰撞,主角血量-1
player.setPlayerHp(player.getPlayerHp() - 1);
//当主角血量小于0,判定游戏失败
if (player.getPlayerHp() <= -1) {
gameState = GAME_LOST;
}
}
}
}
break;
// 计时器
private int noCollisionCount = 0;
// 因为无敌时间
private int noCollisionTime = 60;
// 是否碰撞的标识位
private boolean isCollision;
//判断碰撞(主角与敌机)
public boolean isCollsionWith(Enemy en) {
//是否处于无敌时间
if (isCollision == false) {
int x2 = en.x;
int y2 = en.y;
int w2 = en.frameW;
int h2 = en.frameH;
if (x >= x2 && x >= x2 + w2) {
return false;
} else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
return false;
} else if (y >= y2 && y >= y2 + h2) {
return false;
} else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
return false;
}
//碰撞即进入无敌状态
isCollision = true;
return true;
//处于无敌状态,无视碰撞
} else {
return false;
}
}
修改逻辑方法
/**
* 游戏逻辑
*/
public void logic() {
if (isUp) {
y -= speed;
}
if (isDown) {
y += speed;
}
if (isLeft) {
x -= speed;
}
if (isRight) {
x += speed;
}
// 判断屏幕X边界
if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
x = MySurfaceView.screenW - bmpPlayer.getWidth();
} else if (x <= 0) {
x = 0;
}
// 判断屏幕Y边界
if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
y = MySurfaceView.screenH - bmpPlayer.getHeight();
} else if (y <= 0) {
y = 0;
}
// 处理无敌状态
if (isCollision) {
// 计时器开始计时
noCollisionCount++;
if (noCollisionCount >= noCollisionTime) {
// 无敌时间过后,接触无敌状态及初始化计数器
isCollision = false;
noCollisionCount = 0;
}
}
}
修改主角的绘制
Player 类
// 主角游戏绘制方法
public void draw(Canvas canvas, Paint paint) {
// 绘制主角
// 当处于无敌时间时,让主角闪烁
if (isCollision) {
// 每2次游戏循环,绘制一次主角
if (noCollisionCount % 2 == 0) {
canvas.drawBitmap(bmpPlayer, x, y, paint);
}
} else {
canvas.drawBitmap(bmpPlayer, x, y, paint);
}
// 绘制血量
for (int i = 0; i < playerHp; i++) {
canvas.drawBitmap(bmpPlayerHP, i * bmpPlayerHP.getWidth(),
MySurfaceView.screenH - bmpPlayerHP.getHeight(), paint);
}
}


猜你喜欢
- MultiFrameImageStreamCompleterMultiFrameImageStreamCompleter 是一个可组合的 I
- 一、总体概述官方文档:https://docs.devexpress.com/WindowsForms/8117/controls-and-
- 一、在drawable下面添加xml文件rounded_editview.xml<?xml version="1.0&quo
- 目录一、handler基本认识1、基本组成2、基本使用方法3、工作流程二、发送消息三、消息进入消息队列1、入队前的准备工作2、将消息加入队列
- C#利用缓存分块读写大文件,供大家参考,具体内容如下在日常生活中,可能会遇到大文件的读取,不论是什么格式,按照储存文件的格式读取大文件,就会
- 什么是粘包/拆包 一般所谓的TCP粘包是在一次接收数据不能完全地体现
- Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口)、Org.apache接口和Android.n
- 前言:小编引入的图片和文字描述都是来自于尚硅谷的视频讲解,在此感谢尚硅谷的老师,同时也结合 seata文档官方文档进行整合项目地址(gite
- 1.介绍说明: 其实@Valid 与 @Validated都是做数据校验的,只不过注解位置与用法有点不同。不同点:(1)@Valid是使用H
- 1. Android中文件读写的原理: (1).所有文件的储存都是字节的储存。 (2).在磁盘上保留的并不是文件的字符而是先把字符编码成字节
- 1.简述描述:给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。1.此题对比原题有改动2.题目保
- ODT文档格式一种开放文档格式(OpenDocument Text)。通常,ODT格式的文件可以使用LibreOffice Writer、M
- 译文链接:https://www.infoworld.com/art...C# 在 3.0 版本中提供了对 扩展方法 的支持,扩展方法常用于
- 最近用了Stream流,感觉超好用,记录一下。1、快速创建List比如我有个实体类User,User有个属性Namepublic class
- 使用Castle.Core.dll实现,核心代码是使用Castle.DynamicProxy.ProxyGenerator类的CreateI
- monaco editor创建//创建和设置值if (!this.monacoEditor) { this.monacoEdit
- 本文实例讲述了C#提取网页中超链接link和text部分的方法。分享给大家供大家参考,具体如下:string s = "..&qu
- 在Android项目开发过程中,Android Studio是一款非常强大的开发工具。到底有多强大,用了你就知道了。本文我将介绍Studio
- 对于登录功能本身没有任何特别,使用httpclient向服务器post用户名密码即可。但是为了保持登录的状态(在各个Activity之间切换
- 本文实例为大家分享了openoffice+jodconverter-code-3.0-bate4实现ppt转图片的具体代码,供大家参考,具体