Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 游戲 實戰打飛機游戲 BOSS的設計 以及勝利失敗頁面(6結束)

android 游戲 實戰打飛機游戲 BOSS的設計 以及勝利失敗頁面(6結束)

編輯:關於Android編程

這裡寫圖片描述

修改子彈類:

public class Bullet {
    //子彈圖片資源
    public Bitmap bmpBullet;
    //子彈的坐標
    public int bulletX, bulletY;
    //子彈的速度
    public int speed;
    //子彈的種類以及常量
    public int bulletType;
    //主角的
    public static final int BULLET_PLAYER = -1;
    //鴨子的
    public static final int BULLET_DUCK = 1;
    //蒼蠅的
    public static final int BULLET_FLY = 2;
    //Boss的
    public static final int BULLET_BOSS = 3;
    //子彈是否超屏, 優化處理
    public boolean isDead;

    //Boss瘋狂狀態下子彈相關成員變量
    private int dir;//當前Boss子彈方向
    //8方向常量
    public static final int DIR_UP = -1;
    public static final int DIR_DOWN = 2;
    public static final int DIR_LEFT = 3;
    public static final int DIR_RIGHT = 4;
    public static final int DIR_UP_LEFT = 5;
    public static final int DIR_UP_RIGHT = 6;
    public static final int DIR_DOWN_LEFT = 7;
    public static final int DIR_DOWN_RIGHT = 8;

    //子彈當前方向
    public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType) {
        this.bmpBullet = bmpBullet;
        this.bulletX = bulletX;
        this.bulletY = bulletY;
        this.bulletType = bulletType;
        //不同的子彈類型速度不一
        switch (bulletType) {
        case BULLET_PLAYER:
            speed = 4;
            break;
        case BULLET_DUCK:
            speed = 3;
            break;
        case BULLET_FLY:
            speed = 4;
            break;
        case BULLET_BOSS:
            speed = 5;
            break;
        }
    }

    /**
     * 專用於處理Boss瘋狂狀態下創建的子彈
     * @param bmpBullet
     * @param bulletX
     * @param bulletY
     * @param bulletType
     * @param Dir
     */
    public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType, int dir) {
        this.bmpBullet = bmpBullet;
        this.bulletX = bulletX;
        this.bulletY = bulletY;
        this.bulletType = bulletType;
        speed = 5;
        this.dir = dir;
    }

    //子彈的繪制
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bmpBullet, bulletX, bulletY, paint);
    }

    //子彈的邏輯
    public void logic() {
        //不同的子彈類型邏輯不一
        //主角的子彈垂直向上運動
        switch (bulletType) {
        case BULLET_PLAYER:
            bulletY -= speed;
            if (bulletY < -50) {
                isDead = true;
            }
            break;
        //鴨子和蒼蠅的子彈都是垂直下落運動
        case BULLET_DUCK:
        case BULLET_FLY:
            bulletY += speed;
            if (bulletY > MySurfaceView.screenH) {
                isDead = true;
            }
            break;
        //Boss瘋狂狀態下的8方向子彈邏輯
        case BULLET_BOSS:
            //Boss瘋狂狀態下的子彈邏輯待實現
            switch (dir) {
            //方向上的子彈
            case DIR_UP:
                bulletY -= speed;
                break;
            //方向下的子彈
            case DIR_DOWN:
                bulletY += speed;
                break;
            //方向左的子彈
            case DIR_LEFT:
                bulletX -= speed;
                break;
            //方向右的子彈
            case DIR_RIGHT:
                bulletX += speed;
                break;
            //方向左上的子彈
            case DIR_UP_LEFT:
                bulletY -= speed;
                bulletX -= speed;
                break;
            //方向右上的子彈
            case DIR_UP_RIGHT:
                bulletX += speed;
                bulletY -= speed;
                break;
            //方向左下的子彈
            case DIR_DOWN_LEFT:
                bulletX -= speed;
                bulletY += speed;
                break;
            //方向右下的子彈
            case DIR_DOWN_RIGHT:
                bulletY += speed;
                bulletX += speed;
                break;
            }
            //邊界處理
            if (bulletY > MySurfaceView.screenH || bulletY <= -40 || bulletX > MySurfaceView.screenW || bulletX <= -40) {
                isDead = true;
            }
            break;
        }
    }
}

下面 新建BOSS類

創建BOSS

public class Boss {
    //Boss的血量
    public int hp = 50;
    //Boss的圖片資源
    private Bitmap bmpBoss;
    //Boss坐標
    public int x, y;
    //Boss每幀的寬高
    public int frameW, frameH;
    //Boss當前幀下標
    private int frameIndex;
    //Boss運動的速度
    private int speed = 5;
    //Boss的運動軌跡
    //一定時間會向著屏幕下方運動,並且發射大范圍子彈,(是否狂態)
    //正常狀態下 ,子彈垂直朝下運動
    private boolean isCrazy;
    //進入瘋狂狀態的狀態時間間隔
    private int crazyTime = 200;
    //計數器
    private int count;

    //Boss的構造函數
    public Boss(Bitmap bmpBoss) {
        this.bmpBoss = bmpBoss;
        frameW = bmpBoss.getWidth() / 10;
        frameH = bmpBoss.getHeight();
        //Boss的X坐標居中
        x = MySurfaceView.screenW / 2 - frameW / 2;
        y = 0;
    }

    //Boss的繪制
    public void draw(Canvas canvas, Paint paint) {
        canvas.save();
        canvas.clipRect(x, y, x + frameW, y + frameH);
        canvas.drawBitmap(bmpBoss, x - frameIndex * frameW, y, paint);
        canvas.restore();
    }

    //Boss的邏輯
    public void logic() {
        //不斷循環播放幀形成動畫
        frameIndex++;
        if (frameIndex >= 10) {
            frameIndex = 0;
        }
        //沒有瘋狂的狀態
        if (isCrazy == false) {
            x += speed;
            if (x + frameW >= MySurfaceView.screenW) {
                speed = -speed;
            } else if (x <= 0) {
                speed = -speed;
            }
            count++;
            if (count % crazyTime == 0) {
                isCrazy = true;
                speed = 24;
            }
            //瘋狂的狀態
        } else {
            speed -= 1;
            //當Boss返回時創建大量子彈
            if (speed == 0) {
                //添加8方向子彈
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_UP));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_DOWN));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_LEFT));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_RIGHT));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_UP_LEFT));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_UP_RIGHT));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_DOWN_LEFT));
                MySurfaceView.vcBulletBoss.add(new Bullet(MySurfaceView.bmpBossBullet, x+40, y+10, Bullet.BULLET_BOSS, Bullet.DIR_DOWN_RIGHT));
            }
            y += speed;
            if (y <= 0) {
                //恢復正常狀態
                isCrazy = false;
                speed = 5;
            }
        }
    }

    //判斷碰撞(Boss被主角子彈擊中)
    public boolean isCollsionWith(Bullet bullet) {
        int x2 = bullet.bulletX;
        int y2 = bullet.bulletY;
        int w2 = bullet.bmpBullet.getWidth();
        int h2 = bullet.bmpBullet.getHeight();
        if (x >= x2 && x >= x2 + w2) {
            return false;
        } else if (x <= x2 && x + frameW <= x2) {
            return false;
        } else if (y >= y2 && y >= y2 + h2) {
            return false;
        } else if (y <= y2 && y + frameH <= y2) {
            return false;
        }
        return true;
    }

    //設置Boss血量
    public void setHp(int hp) {
        this.hp = hp;
    }
}

在主界面實例化 子彈 以及boss

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
    private SurfaceHolder sfh;
    private Paint paint;
    private Thread th;
    private boolean flag;
    private Canvas canvas;
    public static int screenW, screenH;
    //定義游戲狀態常量
    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子彈
    //聲明一個菜單對象
    private GameMenu gameMenu;
    //聲明一個滾動游戲背景對象
    private GameBg backGround;
    //聲明主角對象
    private Player player;
    //聲明一個敵機容器
    private Vector 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 Vector vcBullet;
    //添加子彈的計數器
    private int countEnemyBullet;
    //主角子彈容器
    private Vector vcBulletPlayer;
    //添加子彈的計數器
    private int countPlayerBullet;
    //爆炸效果容器
    private Vector vcBoom;
    //聲明Boss
    private Boss boss;
    //Boss的子彈容器
    public static Vector vcBulletBoss;

    /**
     * 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);
        setFocusableInTouchMode(true);
        //設置背景常亮
        this.setKeepScreenOn(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() {
        //放置游戲切入後台重新進入游戲時,游戲被重置!
        //當游戲狀態處於菜單時,才會重置游戲
        if (gameState == GAME_MENU) {
            //加載游戲資源
            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);
            //爆炸效果容器實例
            vcBoom = new Vector();
            //敵機子彈容器實例
            vcBullet = new Vector();
            //主角子彈容器實例
            vcBulletPlayer = new Vector();
            //菜單類實例
            gameMenu = new GameMenu(bmpMenu, bmpButton, bmpButtonPress);
            //實例游戲背景
            backGround = new GameBg(bmpBackGround);
            //實例主角
            player = new Player(bmpPlayer, bmpPlayerHp);
            //實例敵機容器
            vcEnemy = new Vector();
            //實例隨機庫
            random = new Random();
            //---Boss相關
            //實例boss對象
            boss = new Boss(bmpEnemyBoos);
            //實例Boss子彈容器
            vcBulletBoss = new Vector();
        }
    }

    /**
     * 游戲繪圖
     */
    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:
                    //游戲背景
                    backGround.draw(canvas, paint);
                    //主角繪圖函數
                    player.draw(canvas, paint);
                    if (isBoss == false) {
                        //敵機繪制
                        for (int i = 0; i < vcEnemy.size(); i++) {
                            vcEnemy.elementAt(i).draw(canvas, paint);
                        }
                        //敵機子彈繪制
                        for (int i = 0; i < vcBullet.size(); i++) {
                            vcBullet.elementAt(i).draw(canvas, paint);
                        }
                    } else {
                        //Boos的繪制
                        boss.draw(canvas, paint);
                        //Boss子彈邏輯
                        for (int i = 0; i < vcBulletBoss.size(); i++) {
                            vcBulletBoss.elementAt(i).draw(canvas, paint);
                        }
                    }
                    //處理主角子彈繪制
                    for (int i = 0; i < vcBulletPlayer.size(); i++) {
                        vcBulletPlayer.elementAt(i).draw(canvas, paint);
                    }
                    //爆炸效果繪制
                    for (int i = 0; i < vcBoom.size(); i++) {
                        vcBoom.elementAt(i).draw(canvas, paint);
                    }
                    break;
                case GAME_PAUSE:
                    break;
                case GAME_WIN:
                    canvas.drawBitmap(bmpGameWin, 0, 0, paint);
                    break;
                case GAME_LOST:
                    canvas.drawBitmap(bmpGameLost, 0, 0, paint);
                    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_PAUSE:
            break;
        case GAME_WIN:
            break;
        case GAME_LOST:

            break;
        }
        return true;
    }

    /**
     * 按鍵按下事件監聽
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //處理back返回按鍵
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //游戲勝利、失敗、進行時都默認返回菜單
            if (gameState == GAMEING || gameState == GAME_WIN || gameState == GAME_LOST) {
                gameState = GAME_MENU;
                //Boss狀態設置為沒出現
                isBoss = false;
                //重置游戲
                initGame();
                //重置怪物出場
                enemyArrayIndex = 0;
            } else if (gameState == GAME_MENU) {
                //當前游戲狀態在菜單界面,默認返回按鍵退出游戲
                MainActivity.instance.finish();
                System.exit(0);
            }
            //表示此按鍵已處理,不再交給系統處理,
            //從而避免游戲被切入後台
            return true;
        }
        //按鍵監聽事件函數根據游戲狀態不同進行不同監聽
        switch (gameState) {
        case GAME_MENU:
            break;
        case GAMEING:
            //主角的按鍵按下事件
            player.onKeyDown(keyCode, event);
            break;
        case GAME_PAUSE:
            break;
        case GAME_WIN:
            break;
        case GAME_LOST:
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 按鍵抬起事件監聽
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        //處理back返回按鍵
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //游戲勝利、失敗、進行時都默認返回菜單
            if (gameState == GAMEING || gameState == GAME_WIN || gameState == GAME_LOST) {
                gameState = GAME_MENU;
            }
            //表示此按鍵已處理,不再交給系統處理,
            //從而避免游戲被切入後台
            return true;
        }
        //按鍵監聽事件函數根據游戲狀態不同進行不同監聽
        switch (gameState) {
        case GAME_MENU:
            break;
        case GAMEING:
            //按鍵抬起事件
            player.onKeyUp(keyCode, event);
            break;
        case GAME_PAUSE:
            break;
        case GAME_WIN:
            break;
        case GAME_LOST:
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 游戲邏輯
     */
    private void logic() {
        //邏輯處理根據游戲狀態不同進行不同處理
        switch (gameState) {
        case GAME_MENU:
            break;
        case GAMEING:
            //背景邏輯
            backGround.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;
                        }
                    }
                }
                //每2秒添加一個敵機子彈
                countEnemyBullet++;
                if (countEnemyBullet % 40 == 0) {
                    for (int i = 0; i < vcEnemy.size(); i++) {
                        Enemy en = vcEnemy.elementAt(i);
                        //不同類型敵機不同的子彈運行軌跡
                        int bulletType = 0;
                        switch (en.type) {
                        //蒼蠅
                        case Enemy.TYPE_FLY:
                            bulletType = Bullet.BULLET_FLY;
                            break;
                        //鴨子
                        case Enemy.TYPE_DUCKL:
                        case Enemy.TYPE_DUCKR:
                            bulletType = Bullet.BULLET_DUCK;
                            break;
                        }
                        vcBullet.add(new Bullet(bmpEnemyBullet, en.x + 10, en.y + 20, bulletType));
                    }
                }
                //處理敵機子彈邏輯
                for (int i = 0; i < vcBullet.size(); i++) {
                    Bullet b = vcBullet.elementAt(i);
                    if (b.isDead) {
                        vcBullet.removeElement(b);
                    } else {
                        b.logic();
                    }
                }
                //處理敵機子彈與主角碰撞
                for (int i = 0; i < vcBullet.size(); i++) {
                    if (player.isCollsionWith(vcBullet.elementAt(i))) {
                        //發生碰撞,主角血量-1
                        player.setPlayerHp(player.getPlayerHp() - 1);
                        //當主角血量小於0,判定游戲失敗
                        if (player.getPlayerHp() <= -1) {
                            gameState = GAME_LOST;
                        }
                    }
                }
                //處理主角子彈與敵機碰撞
                for (int i = 0; i < vcBulletPlayer.size(); i++) {
                    //取出主角子彈容器的每個元素
                    Bullet blPlayer = vcBulletPlayer.elementAt(i);
                    for (int j = 0; j < vcEnemy.size(); j++) {
                        //添加爆炸效果
                        //取出敵機容器的每個元與主角子彈遍歷判斷
                        if (vcEnemy.elementAt(j).isCollsionWith(blPlayer)) {
                            vcBoom.add(new Boom(bmpBoom, vcEnemy.elementAt(j).x, vcEnemy.elementAt(j).y, 7));
                        }
                    }
                }
            } else {//Boss相關邏輯
                //每0.5秒添加一個主角子彈
                boss.logic();
                if (countPlayerBullet % 10 == 0) {
                    //Boss的沒發瘋之前的普通子彈
                    vcBulletBoss.add(new Bullet(bmpBossBullet, boss.x + 35, boss.y + 40, Bullet.BULLET_FLY));
                }
                //Boss子彈邏輯
                for (int i = 0; i < vcBulletBoss.size(); i++) {
                    Bullet b = vcBulletBoss.elementAt(i);
                    if (b.isDead) {
                        vcBulletBoss.removeElement(b);
                    } else {
                        b.logic();
                    }
                }
                //Boss子彈與主角的碰撞
                for (int i = 0; i < vcBulletBoss.size(); i++) {
                    if (player.isCollsionWith(vcBulletBoss.elementAt(i))) {
                        //發生碰撞,主角血量-1
                        player.setPlayerHp(player.getPlayerHp() - 1);
                        //當主角血量小於0,判定游戲失敗
                        if (player.getPlayerHp() <= -1) {
                            gameState = GAME_LOST;
                        }
                    }
                }
                //Boss被主角子彈擊中,產生爆炸效果
                for (int i = 0; i < vcBulletPlayer.size(); i++) {
                    Bullet b = vcBulletPlayer.elementAt(i);
                    if (boss.isCollsionWith(b)) {
                        if (boss.hp <= 0) {
                            //游戲勝利
                            gameState = GAME_WIN;
                        } else {
                            //及時刪除本次碰撞的子彈,防止重復判定此子彈與Boss碰撞、
                            b.isDead = true;
                            //Boss血量減1
                            boss.setHp(boss.hp - 1);
                            //在Boss上添加三個Boss爆炸效果
                            vcBoom.add(new Boom(bmpBoosBoom, boss.x + 25, boss.y + 30, 5));
                            vcBoom.add(new Boom(bmpBoosBoom, boss.x + 35, boss.y + 40, 5));
                            vcBoom.add(new Boom(bmpBoosBoom, boss.x + 45, boss.y + 50, 5));
                        }
                    }
                }
            }
            //每1秒添加一個主角子彈
            countPlayerBullet++;
            if (countPlayerBullet % 20 == 0) {
                vcBulletPlayer.add(new Bullet(bmpBullet, player.x + 15, player.y - 20, Bullet.BULLET_PLAYER));
            }
            //處理主角子彈邏輯
            for (int i = 0; i < vcBulletPlayer.size(); i++) {
                Bullet b = vcBulletPlayer.elementAt(i);
                if (b.isDead) {
                    vcBulletPlayer.removeElement(b);
                } else {
                    b.logic();
                }
            }
            //爆炸效果邏輯
            for (int i = 0; i < vcBoom.size(); i++) {
                Boom boom = vcBoom.elementAt(i);
                if (boom.playEnd) {
                    //播放完畢的從容器中刪除
                    vcBoom.removeElementAt(i);
                } else {
                    vcBoom.elementAt(i).logic();
                }
            }
            break;
        case GAME_PAUSE:
            break;
        case GAME_WIN:
            break;
        case GAME_LOST:
            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;
    }
}

以及勝利和失敗頁面

最後附上 MainActivit

public class MainActivity extends Activity {
    public static MainActivity instance;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        //設置全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //顯示自定義的SurfaceView視圖
        setContentView(new MySurfaceView(this));
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        MySurfaceView.gameState = MySurfaceView.GAME_MENU;
        super.onDestroy();
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved