标签:
子弹生成 新建子弹类
public class Bullet {
// 子弹图片资源
public Bitmap bmpBullet;
// 子弹的坐标
public int bulletX, bulletY;
// 子弹的速度
public int speed;
// 子弹的种类以及常量
public int bulletType;
// 主角的
public static final int BULLET_PLAYER = -1;
// 鸭子的
public static final int BULLET_DUCK = 1;
// 苍蝇的
public static final int BULLET_FLY = 2;
// Boss的
public static final int BULLET_BOSS = 3;
// 子弹是否超屏, 优化处理
public boolean isDead;
// Boss疯狂状态下子弹相关成员变量
private int dir;// 当前Boss子弹方向
// 8方向常量
public static final int DIR_UP = -1;
public static final int DIR_DOWN = 2;
public static final int DIR_LEFT = 3;
public static final int DIR_RIGHT = 4;
public static final int DIR_UP_LEFT = 5;
public static final int DIR_UP_RIGHT = 6;
public static final int DIR_DOWN_LEFT = 7;
public static final int DIR_DOWN_RIGHT = 8;
// 子弹当前方向
public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType) {
this.bmpBullet = bmpBullet;
this.bulletX = bulletX;
this.bulletY = bulletY;
this.bulletType = bulletType;
// 不同的子弹类型速度不一
switch (bulletType) {
case BULLET_PLAYER:
speed = 4;
break;
case BULLET_DUCK:
speed = 3;
break;
case BULLET_FLY:
speed = 4;
break;
case BULLET_BOSS:
speed = 5;
break;
}
}
public void draw(Canvas canvas, Paint paint) {
canvas.drawBitmap(bmpBullet, bulletX, bulletY, paint);
}
// 子弹的逻辑
public void logic() {
// 不同的子弹类型逻辑不一
// 主角的子弹垂直向上运动
switch (bulletType) {
case BULLET_PLAYER:
bulletY -= speed;
if (bulletY < -50) {
isDead = true;
}
break;
// 鸭子和苍蝇的子弹都是垂直下落运动
case BULLET_DUCK:
case BULLET_FLY:
bulletY += speed;
if (bulletY > MySurfaceView.screenH) {
isDead = true;
}
break;
case BULLET_BOSS:
// Boss疯狂状态下的子弹逻辑待实现
// 边界处理
if (bulletY > MySurfaceView.screenH || bulletY <= -40
|| bulletX > MySurfaceView.screenW || bulletX <= -40) {
isDead = true;
}
break;
}
}
}
在在MySurfacview里面调用 生成子弹主角的和自己的
package com.gsf;
import java.util.Random;
import java.util.Vector;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
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;
// 敌机子弹容器
private Vector<Bullet> vcBullet;
// 添加子弹的计数器
private int countEnemyBullet;
// 主角子弹容器
private Vector<Bullet> vcBulletPlayer;
// 添加子弹的计数器
private int countPlayerBullet;
/**
* 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);
//敌机子弹容器实例
vcBullet = new Vector<Bullet>();
//主角子弹容器实例
vcBulletPlayer = new Vector<Bullet>();
// 实例敌机容器
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);
}
//敌机子弹绘制
for (int i = 0; i < vcBullet.size(); i++) {
vcBullet.elementAt(i).draw(canvas, paint);
}
} else {
// boss 绘制
}
// 处理主角子弹绘制
for (int i = 0; i < vcBulletPlayer.size(); i++) {
vcBulletPlayer.elementAt(i).draw(canvas, paint);
}
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++;
}
}
// 每2秒添加一个敌机子弹
countEnemyBullet++;
if (countEnemyBullet % 40 == 0) {
for (int i = 0; i < vcEnemy.size(); i++) {
Enemy en = vcEnemy.elementAt(i);
// 不同类型敌机不同的子弹运行轨迹
int bulletType = 0;
switch (en.type) {
// 苍蝇
case Enemy.TYPE_FLY:
bulletType = Bullet.BULLET_FLY;
break;
// 鸭子
case Enemy.TYPE_DUCKL:
case Enemy.TYPE_DUCKR:
bulletType = Bullet.BULLET_DUCK;
break;
}
vcBullet.add(new Bullet(bmpEnemyBullet, en.x + 10,
en.y + 20, bulletType));
}
}
// 处理敌机子弹逻辑
for (int i = 0; i < vcBullet.size(); i++) {
Bullet b = vcBullet.elementAt(i);
if (b.isDead) {
vcBullet.removeElement(b);
} else {
b.logic();
}
}
//每1秒添加一个主角子弹
countPlayerBullet++;
if (countPlayerBullet % 20 == 0) {
vcBulletPlayer.add(new Bullet(bmpBullet, player.x + 15, player.y - 20, Bullet.BULLET_PLAYER));
}
// 处理敌机与主角的碰撞
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;
}
}
}
//处理主角子弹逻辑
for (int i = 0; i < vcBulletPlayer.size(); i++) {
Bullet b = vcBulletPlayer.elementAt(i);
if (b.isDead) {
vcBulletPlayer.removeElement(b);
} else {
b.logic();
}
}
}
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类 添加碰撞检测
// // 判断碰撞(主角与敌机子弹)
public boolean isCollsionWith(Bullet bullet) {
// 是否处于无敌时间
if (isCollision == false) {
int x2 = bullet.bulletX;
int y2 = bullet.bulletY;
int w2 = bullet.bmpBullet.getWidth();
int h2 = bullet.bmpBullet.getHeight();
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;
}
}
在主界面 MySrufaceView 的逻辑函数中添加 主角和敌机子弹的碰撞
//处理敌机子弹与主角碰撞
for (int i = 0; i < vcBullet.size(); i++) {
if (player.isCollsionWith(vcBullet.elementAt(i))) {
//发生碰撞,主角血量-1
player.setPlayerHp(player.getPlayerHp() - 1);
//当主角血量小于0,判定游戏失败
if (player.getPlayerHp() <= -1) {
gameState = GAME_LOST;
}
}
}
当然 主角的子弹碰撞了敌机 也需要进行碰撞检测 修改 敌机类 Enemy
//判断碰撞(敌机与主角子弹碰撞)
public boolean isCollsionWith(Bullet bullet) {
int x2 = bullet.bulletX;
int y2 = bullet.bulletY;
int w2 = bullet.bmpBullet.getWidth();
int h2 = bullet.bmpBullet.getHeight();
if (x >= x2 && x >= x2 + w2) {
return false;
} else if (x <= x2 && x + frameW <= x2) {
return false;
} else if (y >= y2 && y >= y2 + h2) {
return false;
} else if (y <= y2 && y + frameH <= y2) {
return false;
}
//发生碰撞,让其死亡
isDead = true;
return true;
}
在主视图中添加逻辑 主角子弹和敌机碰撞的逻辑
//处理主角子弹与敌机碰撞
for (int i = 0; i < vcBulletPlayer.size(); i++) {
//取出主角子弹容器的每个元素
Bullet blPlayer = vcBulletPlayer.elementAt(i);
for (int j = 0; j < vcEnemy.size(); j++) {
//添加爆炸效果 待完成
}
}
上面完成 的效果图
===============下面完成爆炸的效果============
新建一个爆炸类 Boom
/**
* 瞬间爆炸 类
* @author liuml
* @time 2016-6-1 上午11:32:56
*/
public class Boom {
//爆炸效果资源图
private Bitmap bmpBoom;
//爆炸效果的位置坐标
private int boomX, boomY;
//爆炸动画播放当前的帧下标
private int cureentFrameIndex;
//爆炸效果的总帧数
private int totleFrame;
//每帧的宽高
private int frameW, frameH;
//是否播放完毕,优化处理
public boolean playEnd;
//爆炸效果的构造函数
public Boom(Bitmap bmpBoom, int x, int y, int totleFrame) {
this.bmpBoom = bmpBoom;
this.boomX = x;
this.boomY = y;
this.totleFrame = totleFrame;
frameW = bmpBoom.getWidth() / totleFrame;
frameH = bmpBoom.getHeight();
}
//爆炸效果绘制
public void draw(Canvas canvas, Paint paint) {
canvas.save();
canvas.clipRect(boomX, boomY, boomX + frameW, boomY + frameH);
canvas.drawBitmap(bmpBoom, boomX - cureentFrameIndex * frameW, boomY, paint);
canvas.restore();
}
//爆炸效果的逻辑
public void logic() {
if (cureentFrameIndex < totleFrame) {
cureentFrameIndex++;
} else {
playEnd = true;
}
}
}
下面就还是老套路了 在主界面 声明爆炸容器 然后绘制 然后是逻辑实现
package com.gsf;
import java.util.Random;
import java.util.Vector;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
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;
// 敌机子弹容器
private Vector<Bullet> vcBullet;
// 添加子弹的计数器
private int countEnemyBullet;
// 主角子弹容器
private Vector<Bullet> vcBulletPlayer;
// 添加子弹的计数器
private int countPlayerBullet;
//爆炸效果容器
private Vector<Boom> vcBoom;
/**
* 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);
//敌机子弹容器实例
vcBullet = new Vector<Bullet>();
//主角子弹容器实例
vcBulletPlayer = new Vector<Bullet>();
// 实例敌机容器
vcEnemy = new Vector<Enemy>();
// 实例随机库
random = new Random();
//爆炸效果容器实例
vcBoom = new Vector<Boom>();
}
/**
* 游戏绘图
*/
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);
}
//敌机子弹绘制
for (int i = 0; i < vcBullet.size(); i++) {
vcBullet.elementAt(i).draw(canvas, paint);
}
//爆炸效果绘制
for (int i = 0; i < vcBoom.size(); i++) {
vcBoom.elementAt(i).draw(canvas, paint);
}
} else {
// boss 绘制
}
// 处理主角子弹绘制
for (int i = 0; i < vcBulletPlayer.size(); i++) {
vcBulletPlayer.elementAt(i).draw(canvas, paint);
}
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++;
}
}
// 每2秒添加一个敌机子弹
countEnemyBullet++;
if (countEnemyBullet % 40 == 0) {
for (int i = 0; i < vcEnemy.size(); i++) {
Enemy en = vcEnemy.elementAt(i);
// 不同类型敌机不同的子弹运行轨迹
int bulletType = 0;
switch (en.type) {
// 苍蝇
case Enemy.TYPE_FLY:
bulletType = Bullet.BULLET_FLY;
break;
// 鸭子
case Enemy.TYPE_DUCKL:
case Enemy.TYPE_DUCKR:
bulletType = Bullet.BULLET_DUCK;
break;
}
vcBullet.add(new Bullet(bmpEnemyBullet, en.x + 10,
en.y + 20, bulletType));
}
}
// 处理敌机子弹逻辑
for (int i = 0; i < vcBullet.size(); i++) {
Bullet b = vcBullet.elementAt(i);
if (b.isDead) {
vcBullet.removeElement(b);
} else {
b.logic();
}
}
//每1秒添加一个主角子弹
countPlayerBullet++;
if (countPlayerBullet % 20 == 0) {
vcBulletPlayer.add(new Bullet(bmpBullet, player.x + 15, player.y - 20, Bullet.BULLET_PLAYER));
}
// 处理敌机与主角的碰撞
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;
}
}
}
//处理主角子弹逻辑
for (int i = 0; i < vcBulletPlayer.size(); i++) {
Bullet b = vcBulletPlayer.elementAt(i);
if (b.isDead) {
vcBulletPlayer.removeElement(b);
} else {
b.logic();
}
}
//处理敌机子弹与主角碰撞
for (int i = 0; i < vcBullet.size(); i++) {
if (player.isCollsionWith(vcBullet.elementAt(i))) {
//发生碰撞,主角血量-1
player.setPlayerHp(player.getPlayerHp() - 1);
//当主角血量小于0,判定游戏失败
if (player.getPlayerHp() <= -1) {
gameState = GAME_LOST;
}
}
}
//处理主角子弹与敌机碰撞
for (int i = 0; i < vcBulletPlayer.size(); i++) {
//取出主角子弹容器的每个元素
Bullet blPlayer = vcBulletPlayer.elementAt(i);
for (int j = 0; j < vcEnemy.size(); j++) {
//添加爆炸效果
//取出敌机容器的每个元与主角子弹遍历判断
if (vcEnemy.elementAt(j).isCollsionWith(blPlayer)) {
vcBoom.add(new Boom(bmpBoom, vcEnemy.elementAt(j).x, vcEnemy.elementAt(j).y, 7));
}
}
}
}
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;
}
}
android 游戏 实战打飞机游戏 子弹生成与碰撞 以及爆炸效果(5)
标签:
原文地址:http://blog.csdn.net/liudao7994/article/details/51553677