android 游戏 实战打飞机游戏 菜单页面(1)
生活随笔
收集整理的这篇文章主要介绍了
android 游戏 实战打飞机游戏 菜单页面(1)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
目标 实现 控制 小飞机 左右移动 躲避子弹 打boss.
本节 实现 开始菜单界面
1 首先 资源文件拷过来
2, 划分游戏状态
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; 定义五种状态定义完方法后 在绘图方法中 ,实体键 按下方法 ,抬起方法,触屏监听,逻辑方法,switch
//在那几个方法中加这个switch (gameState) {case GAME_MENU:break;case GAMEING:break;case GAME_WIN:break;case GAME_LOST:break;case GAME_PAUSE:break;default:break;}下面再声明一些东西
//声明一个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;//怪物猪头Boosprivate 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子弹初始化 游戏
/*** SurfaceView视图创建,响应此函数*/@Overridepublic 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
菜单类
包括 初始化绘制按钮和背景图
==============
然后 在MySurfaceView中使用 GameMenu 使用菜单类
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;// 怪物猪头Boosprivate 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;/*** 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视图创建,响应此函数*/@Overridepublic 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);}/*** 游戏绘图*/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: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);}}/*** 触屏事件监听*/@Overridepublic 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;}/*** 按键事件监听*/@Overridepublic 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);}@Overridepublic 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:break;case GAME_WIN:break;case GAME_LOST:break;case GAME_PAUSE:break;}}@Overridepublic 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视图状态发生改变,响应此函数*/@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}/*** SurfaceView视图消亡时,响应此函数*/@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {flag = false;}}效果图
总结
以上是生活随笔为你收集整理的android 游戏 实战打飞机游戏 菜单页面(1)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 补丁上打补丁 微软公布WindowsXP
- 下一篇: 虚幻4游戏菜单功能的实现