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游戏菜单功能的实现