Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實戰打飛機游戲之怪物(敵機)類的實現(4)

Android實戰打飛機游戲之怪物(敵機)類的實現(4)

編輯:關於Android編程

先看看效果圖:

分析: 根據敵機類型區分 敵機 運動邏輯 以及繪制

/**
 * 敵機
 * 
 * @author liuml
 * @time 2016-5-31 下午4:14:59
 */
public class Enemy {

  // 敵機的種類標識
  public int type;
  // 蒼蠅
  public static final int TYPE_FLY = 1;
  // 鴨子(從左往右運動)
  public static final int TYPE_DUCKL = 2;
  // 鴨子(從右往左運動)
  public static final int TYPE_DUCKR = 3;
  // 敵機圖片資源
  public Bitmap bmpEnemy;
  // 敵機坐標
  public int x, y;
  // 敵機每幀的寬高
  public int frameW, frameH;
  // 敵機當前幀下標
  private int frameIndex;
  // 敵機的移動速度
  private int speed;;
  // 判斷敵機是否已經出屏
  public boolean isDead;

  // 敵機的構造函數
  public Enemy(Bitmap bmpEnemy, int enemyType, int x, int y) {
    this.bmpEnemy = bmpEnemy;
    frameW = bmpEnemy.getWidth() / 10;
    frameH = bmpEnemy.getHeight();
    this.type = enemyType;
    this.x = x;
    this.y = y;
    // 不同種類的敵機血量不同
    switch (type) {
    // 蒼蠅
    case TYPE_FLY:
      speed = 25;
      break;
    // 鴨子
    case TYPE_DUCKL:
      speed = 3;
      break;
    case TYPE_DUCKR:
      speed = 3;
      break;
    }
  }

  // 敵機繪圖函數
  public void draw(Canvas canvas, Paint paint) {
    canvas.save();
    canvas.clipRect(x, y, x + frameW, y + frameH);
    canvas.drawBitmap(bmpEnemy, x - frameIndex * frameW, y, paint);
    canvas.restore();
  }

  // 敵機邏輯AI
  public void logic() {
    // 不斷循環播放幀形成動畫
    frameIndex++;
    if (frameIndex >= 10) {
      frameIndex = 0;
    }
    // 不同種類的敵機擁有不同的AI邏輯
    switch (type) {
    case TYPE_FLY:
      if (isDead == false) {
        // 減速出現,加速返回
        speed -= 1;
        y += speed;
        if (y <= -200) {
          isDead = true;
        }
      }
      break;
    case TYPE_DUCKL:
      if (isDead == false) {
        // 斜右下角運動
        x += speed / 2;
        y += speed;
        if (x > MySurfaceView.screenW) {
          isDead = true;
        }
      }
      break;
    case TYPE_DUCKR:
      if (isDead == false) {
        // 斜左下角運動
        x -= speed / 2;
        y += speed;
        if (x < -50) {
          isDead = true;
        }
      }
      break;
    }
  }

}

在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 Vector<Enemy> vcEnemy;
  // 每次生成敵機的時間(毫秒)
  private int createEnemyTime = 50;
  private int count;// 計數器
  // 敵人數組:1和2表示敵機的種類,-1表示Boss
  // 二維數組的每一維都是一組怪物
  private int enemyArray[][] = { { 1, 2 }, { 1, 1 }, { 1, 3, 1, 2 },
      { 1, 2 }, { 2, 3 }, { 3, 1, 3 }, { 2, 2 }, { 1, 2 }, { 2, 2 },
      { 1, 3, 1, 1 }, { 2, 1 }, { 1, 3 }, { 2, 1 }, { -1 } };
  // 當前取出一維數組的下標
  private int enemyArrayIndex;
  // 是否出現Boss標識位
  private boolean isBoss;
  // 隨機庫,為創建的敵機賦予隨即坐標
  private Random random;

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

    // 實例敵機容器
    vcEnemy = new Vector<Enemy>();
    // 實例隨機庫
    random = new Random();
  }

  /**
   * 游戲繪圖
   */
  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);
          if (isBoss == false) {
            // 敵機繪制
            for (int i = 0; i < vcEnemy.size(); i++) {
              vcEnemy.elementAt(i).draw(canvas, paint);
            }

          } else {
            // boss 繪制
          }
          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();
      // 敵機邏輯
      if (isBoss == false) {
        // 敵機邏輯
        for (int i = 0; i < vcEnemy.size(); i++) {
          Enemy en = vcEnemy.elementAt(i);
          // 因為容器不斷添加敵機 ,那麼對敵機isDead判定,
          // 如果已死亡那麼就從容器中刪除,對容器起到了優化作用;
          if (en.isDead) {
            vcEnemy.removeElementAt(i);
          } else {
            en.logic();
          }
        }
        // 生成敵機
        count++;
        if (count % createEnemyTime == 0) {
          for (int i = 0; i < enemyArray[enemyArrayIndex].length; i++) {
            // 蒼蠅
            if (enemyArray[enemyArrayIndex][i] == 1) {
              int x = random.nextInt(screenW - 100) + 50;
              vcEnemy.addElement(new Enemy(bmpEnemyFly, 1, x, -50));
              // 鴨子左
            } else if (enemyArray[enemyArrayIndex][i] == 2) {
              int y = random.nextInt(20);
              vcEnemy.addElement(new Enemy(bmpEnemyDuck, 2, -50,
                  y));
              // 鴨子右
            } else if (enemyArray[enemyArrayIndex][i] == 3) {
              int y = random.nextInt(20);
              vcEnemy.addElement(new Enemy(bmpEnemyDuck, 3,
                  screenW + 50, y));
            }
          }
          // 這裡判斷下一組是否為最後一組(Boss)
          if (enemyArrayIndex == enemyArray.length - 1) {
            isBoss = true;
          } else {
            enemyArrayIndex++;
          }
        }
      }
      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;
  }
}

