本文目標實現控制小飛機的左右移動、躲避子彈、打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;//怪物豬頭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子彈
初始化 遊戲
/** * 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
菜單類
包括 初始化繪製按鈕和背景圖
package com.gsf;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Paint;import android.view.MotionEvent;/** * * @author liuml * @time 2016-5-27 下午5:43:34 */public class GameMenu { // 菜單背景圖 private Bitmap bmpMenu; // 按鈕圖片資源(按下和未按下圖) private Bitmap bmpButton, bmpButtonPress; // 按鈕的座標 private int btnX, btnY; // 按鈕是否按下標識位 private Boolean isPress; // 菜單初始化 public GameMenu(Bitmap bmpMenu, Bitmap bmpButton, Bitmap bmpButtonPress) { this.bmpMenu = bmpMenu; this.bmpButton = bmpButton; this.bmpButtonPress = bmpButtonPress; // X置中,Y緊接螢幕底部 btnX = MySurfaceView.screenW / 2 - bmpButton.getWidth() / 2; btnY = MySurfaceView.screenH - bmpButton.getHeight(); isPress = false; } public void draw(Canvas canvas, Paint paint) { // 繪製菜單背景圖 canvas.drawBitmap(bmpMenu, 0, 0, paint); if (isPress) { canvas.drawBitmap(bmpButtonPress, btnX, btnY, paint); } else { canvas.drawBitmap(bmpButton, btnX, btnY, paint); } } public void onTouchEvent(MotionEvent event) { // 擷取當前觸控位置 int pointX = (int) event.getX(); int pointyY = (int) event.getY(); // 當使用者是按下和移動時 if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { // 判定使用者是否點擊按鈕 if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) { if (pointyY > btnY && pointyY < btnY + bmpButton.getHeight()) { isPress = true; } else { isPress = false; } } else { isPress = false; } // 當用於是抬起動作時 } else if (event.getAction() == MotionEvent.ACTION_UP) { // 判斷抬起時是否點擊按鈕,防止使用者移動到別處 if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) { if (pointyY > btnY && pointyY < btnY + bmpButton.getHeight()) { isPress = false;//抬起後重設 還原Button狀態為未按下狀態 //改變當前遊戲狀態為開始遊戲 MySurfaceView.gameState = MySurfaceView.GAMEING; } } } }}
然後 在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;// 怪物豬頭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; /** * 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); } /** * 遊戲繪圖 */ 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); } } /** * 觸屏事件監聽 */ @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: 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; }}
效果圖:
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。