首先分析下游戏界面内的元素:
无限滚动的背景图, 可以操作的主角,主角的子弹, 主角的血量,两种怪物(敌机),一个boss, boss的爆炸效果.
先看效果图
1、首先实现无限滚动的背景图 原理: 定义两个位图对象 当第一个位图到末尾是 第二个位图从第一个位图的末尾跟上.
public class GameBg {
// 游戏背景的图片资源
// 为了循环播放,这里定义两个位图对象,
// 其资源引用的是同一张图片
private Bitmap bmpBackGround1;
private Bitmap bmpBackGround2;
// 游戏背景坐标
private int bg1x, bg1y, bg2x, bg2y;
private int speed = 3;
public GameBg(Bitmap bmpBackGround) {
this.bmpBackGround1 = bmpBackGround;
this.bmpBackGround2 = bmpBackGround;
// 首先让第一张填满屏幕
bg1y = -Math.abs(bmpBackGround.getHeight() - MySurfaceView.screenH);
bg2y = bg1y - bmpBackGround1.getHeight() +50;
}
public void draw(Canvas canvas,Paint paint){
canvas.drawBitmap(bmpBackGround1, bg1x, bg1y, paint);
canvas.drawBitmap(bmpBackGround2, bg2x, bg2y, paint);
}
public void logic(){
bg1y +=speed;
bg2y +=speed;
if(bg1y > MySurfaceView.screenH){
bg1y = bg2y - bmpBackGround1.getHeight() +50;
}
if(bg2y > MySurfaceView.screenH){
bg2y = bg1y - bmpBackGround1.getHeight() +50;
}
}
}
然后再在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 GameMenu gameMenu;
private GameBg gameBg;
public MySurfaceView(Context context) {
super(context);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
setFocusable(true);
}
@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);
}
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);
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:
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:
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();
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();
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
}
您可能感兴趣的文章:打飞机游戏终极BOSS Android实战打飞机游戏完结篇Android实战打飞机游戏之子弹生成与碰撞以及爆炸效果(5)Android实战打飞机游戏之怪物(敌机)类的实现(4)Android实战打飞机游戏之实现主角以及主角相关元素(3)Android实战打飞机游戏之菜单页面设计(1)Android实现纸飞机的简单操作