碰撞檢測
修改Player類

package com.gsf;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.KeyEvent;

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;
  }


  //判斷碰撞(敵機與主角子彈碰撞)
  public boolean isCollsionWith(Enemy bullet) {
      int x2 = bullet.x;
      int y2 = bullet.y;
      int w2 = bullet.frameW;
      int h2 = bullet.frameH;
      if (x >= x2 && x >= x2 + w2) {
        return false;
      } else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
        return false;
      } else if (y >= y2 && y >= y2 + h2) {
        return false;
      } else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
        return false;
      }
      //發生碰撞,讓其死亡
      //isDead = true;
      return true;
    }


}

在MySurface中 加上碰撞邏輯

  /**
   * 游戲邏輯
   */
  private void logic() {
    switch (gameState) {
    case GAME_MENU:

      break;
    case GAMEING:
      gameBg.logic();
      player.logic();
      // 敵機邏輯
      if (isBoss == false) {
        // 敵機邏輯
        for (int i = 0; i < vcEnemy.size(); i++) {
          Enemy en = vcEnemy.elementAt(i);
          // 因為容器不斷添加敵機 ,那麼對敵機isDead判定,
          // 如果已死亡那麼就從容器中刪除,對容器起到了優化作用;
          if (en.isDead) {
            vcEnemy.removeElementAt(i);
          } else {
            en.logic();
          }
        }
        // 生成敵機
        count++;
        if (count % createEnemyTime == 0) {
          for (int i = 0; i < enemyArray[enemyArrayIndex].length; i++) {
            // 蒼蠅
            if (enemyArray[enemyArrayIndex][i] == 1) {
              int x = random.nextInt(screenW - 100) + 50;
              vcEnemy.addElement(new Enemy(bmpEnemyFly, 1, x, -50));
              // 鴨子左
            } else if (enemyArray[enemyArrayIndex][i] == 2) {
              int y = random.nextInt(20);
              vcEnemy.addElement(new Enemy(bmpEnemyDuck, 2, -50,
                  y));
              // 鴨子右
            } else if (enemyArray[enemyArrayIndex][i] == 3) {
              int y = random.nextInt(20);
              vcEnemy.addElement(new Enemy(bmpEnemyDuck, 3,
                  screenW + 50, y));
            }
          }
          // 這裡判斷下一組是否為最後一組(Boss)
          if (enemyArrayIndex == enemyArray.length - 1) {
            isBoss = true;
          } else {
            enemyArrayIndex++;
          }
        }
        //處理敵機與主角的碰撞
        for (int i = 0; i < vcEnemy.size(); i++) {
          if (player.isCollsionWith(vcEnemy.elementAt(i))) {
            //發生碰撞,主角血量-1
            player.setPlayerHp(player.getPlayerHp() - 1);
            //當主角血量小於0,判定游戲失敗
            if (player.getPlayerHp() <= -1) {
              gameState = GAME_LOST;
            }
          }
        }
      }
      break;

// 計時器
  private int noCollisionCount = 0;
  // 因為無敵時間
  private int noCollisionTime = 60;
  // 是否碰撞的標識位
  private boolean isCollision;

//判斷碰撞(主角與敵機)
  public boolean isCollsionWith(Enemy en) {
    //是否處於無敵時間
    if (isCollision == false) {
      int x2 = en.x;
      int y2 = en.y;
      int w2 = en.frameW;
      int h2 = en.frameH;
      if (x >= x2 && x >= x2 + w2) {
        return false;
      } else if (x <= x2 && x + bmpPlayer.getWidth() <= x2) {
        return false;
      } else if (y >= y2 && y >= y2 + h2) {
        return false;
      } else if (y <= y2 && y + bmpPlayer.getHeight() <= y2) {
        return false;
      }
      //碰撞即進入無敵狀態
      isCollision = true;
      return true;
      //處於無敵狀態,無視碰撞
    } else {
      return 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;
    }

    // 處理無敵狀態
    if (isCollision) {
      // 計時器開始計時
      noCollisionCount++;
      if (noCollisionCount >= noCollisionTime) {
        // 無敵時間過後,接觸無敵狀態及初始化計數器
        isCollision = false;
        noCollisionCount = 0;
      }
    }

  }

修改主角的繪制
Player 類

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

    // 繪制主角
    // 當處於無敵時間時,讓主角閃爍
    if (isCollision) {
      // 每2次游戲循環,繪制一次主角
      if (noCollisionCount % 2 == 0) {
        canvas.drawBitmap(bmpPlayer, x, y, paint);
      }
    } else {
      canvas.drawBitmap(bmpPlayer, x, y, paint);
    }
    // 繪制血量

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

  }

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

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