Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實戰打飛機游戲之實現主角以及主角相關元素(3)

Android實戰打飛機游戲之實現主角以及主角相關元素(3)

編輯:關於Android編程

先看效果圖

新建player 類

public class Player {

 private int playerHp = 3;

 private Bitmap bmpPlayerHP;
 // 主角坐標以及位圖
 private int x, y;
 private Bitmap bmpPlayer;
 // 主角移動速度

 private int speed = 5;
 // 主角移動標識
 private boolean isUp, isDown, isLeft, isRight;

 // 主角的構造函數
 public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp) {
  this.bmpPlayer = bmpPlayer;
  this.bmpPlayerHP = bmpPlayerHp;
  // 飛機初始位置
  x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;
  y = MySurfaceView.screenH - bmpPlayer.getHeight();
 }

 // 主角游戲繪制方法
 public void draw(Canvas canvas, Paint paint) {

  // 繪制主角
  canvas.drawBitmap(bmpPlayer, x, y, paint);
  // 繪制血量

  for (int i = 0; i < playerHp; i++) {
   canvas.drawBitmap(bmpPlayerHP, i * bmpPlayerHP.getWidth(),
     MySurfaceView.screenH - bmpPlayerHP.getHeight(), paint);
  }

 }

 /**
  * 按鍵事件監聽
  */
 public void onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
   isUp = true;
  }
  if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
   isDown = true;
  }
  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
   isLeft = true;
  }
  if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
   isRight = true;
  }
 }

 public void onKeyUp(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
   isUp = false;
  }
  if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
   isDown = false;
  }
  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
   isLeft = false;
  }
  if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
   isRight = false;
  }
 }

 /**
  * 游戲邏輯
  */
 public void logic() {
  if (isUp) {
   y -= speed;
  }
  if (isDown) {
   y += speed;
  }
  if (isLeft) {
   x -= speed;
  }
  if (isRight) {
   x += speed;
  }
  // 判斷屏幕X邊界
  if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
   x = MySurfaceView.screenW - bmpPlayer.getWidth();
  } else if (x <= 0) {
   x = 0;
  }
  // 判斷屏幕Y邊界
  if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
   y = MySurfaceView.screenH - bmpPlayer.getHeight();
  } else if (y <= 0) {
   y = 0;
  }

 }


 //設置主角血量
 public void setPlayerHp(int hp) {
  this.playerHp = hp;
 }

 //獲取主角血量
 public int getPlayerHp() {
  return playerHp;
 }


}

再在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;

 private Player player;

 /**
  * 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);

 }

 /**
  * 游戲繪圖
  */
 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);
     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();
   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;
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved