ubuntu 下編譯C++代碼湧現的成績處理。本站提示廣大學習愛好者:(ubuntu 下編譯C++代碼湧現的成績處理)文章只能為提供參考,不一定能成為您想要的結果。以下是ubuntu 下編譯C++代碼湧現的成績處理正文
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; } } 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; case BULLET_BOSS: // Boss猖狂狀況下的槍彈邏輯待完成 // 界限處置 if (bulletY > MySurfaceView.screenH || bulletY <= -40 || bulletX > MySurfaceView.screenW || bulletX <= -40) { isDead = true; } break; } } }
在在MySurfacview外面挪用 生成槍彈配角的和本身的
package com.gsf; import java.util.Random; import java.util.Vector; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; 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; // 敵機槍彈容器 private Vector<Bullet> vcBullet; // 添加槍彈的計數器 private int countEnemyBullet; // 配角槍彈容器 private Vector<Bullet> vcBulletPlayer; // 添加槍彈的計數器 private int countPlayerBullet; /** * 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); //敵機槍彈容器實例 vcBullet = new Vector<Bullet>(); //配角槍彈容器實例 vcBulletPlayer = new Vector<Bullet>(); // 實例敵機容器 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); } //敵機槍彈繪制 for (int i = 0; i < vcBullet.size(); i++) { vcBullet.elementAt(i).draw(canvas, paint); } } else { // boss 繪制 } // 處置配角槍彈繪制 for (int i = 0; i < vcBulletPlayer.size(); i++) { vcBulletPlayer.elementAt(i).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(); // 敵機邏輯 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++; } } // 每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(); } } //每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 < vcEnemy.size(); i++) { if (player.isCollsionWith(vcEnemy.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 b = vcBulletPlayer.elementAt(i); if (b.isDead) { vcBulletPlayer.removeElement(b); } else { b.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; } }
如今槍彈碰撞配角 並沒有產生工作 須要槍彈和配角的碰撞檢測
//修正Player類 添加碰撞檢測 // // 斷定碰撞(配角與敵機槍彈) public boolean isCollsionWith(Bullet bullet) { // 能否處於無敵時光 if (isCollision == false) { 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 + 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; } }
在主界面 MySrufaceView 的邏輯函數中添加 配角和敵機槍彈的碰撞
//處置敵機槍彈與配角碰撞 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; } } }
固然 配角的槍彈碰撞了敵機 也須要停止碰撞檢測 修正 敵機類 Enemy
//斷定碰撞(敵機與配角槍彈碰撞) 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; } //產生碰撞,讓其逝世亡 isDead = true; return true; }
在主視圖中添加邏輯 配角槍彈和敵機碰撞的邏輯
//處置配角槍彈與敵機碰撞 for (int i = 0; i < vcBulletPlayer.size(); i++) { //掏出配角槍彈容器的每一個元素 Bullet blPlayer = vcBulletPlayer.elementAt(i); for (int j = 0; j < vcEnemy.size(); j++) { //添加爆炸後果 待完成 } }
下面完成 的後果圖
上面完成爆炸的後果
新建一個爆炸類 Boom
/** * 剎時爆炸 類 * @author liuml * @time 2016-6-1 上午11:32:56 */ public class Boom { //爆炸後果資本圖 private Bitmap bmpBoom; //爆炸後果的地位坐標 private int boomX, boomY; //爆炸動畫播放以後的幀下標 private int cureentFrameIndex; //爆炸後果的總幀數 private int totleFrame; //每幀的寬高 private int frameW, frameH; //能否播放終了,優化處置 public boolean playEnd; //爆炸後果的結構函數 public Boom(Bitmap bmpBoom, int x, int y, int totleFrame) { this.bmpBoom = bmpBoom; this.boomX = x; this.boomY = y; this.totleFrame = totleFrame; frameW = bmpBoom.getWidth() / totleFrame; frameH = bmpBoom.getHeight(); } //爆炸後果繪制 public void draw(Canvas canvas, Paint paint) { canvas.save(); canvas.clipRect(boomX, boomY, boomX + frameW, boomY + frameH); canvas.drawBitmap(bmpBoom, boomX - cureentFrameIndex * frameW, boomY, paint); canvas.restore(); } //爆炸後果的邏輯 public void logic() { if (cureentFrameIndex < totleFrame) { cureentFrameIndex++; } else { playEnd = true; } } }
上面就照樣老套路了 在主界面 聲明爆炸容器 然後繪制 然後是邏輯完成
package com.gsf; import java.util.Random; import java.util.Vector; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; 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; // 敵機槍彈容器 private Vector<Bullet> vcBullet; // 添加槍彈的計數器 private int countEnemyBullet; // 配角槍彈容器 private Vector<Bullet> vcBulletPlayer; // 添加槍彈的計數器 private int countPlayerBullet; //爆炸後果容器 private Vector<Boom> vcBoom; /** * 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); //敵機槍彈容器實例 vcBullet = new Vector<Bullet>(); //配角槍彈容器實例 vcBulletPlayer = new Vector<Bullet>(); // 實例敵機容器 vcEnemy = new Vector<Enemy>(); // 實例隨機庫 random = new Random(); //爆炸後果容器實例 vcBoom = new Vector<Boom>(); } /** * 游戲畫圖 */ 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); } //敵機槍彈繪制 for (int i = 0; i < vcBullet.size(); i++) { vcBullet.elementAt(i).draw(canvas, paint); } //爆炸後果繪制 for (int i = 0; i < vcBoom.size(); i++) { vcBoom.elementAt(i).draw(canvas, paint); } } else { // boss 繪制 } // 處置配角槍彈繪制 for (int i = 0; i < vcBulletPlayer.size(); i++) { vcBulletPlayer.elementAt(i).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(); // 敵機邏輯 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++; } } // 每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(); } } //每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 < vcEnemy.size(); i++) { if (player.isCollsionWith(vcEnemy.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 b = vcBulletPlayer.elementAt(i); if (b.isDead) { vcBulletPlayer.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)); } } } } 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; } }
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。