亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

Android植物大戰僵尸小游戲

2020-04-11 11:10:58
字體:
來源:轉載
供稿:網友

Android植物大戰僵尸小游戲全部內容如下:

相關下載:Android植物大戰僵尸小游戲

具體代碼如下所示:

package com.example.liu.mygame;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.tools.DeviceTools;import com.example.liu.mygame.view.GameView;import android.os.Bundle;import android.app.Activity;import android.graphics.BitmapFactory;import android.view.Menu;import android.view.MotionEvent;public class MainActivity extends Activity { private GameView gameview; @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  init();  gameview = new GameView(this);  setContentView(gameview); } // 初始化游戲資源 private void init() {  // TODO Auto-generated method stub  // 獲取屏幕大小尺寸  Config.deviceWidth = DeviceTools.getDeviceInfo(this)[0];  Config.deviceHeight = DeviceTools.getDeviceInfo(this)[1];  // 得到原始圖片  Config.gameBK = BitmapFactory.decodeResource(getResources(),    R.drawable.bk);  // 獲取縮放比  Config.scaleWidth = Config.deviceWidth    / (float) Config.gameBK.getWidth();  Config.scaleHeight = Config.deviceHeight    / (float) Config.gameBK.getHeight();  // 處理圖片讓它成為目標圖片  Config.gameBK = DeviceTools.resizeBitmap(Config.gameBK);  Config.seedBank = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.seedbank));  // 繪制出卡片,不能進行等比縮放要進行目標大小的輸入控制  Config.seedFlower = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.seed_flower),    Config.seedBank.getWidth() / 10,    Config.seedBank.getHeight() * 8 / 10);  Config.seedPea = DeviceTools.resizeBitmap(BitmapFactory.decodeResource(    getResources(), R.drawable.seed_pea), Config.seedBank    .getWidth() / 10, Config.seedBank.getHeight() * 8 / 10);  // 初始化陽光圖片  Config.sun = DeviceTools.resizeBitmap(BitmapFactory.decodeResource(    getResources(), R.drawable.sun));  // 初始化子彈圖片  Config.bullet = DeviceTools.resizeBitmap(BitmapFactory.decodeResource(    getResources(), R.drawable.bullet));  // 初始化gameOver圖片  Config.gameOver = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.gameover));  // 初始化動態圖片幀  Config.flowerFrames[0] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_01));  Config.flowerFrames[1] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_02));  Config.flowerFrames[2] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_03));  Config.flowerFrames[3] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_04));  Config.flowerFrames[4] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_05));  Config.flowerFrames[5] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_06));  Config.flowerFrames[6] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_07));  Config.flowerFrames[7] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_1_08));  Config.peaFrames[0] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_01));  Config.peaFrames[1] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_02));  Config.peaFrames[2] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_03));  Config.peaFrames[3] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_04));  Config.peaFrames[4] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_05));  Config.peaFrames[5] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_06));  Config.peaFrames[6] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_07));  Config.peaFrames[7] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.p_2_08));  Config.zombieFrames[0] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_01));  Config.zombieFrames[1] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_02));  Config.zombieFrames[2] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_03));  Config.zombieFrames[3] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_04));  Config.zombieFrames[4] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_05));  Config.zombieFrames[5] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_06));  Config.zombieFrames[6] = DeviceTools.resizeBitmap(BitmapFactory    .decodeResource(getResources(), R.drawable.z_1_07)); } // 重寫onTouch觸摸響應事件,返回值為gameview中生成的onTouch事件值 @Override public boolean onTouchEvent(MotionEvent event) {  // TODO Auto-generated method stub  return gameview.onTouchEvent(event); } // 銷毀 @Override protected void onDestroy() {  super.onDestroy(); } // 停止 @Override protected void onPause() {  super.onPause(); } // 重啟 @Override protected void onResume() {  super.onResume(); }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;public class Bullet extends BaseModel { // 位置 private int locationX; private int locationY; // 生命 private boolean isAlife; // 子彈產生時間 private long birthTime = 0l; // X方向上的速度分量 // 根據幀數,確定移動時間,然后來確定移動方式 private float SpeedX = 10; public Bullet(int locationX, int locationY) {  this.locationX = locationX + 40;  this.locationY = locationY + 20;  this.isAlife = true;  // 獲取系統時間  birthTime = System.currentTimeMillis(); } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   // 移動   locationX += SpeedX;   // 如果圖片的Y軸坐標移動到超出屏幕或者說移動到與屏幕齊平,那么生命周期結束   if (locationX > Config.deviceWidth) {    // 去除子彈    isAlife = false;   }  }  canvas.drawBitmap(Config.bullet, locationX, locationY, paint); } @Override public int getModelWidth() {  // TODO Auto-generated method stub  return Config.bullet.getWidth(); } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.view.MotionEvent;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.TouchAble;import com.example.liu.mygame.view.GameView;public class EmplaceFlower extends BaseModel implements TouchAble { private int locationX; private int locationY; // 生命 private boolean isAlife; // 觸摸區域(矩形) private Rect touchArea; public EmplaceFlower(int locationX, int locationY) {  this.locationX = locationX;  this.locationY = locationY;  this.isAlife = true;  // 初始化觸摸響應矩形區域,與整體屏幕一致大小  touchArea = new Rect(0, 0, Config.deviceWidth, Config.deviceHeight); } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   canvas.drawBitmap(Config.flowerFrames[0], locationX, locationY,     paint);  } } @Override public boolean onTouch(MotionEvent event) {  // TODO Auto-generated method stub  int x = (int) event.getX();  int y = (int) event.getY();  // 如果點擊的地方是在矩形區域內,那么開始設置跟隨  if (touchArea.contains(x, y)) {   // 圖標跟隨   // switch中需要相應三個事件:按下、抬起、拖動   switch (event.getAction()) {   case MotionEvent.ACTION_DOWN:    break;   case MotionEvent.ACTION_MOVE:    // drawSelf方法已定,那么我們需要改變表示位置的兩個變量,同時也要改變響應點擊的區域touchArea    locationX = x - Config.flowerFrames[0].getWidth() / 2;    locationY = y - Config.flowerFrames[0].getHeight() / 2;    break;   case MotionEvent.ACTION_UP:    // 放手以后此移動中的實例的生命周期結束并在特定點產生新的固定的實例    isAlife = false;    // 交由GameView處理    GameView.getInstance().applay4Plant(locationX, locationY, this);    break;   }  }  return false; } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.view.MotionEvent;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.TouchAble;import com.example.liu.mygame.view.GameView;public class EmplacePea extends BaseModel implements TouchAble { private int locationX; private int locationY; // 生命 private boolean isAlife; // 觸摸區域(矩形) private Rect touchArea; public EmplacePea(int locationX, int locationY) {  this.locationX = locationX;  this.locationY = locationY;  this.isAlife = true;  // 初始化觸摸響應矩形區域  touchArea = new Rect(0, 0, Config.deviceWidth, Config.deviceHeight); } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   canvas.drawBitmap(Config.peaFrames[0], locationX, locationY, paint);  } } @Override public boolean onTouch(MotionEvent event) {  // TODO Auto-generated method stub  int x = (int) event.getX();  int y = (int) event.getY();  // 如果點擊的地方是在矩形區域內,那么開始設置跟隨  if (touchArea.contains(x, y)) {   // 圖標跟隨   // switch中需要相應三個事件:按下、抬起、拖動   switch (event.getAction()) {   case MotionEvent.ACTION_DOWN:    break;   case MotionEvent.ACTION_MOVE:    // drawSelf方法已定,那么我們需要改變表示位置的兩個變量,同時也要改變響應點擊的區域touchArea    locationX = x - Config.peaFrames[0].getWidth() / 2;    locationY = y - Config.peaFrames[0].getHeight() / 2;    break;   case MotionEvent.ACTION_UP:    // 放手以后此移動中的實例的生命周期結束并在特定點產生新的固定的實例    isAlife = false;    // 交由GameView處理    GameView.getInstance().applay4Plant(locationX, locationY, this);    break;   }  }  return false; } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.Plant;import com.example.liu.mygame.view.GameView;//豌豆射手實體類public class Flower extends BaseModel implements Plant { private int locationX; private int locationY; private boolean isAlife; // 圖片幀數組的下標 private int frameIndex = 0; // 一個標記通過此標記確定此處是否有植物 private int mapIndex; // 控制產生陽光的時間 private long lastBirthTime; // 擺動速度控制,兩幀一動 private boolean swingSpeed; public Flower(int locationX, int locationY, int mapIndex) {  this.locationX = locationX;  this.locationY = locationY;  this.mapIndex = mapIndex;  isAlife = true;  // 初始化時間用來確保初試時間與花的創造時間一致  lastBirthTime = System.currentTimeMillis();  swingSpeed = false; } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   // 這里繪入的bitmap就需要在繪制自己的時候換自己的幀動畫以形成動態效果   // 這個組在Config中已經初始化好了   canvas.drawBitmap(Config.flowerFrames[frameIndex], locationX,     locationY, paint);   // 用此變量讓數組變化   // 通過這樣的取模方法,可以讓這個frameIndex值不超過7   // 當frameIndex為8時會變為0,避免數組越界   if (!swingSpeed) {    frameIndex = (++frameIndex) % 8;    swingSpeed = false;   } else {    swingSpeed = true;   }   // 用此處判斷來確定每10秒一個陽光的產生   if (System.currentTimeMillis() - lastBirthTime > 10000) {    lastBirthTime = System.currentTimeMillis();    giveBirth2Sun();   }  } } // 產生陽光 // 陽光具有生命,然后兩種情況,被點擊則轉換狀態,移動到上方陽光的標志處,過一段時間不點擊則死亡消失 // 產生在花的位置上 private void giveBirth2Sun() {  // 首先要有陽光的圖層集合,處于第三層,那么就需要操作集合,就需要調用GameView.getInstance  GameView.getInstance().giveBrith2Sun(locationX, locationY); } @Override public int getModelWidth() {  // TODO Auto-generated method stub  return Config.flowerFrames[0].getWidth(); } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; } @Override public int getmapIndex() {  // TODO Auto-generated method stub  return mapIndex; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import android.util.Log;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.Plant;import com.example.liu.mygame.view.GameView;//豌豆射手實體類public class Pea extends BaseModel implements Plant { private int locationX; private int locationY; private boolean isAlife; // 圖片幀數組的下標 private int frameIndex = 0; // 一個標記通過此標記確定此處是否有植物 private int mapIndex; // 控制產生子彈的時間 private long lastBirthTime; // 擺動速度控制,兩幀一動 private boolean swingSpeed; public Pea(int locationX, int locationY, int mapIndex) {  this.locationX = locationX;  this.locationY = locationY;  this.mapIndex = mapIndex;  isAlife = true;  swingSpeed = false; } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   // 這里繪入的bitmap就需要在繪制自己的時候換自己的幀動畫以形成動態效果   // 這個組在Config中已經初始化好了   canvas.drawBitmap(Config.peaFrames[frameIndex], locationX,     locationY, paint);   // 用此變量讓數組變化   // 通過這樣的取模方法,可以讓這個frameIndex值不超過7   // 當frameIndex為8時會變為0,避免數組越界   if (!swingSpeed) {    frameIndex = (++frameIndex) % 8;    swingSpeed = false;   } else {    swingSpeed = true;   }   // 用此處判斷來確定每10秒一個子彈的產生   if (System.currentTimeMillis() - lastBirthTime > 10000) {    lastBirthTime = System.currentTimeMillis();    giveBirth2Bullet();   }  } } // 產生子彈 // 子彈具有生命,然后兩種情況,被點擊則轉換狀態,移動到上方子彈的標志處,過一段時間不點擊則死亡消失 // 產生在花的位置上 private void giveBirth2Bullet() {  // 首先要有子彈的圖層集合,處于第三層,那么就需要操作集合,就需要調用GameView.getInstance  GameView.getInstance().giveBirth2Bullet(locationX, locationY); } @Override public int getModelWidth() {  // TODO Auto-generated method stub  return Config.peaFrames[0].getWidth(); } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; } @Override public int getmapIndex() {  // TODO Auto-generated method stub  return mapIndex; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.util.Log;import android.view.MotionEvent;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.TouchAble;import com.example.liu.mygame.view.GameView;public class SeedFlower extends BaseModel implements TouchAble { private int locationX; private int locationY; // 生命 private boolean isAlife; // 觸摸區域(矩形) private Rect touchArea; public SeedFlower(int locationX, int locationY) {  this.locationX = locationX;  this.locationY = locationY;  this.isAlife = true;  // 初始化觸摸響應矩形區域  touchArea = new Rect(locationX, locationY, locationX    + Config.seedFlower.getWidth(), locationY    + Config.seedFlower.getHeight()); } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   canvas.drawBitmap(Config.seedFlower, locationX, locationY, paint);  } } @Override public boolean onTouch(MotionEvent event) {  // TODO Auto-generated method stub  // 獲取并傳入觸摸的X,Y坐標,getX() getY()獲取到的數據都是float型  int x = (int) event.getX();  int y = (int) event.getY();  if (touchArea.contains(x, y)) {   // 當觸摸點落在區域內則響應   // 生成安置狀態的花(優先級最高)   if (Config.sunlight >= 50) {    applay4EmplaceFlower();    return true;   }  }  return false; } // 通過GameView來請求生成一個安置狀態的花(優先級最高) private void applay4EmplaceFlower() {  // TODO Auto-generated method stub  GameView.getInstance().applay4EmplacePlant(locationX, locationY, this); } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.util.Log;import android.view.MotionEvent;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.TouchAble;import com.example.liu.mygame.view.GameView;public class SeedPea extends BaseModel implements TouchAble { private int locationX; private int locationY; // 生命 private boolean isAlife; // 觸摸區域(矩形) private Rect touchArea; public SeedPea(int locationX, int locationY) {  this.locationX = locationX;  this.locationY = locationY;  this.isAlife = true;  // 初始化觸摸響應矩形區域  touchArea = new Rect(locationX, locationY, locationX    + Config.seedPea.getWidth(), locationY    + Config.seedPea.getHeight()); } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   canvas.drawBitmap(Config.seedPea, locationX, locationY, paint);  } } @Override public boolean onTouch(MotionEvent event) {  // TODO Auto-generated method stub  // 獲取并傳入觸摸的X,Y坐標,getX() getY()獲取到的數據都是float型  int x = (int) event.getX();  int y = (int) event.getY();  if (touchArea.contains(x, y)) {   // 當觸摸點落在區域內則響應   // 生成安置狀態的豌豆(優先級最高)   if (Config.sunlight >= 100) {    applay4EmplacePea();    return true;   }  }  return false; } // 通過GameView來請求生成一個安置狀態的豌豆(優先級最高) private void applay4EmplacePea() {  // TODO Auto-generated method stub  GameView.getInstance().applay4EmplacePlant(locationX, locationY, this); } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.view.MotionEvent;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.TouchAble;public class Sun extends BaseModel implements TouchAble { // 位置 private int locationX; private int locationY; // 生命 private boolean isAlife; // 可觸摸區域 private Rect touchArea; // 陽光產生時間 private long birthTime; // 標示陽光的狀態 private SunState state; // 移動距離 private int DirectionDistanceX; private int DirectionDistanceY; // XY方向上的速度分量 // 根據幀數,確定移動時間,然后來確定移動方式 private float SpeedX; private float SpeedY; // 用此枚舉來標示陽光的狀態 // 兩個狀態:靜止、移動 // 移動過程中生命周期對陽光無效,靜止時生命周期有效 public enum SunState {  SHOW, MOVE } public Sun(int locationX, int locationY) {  this.locationX = locationX;  this.locationY = locationY;  this.isAlife = true;  // 初始化觸摸響應矩形區域  // 對于每個陽光來說能出沒的地方只有他這張圖片大小的區域  touchArea = new Rect(locationX, locationY, locationX    + Config.sun.getWidth(), locationY + Config.sun.getHeight());  // 獲取系統時間  birthTime = System.currentTimeMillis();  // 初始實例化為SHOW狀態  state = SunState.SHOW; } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (isAlife) {   if (state == SunState.SHOW) {    // 判斷當前系統時間如果比出生時間大5000毫秒那么陽光生命結束,消失    if (System.currentTimeMillis() - birthTime > Config.lifeTime) {     isAlife = false;    }   } else {// 對于move狀態的陽光的處理    // 移動    locationX -= SpeedX;    locationY -= SpeedY;    // 如果圖片的Y軸坐標移動到超出屏幕或者說移動到與屏幕齊平,那么生命周期結束    if (locationY <= 0) {     // 去除陽光     isAlife = false;     // 改變陽光值     Config.sunlight += 25;    }   }   canvas.drawBitmap(Config.sun, locationX, locationY, paint);  } } // 觸摸事件響應 @Override public boolean onTouch(MotionEvent event) {  // TODO Auto-generated method stub  // 獲取觸摸點  int x = (int) event.getX();  int y = (int) event.getY();  // 如果觸摸點在可觸摸區域內  if (touchArea.contains(x, y)) {   // 開始運動并且不可被點擊,同時可能會與上邊框產生碰撞事件   // 移動過程中也需要時間,如果這個收集時間中用了超過陽光生命值5秒的時間   // 那么我們需要在點擊以后改變陽光的狀態并刪除原本的靜態陽光   state = SunState.MOVE;   // 改變狀態以后,那么就要開始移動,移動的起點不一定,但是終點是一定的   // 移動的終點可以認為是條形框(seedBank)的左上角點   // 起始點就是此陽光圖片的左上角   // XY方向上的移動距離   DirectionDistanceX = locationX - Config.seedBankLocationX;   DirectionDistanceY = locationY;   // 移動速度分量的計算,具體幀數需要項目分析,這里設置為20幀   SpeedX = DirectionDistanceX / 20f;   SpeedY = DirectionDistanceY / 20f;   return true;  }  return false; } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.view.GameView;public class Zombie extends BaseModel { private int locationX; private int locationY; private boolean isAlife; // 僵尸位于的跑道,因為此僵尸只跟其所在的跑道內的植物、子彈等進行碰撞檢測 private int raceWay; // 因為僵尸是移動中的 所以他要有動畫幀的下標 private int frameIndex = 0; // 移動速度,每一幀移動3像素 private int peedX = 3; public Zombie(int locationX, int locationY, int raceWay) {  this.locationX = locationX;  this.locationY = locationY;  isAlife = true;  this.raceWay = raceWay; } // 在某跑道隨機產生僵尸,同時間隔一段時間出現一只僵尸 @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  if (locationX < 0) {   Config.game = false;  }  if (isAlife) {   canvas.drawBitmap(Config.zombieFrames[frameIndex], locationX,     locationY, paint);   frameIndex = (++frameIndex) % 7;   locationX -= peedX;   // 碰撞檢測,僵尸發起的此碰撞檢測   GameView.getInstance().checkCollision(this, raceWay);  } } @Override public int getModelWidth() {  // TODO Auto-generated method stub  return Config.zombieFrames[0].getWidth(); } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.entity;import android.graphics.Canvas;import android.graphics.Paint;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.view.GameView;public class ZombieManager extends BaseModel { // 一般需要顯示出現在屏幕上的實體才需要繼承BaseModel // 所以此處的僵尸控制器其實不需要繼承BaseModel // 但是為了與之前的flower和pea產生器相統一 // 效仿以前的模式減少工作量 // 在這里也進行繼承 private boolean isAlife; // 最后一只僵尸的產生時間 private long lastBirthTime; public ZombieManager() {  lastBirthTime = System.currentTimeMillis();  isAlife = true; } @Override public void drawSelf(Canvas canvas, Paint paint) {  // TODO Auto-generated method stub  // 此處不需要繪制出圖片,所以不需要draw,但是可以進行邏輯上的處理  if (System.currentTimeMillis() - lastBirthTime > 15000) {   lastBirthTime = System.currentTimeMillis();   giveBirth2Zombie();  } } private void giveBirth2Zombie() {  // 與GameView請求加入僵尸  GameView.getInstance().apply4AddZombie(); }} package com.example.liu.mygame.global;import java.util.HashMap;import android.graphics.Bitmap;import android.graphics.Point;//常量public class Config { public static float scaleWidth; public static float scaleHeight; public static int deviceWidth; public static int deviceHeight; public static Bitmap gameBK; public static Bitmap seedBank; public static Bitmap gameOver; // seedBank的位置X坐標 public static int seedBankLocationX; public static Bitmap seedFlower; public static Bitmap seedPea; // 陽光 public static Bitmap sun; // 陽光的生存時間5000毫秒 public static long lifeTime = 5000; // 現在的陽光值 public static int sunlight = 200; // 僵尸和植物圖片的高度差 public static int heightYDistance; // 子彈 public static Bitmap bullet; // 將圖片幀放入數組 public static Bitmap[] flowerFrames = new Bitmap[8]; public static Bitmap[] peaFrames = new Bitmap[8]; public static Bitmap[] zombieFrames = new Bitmap[7]; // 放置植物的點 public static HashMap<Integer, Point> plantPoints = new HashMap<Integer, Point>(); // 跑道 public static int[] raceWayYpoints = new int[5]; // 輸贏判斷標志 public static boolean game = true;} package com.example.liu.mygame.model;import android.graphics.Canvas;import android.graphics.Paint;public class BaseModel { // 基礎類,對于所有能展示在屏幕上的動態對象都要繼承自此類 // 位置 private int locationX; private int locationY; // 生命 private boolean isAlife; // 繪制自己,即移動 public void drawSelf(Canvas canvas, Paint paint) { } public int getModelWidth() {  return 0; } public int getLocationX() {  return locationX; } public void setLocationX(int locationX) {  this.locationX = locationX; } public int getLocationY() {  return locationY; } public void setLocationY(int locationY) {  this.locationY = locationY; } public boolean isAlife() {  return isAlife; } public void setAlife(boolean isAlife) {  this.isAlife = isAlife; }} package com.example.liu.mygame.model;// 所有需要種植在地上保持靜止的植物都要有這個接口public interface Plant { // 用于key public int getmapIndex();} package com.example.liu.mygame.model;import android.view.MotionEvent;public interface TouchAble { // 對于能接受觸摸事件的對象的一個公用接口 // 傳入MotionEvent事件 public boolean onTouch(MotionEvent event);} package com.example.liu.mygame.tools;import com.example.liu.mygame.global.Config;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Matrix;import android.util.DisplayMetrics;import android.util.Log;public class DeviceTools { private static int[] deviceWidthHeight = new int[2]; // 重新設置Bitmap的大小 public static Bitmap resizeBitmap(Bitmap bitmap) {  if (bitmap != null) {   int width = bitmap.getWidth();   int height = bitmap.getHeight();   Log.i("info", width + "," + height);   Matrix matrix = new Matrix();   matrix.postScale(Config.scaleWidth, Config.scaleHeight);   Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,     height, matrix, true);   return resizedBitmap;  } else {   return null;  } } // 重載 // 原因是找到的素材需要進行處理來適應手機屏幕,等比操作,但是如果合成的兩張材料圖不成比例 那么就不得不用這種重載來適應 // 首先傳入一個bitmap和期望的寬高 public static Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {  if (bitmap != null) {   // 獲取傳入的圖片寬高   int width = bitmap.getWidth();   int height = bitmap.getHeight();   // 傳入期望的寬高   int newWidth = w;   int newHeight = h;   // 縮放比   float scaleWidth = ((float) newWidth) / width;   float scaleHeight = ((float) newHeight) / height;   // 圖片矩陣對象,3X3矩陣   Matrix matrix = new Matrix();   // 把縮放比傳入期望矩陣   matrix.postScale(scaleWidth, scaleHeight);   // 生成期望的圖片   Bitmap resizeBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,     height, matrix, true);   return resizeBitmap;  } else {   return null;  } } // 獲取屏幕的寬高 // 在DisplayMetrics類中可以獲取屏幕的亮度,寬高,刷新率等相關信息 public static int[] getDeviceInfo(Context context) {  if ((deviceWidthHeight[0] == 0) && (deviceWidthHeight[1] == 0)) {   DisplayMetrics metrics = new DisplayMetrics();   ((Activity) context).getWindowManager().getDefaultDisplay()     .getMetrics(metrics);   deviceWidthHeight[0] = metrics.widthPixels;   deviceWidthHeight[1] = metrics.heightPixels;  }  return deviceWidthHeight; }} package com.example.liu.mygame.view;import java.util.ArrayList;import com.example.liu.mygame.R;import com.example.liu.mygame.entity.*;import com.example.liu.mygame.global.Config;import com.example.liu.mygame.model.BaseModel;import com.example.liu.mygame.model.Plant;import com.example.liu.mygame.model.TouchAble;import android.R.bool;import android.R.integer;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Point;import android.graphics.Typeface;import android.util.Log;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;//對于這種有賽道的游戲可以用以下方法//對于那種可以上下移動的游戲可以把所有精靈放在一個集合里 不用分圖層 每次繪制的時候要按照Y坐標進行排序//對于先畫出來的肯定是排在前面的 所以Y最小的排在前面即離屏幕上邊緣最近的精靈//那種游戲肯定會通過游戲引擎來進行開發public class GameView extends SurfaceView implements SurfaceHolder.Callback,  Runnable { private Canvas canvas; private Paint paint; private SurfaceHolder surfaceHolder; private boolean gameRunFlag; private Context context;// 用于存放圖片地址 // 把GameView當做總管理,所有的實體都向這里發送請求并處理 private static GameView gameView; private ArrayList<BaseModel> deadList;// 存放已消亡的實體,在拖動放手后實體會不顯示,但是還存在所以要進行清理 private ArrayList<BaseModel> gameLayout3;// 存放第三圖層中的實體; private ArrayList<BaseModel> gameLayout2;// 存放第二圖層中的實體 private ArrayList<BaseModel> gameLayout1;// 存放第一圖層中的實體 // 跑道從上至下 // 這些可以做一個封裝,放入一個for循環進行創建即可 private ArrayList<BaseModel> gameLayout4plant0; private ArrayList<BaseModel> gameLayout4plant1; private ArrayList<BaseModel> gameLayout4plant2; private ArrayList<BaseModel> gameLayout4plant3; private ArrayList<BaseModel> gameLayout4plant4; // 定義僵尸跑道 private ArrayList<BaseModel> gamelayout4zombie0; private ArrayList<BaseModel> gamelayout4zombie1; private ArrayList<BaseModel> gamelayout4zombie2; private ArrayList<BaseModel> gamelayout4zombie3; private ArrayList<BaseModel> gamelayout4zombie4; // 定義僵尸控制器,通過此控制器來使僵尸實現移動 private ZombieManager zombieManager; public GameView(Context context) {  super(context);  // TODO GameView  this.context = context;  paint = new Paint();  surfaceHolder = getHolder();  surfaceHolder.addCallback(this);  gameRunFlag = true;  gameView = this;  if (Config.game == false) {   canvas.drawBitmap(Config.gameOver, 0, 0, paint);  } } @Override public void surfaceCreated(SurfaceHolder holder) {  // TODO surfaceCreated  // 加載bitmap(圖片)  createElement();  new Thread(this).start(); } private void createElement() {  // TODO createElement  // 給植物與僵尸的高度差賦值  Config.heightYDistance = Config.zombieFrames[0].getHeight()    - Config.flowerFrames[0].getHeight();  // 給seedBank的X坐標賦初值  Config.seedBankLocationX = (Config.deviceWidth - Config.seedBank    .getWidth()) / 2;  // 初始化第三圖層  gameLayout3 = new ArrayList<BaseModel>();  // 當此方法被觸發時便會創建卡片對象  gameLayout2 = new ArrayList<BaseModel>();  SeedFlower seedFlower = new SeedFlower(    (Config.deviceWidth - Config.seedBank.getWidth()) / 2      + Config.seedFlower.getWidth() / 3      + Config.seedBank.getWidth() / 7,    Config.seedBank.getHeight() / 10);  SeedPea seedPea = new SeedPea(    (Config.deviceWidth - Config.seedBank.getWidth()) / 2      + Config.seedFlower.getWidth() / 7      + Config.seedBank.getWidth() / 7 * 2,    Config.seedBank.getHeight() / 10);  gameLayout2.add(seedFlower);  gameLayout2.add(seedPea);  // 添加安置狀態中的植物  gameLayout1 = new ArrayList<BaseModel>();  deadList = new ArrayList<BaseModel>();  gameLayout4plant0 = new ArrayList<BaseModel>();  gameLayout4plant1 = new ArrayList<BaseModel>();  gameLayout4plant2 = new ArrayList<BaseModel>();  gameLayout4plant3 = new ArrayList<BaseModel>();  gameLayout4plant4 = new ArrayList<BaseModel>();  // 僵尸跑道初始化  gamelayout4zombie0 = new ArrayList<BaseModel>();  gamelayout4zombie1 = new ArrayList<BaseModel>();  gamelayout4zombie2 = new ArrayList<BaseModel>();  gamelayout4zombie3 = new ArrayList<BaseModel>();  gamelayout4zombie4 = new ArrayList<BaseModel>();  // 初始化僵尸控制器  zombieManager = new ZombieManager();  // 放置植物的合適位置  for (int i = 0; i < 5; i++) {   for (int j = 0; j < 9; j++) {    Config.plantPoints.put(i * 10 + j, new Point(      (j + 2) * Config.deviceWidth / 11 - Config.deviceWidth        / 11 / 2, (i + 1) * Config.deviceHeight / 6));    if (j == 0) {     Config.raceWayYpoints[i] = (i + 1) * Config.deviceHeight       / 6;    }   }  } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,   int height) {  // TODO surfaceChanged } @Override public void surfaceDestroyed(SurfaceHolder holder) {  // TODO surfaceDestroyed } // 所有的動畫幀都由這個run方法來控制 // 控制動畫幀的時候要注意首先進行數據更新 然后在更新圖像 @Override public void run() {  // TODO run  while (gameRunFlag) {   synchronized (surfaceHolder) {    try {     // 為了形成動畫效果首先需要清理屏幕     // 加鎖避免很多線程同時繪制     canvas = surfaceHolder.lockCanvas();     // 繪入背景,最底層圖層     canvas.drawBitmap(Config.gameBK, 0, 0, paint);     // 繪入上方植物欄,倒數第二層圖層,僅覆蓋于背景之上     canvas.drawBitmap(Config.seedBank,       Config.seedBankLocationX, 0, paint);     // 數據更改操作     updateData();     // 繪入植物卡片(第二層)     ondraw(canvas);    } catch (Exception e) {     // TODO: handle exception    } finally {     // 解鎖并提交     surfaceHolder.unlockCanvasAndPost(canvas);     // CanvasAndPost必須要進行解鎖 不管程序有什么問題必須給用戶直觀完整的顯示過程     // 以防萬一的話 加入try catch    }   }   // 加入以下語句每次循環中休眠50毫秒減少一直循環的系統資源浪費   // 使用50毫秒的原因是在42幀及以上肉眼就會認為是流暢的,即1秒42張圖片,每次循環休眠50毫秒即20幀   // 如果把sleep放在synchronized中的話會出現程序每次遍歷完立刻睡眠然后再次遍歷沒有給其他進程事件運行會造成卡死   try {    Thread.sleep(40);   } catch (InterruptedException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }  } } private void updateData() {  // 在此方法中進行數據更新  // 清除deadList  deadList.clear();  // 遍歷第一圖層  for (BaseModel model : gameLayout1) {   if (!model.isAlife()) {    deadList.add(model);   }  }  // 遍歷第二圖層  for (BaseModel model : gameLayout2) {   if (!model.isAlife()) {    deadList.add(model);   }  }  // 遍歷第三圖層  for (BaseModel model : gameLayout3) {   if (!model.isAlife()) {    deadList.add(model);   }  }  // 遍歷五條跑道上的僵尸  for (BaseModel model : gamelayout4zombie0) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gamelayout4zombie1) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gamelayout4zombie2) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gamelayout4zombie3) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gamelayout4zombie4) {   if (!model.isAlife()) {    deadList.add(model);   }  }  // 遍歷五條跑道上的植物  for (BaseModel model : gameLayout4plant0) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gameLayout4plant1) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gameLayout4plant2) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gameLayout4plant3) {   if (!model.isAlife()) {    deadList.add(model);   }  }  for (BaseModel model : gameLayout4plant4) {   if (!model.isAlife()) {    deadList.add(model);   }  }  // 遍歷deadList集合  for (BaseModel model : deadList) {   // 在各個圖層列表中把它們移除   gameLayout1.remove(model);   gameLayout2.remove(model);   gameLayout3.remove(model);   gamelayout4zombie0.remove(model);   gamelayout4zombie1.remove(model);   gamelayout4zombie2.remove(model);   gamelayout4zombie3.remove(model);   gamelayout4zombie4.remove(model);  } } private void ondraw(Canvas canvas) {  // TODO ondraw  // 在此方法中進行繪圖作業  // 按照游戲的層次進行繪制,先畫游戲層次最下方的精靈  // 按照已經寫好的分層順序  // 繪制出陽光值  Paint paint2 = new Paint();  paint2.setTypeface(Typeface.DEFAULT_BOLD);  paint2.setTextSize(15);  canvas.drawText(Config.sunlight + "", Config.deviceWidth * 2 / 7,    Config.deviceHeight / 8, paint2);  // 僵尸控制器中的drawSelf實現僵尸移動  zombieManager.drawSelf(canvas, paint);  // 跑道應該處于第四層故放在上方先繪制出來  // 遍歷五條跑道調用drawSelf方法進行繪制植物  // 此處也可以進行方法的抽象 或者說應該把這些重復的代碼抽象為一個方法調用不同的值進去  for (BaseModel model : gameLayout4plant0) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gameLayout4plant1) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gameLayout4plant2) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gameLayout4plant3) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gameLayout4plant4) {   model.drawSelf(canvas, paint);  }  // 第三層(陽光)  for (BaseModel model : gameLayout3) {   model.drawSelf(canvas, paint);  }  // 第二層  for (BaseModel model : gameLayout2) {   model.drawSelf(canvas, paint);  }  // 遍歷五條跑道調用drawSelf方法進行繪制僵尸  // 此處也可以進行方法的抽象 或者說應該把這些重復的代碼抽象為一個方法調用不同的值進去  // 第二層是植物卡片,僵尸在經過第一行的時候應該可以擋住植物卡片  for (BaseModel model : gamelayout4zombie0) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gamelayout4zombie1) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gamelayout4zombie2) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gamelayout4zombie3) {   model.drawSelf(canvas, paint);  }  for (BaseModel model : gamelayout4zombie4) {   model.drawSelf(canvas, paint);  }  // 第一層  // gameLayout1比gameLayout2的層次要高故放在后面  for (BaseModel model : gameLayout1) {   model.drawSelf(canvas, paint);  }  /*   * private m=200; Paint paint3 = new Paint(); paint3.setAlpha(100);   * canvas.drawRect(100, 100, 200, m, paint3); m-=5; 設置半透明效果   * m的作用是可以讓這個半透明效果逐步消去, m的變化大小就可以理解為此植物的冷卻時間   */ } // 在這里重寫觸摸響應事件 @Override public boolean onTouchEvent(MotionEvent event) {  // TODO onTouchEvent  // 對于相應來說gameLayout1的優先級最高故放在gameLayout2上方  for (BaseModel model : gameLayout1) {   // 判定是否為touchAble的子類,只有是touchAble的子類才能響應   if (model instanceof TouchAble) {    // 然后進行onTouch事件,查看是否被點擊,如果點擊那么返回true    if (((TouchAble) model).onTouch(event)) {     return true;    }   }  }  // 遍歷第二層中的全部實體  for (BaseModel model : gameLayout2) {   // 判定是否為touchAble的子類,只有是touchAble的子類才能響應   if (model instanceof TouchAble) {    // 然后進行onTouch事件,查看是否被點擊,如果點擊那么返回true    if (((TouchAble) model).onTouch(event)) {     return true;    }   }  }  // 遍歷第三層中的全部實體  for (BaseModel model : gameLayout3) {   // 判定是否為touchAble的子類,只有是touchAble的子類才能響應   if (model instanceof TouchAble) {    // 然后進行onTouch事件,查看是否被點擊,如果點擊那么返回true    if (((TouchAble) model).onTouch(event)) {     return true;    }   }  }  return false; } // 獲取GameView的方法,讓GameView編程所有實體的總橋梁 public static GameView getInstance() {  return gameView; } // 添加EmplacePlant植物(優先級最高) public void applay4EmplacePlant(int locationX, int locationY,   BaseModel model) {  // TODO applay4EmplacePlant  // 相當于在進行數據更新,在onDraw中會從這里取出一個個元素進行繪制,繪制過程中如果這里還會有更新那么會產生沖突,所以需要在這里加入一個同步鎖  // 所有對于集合的操作都要加入同步鎖,鎖對象用surfaceHolder當得到此surfaceHolder鎖對象的時候才能夠進行操作  synchronized (surfaceHolder) {   // gameLayout1放的是正在安放狀態的植物,沒有放下   // new一個處于安置狀態的實體   // gameLayout1中只能有0~1個實例   if (gameLayout1.size() < 1) {    if (model instanceof SeedPea) {     gameLayout1.add(new EmplacePea(locationX, locationY));    } else {     gameLayout1.add(new EmplaceFlower(locationX, locationY));    }   }  } } public void applay4Plant(int locationX, int locationY, BaseModel baseModel) {  // TODO applay4Plant  // 安放靜態植物  // 以空間換時間,因為植物、子彈、僵尸、都在第四層所以對于這些來說把它們分為五個賽道從上至下五層  // 每條賽道上有兩個集合,一個是僵尸的集合,另一個是植物與子彈的集合,這樣分是為了碰撞檢測  // 為了減少碰撞檢測事件去除部分不必要的運算,故而分成很多層  // 循環這個可安放植物的HashMap,目的是拿出每個元素與locationX和locationY進行比較  // key的作用是讓每個Paint的標示不同  synchronized (surfaceHolder) {// 加鎖   Point point;   for (Integer key : Config.plantPoints.keySet()) {    // 找距離locationX與locationY最近而且處于目標地域上    point = Config.plantPoints.get(key);    if ((Math.abs(locationX - point.x) < Config.deviceWidth / 11 / 2)      && (Math.abs(locationY - point.y) < Config.deviceHeight / 6 / 2)) {     // 跑道標示     int raceWayIndex = 6;     for (int i = 0; i < Config.raceWayYpoints.length; i++) {      // 遍歷跑道的Y值      if (point.y == Config.raceWayYpoints[i]) {       // 如果Y值相等那么跑道確定       raceWayIndex = i;      }     }     if (isPlantExist(key, raceWayIndex)) {      // 跑道數出發事件      switch (raceWayIndex) {      case 0:       if (baseModel instanceof EmplacePea) {        gameLayout4plant0.add(new Pea(point.x, point.y,          key));        Config.sunlight -= 100;       } else {        gameLayout4plant0.add(new Flower(point.x,          point.y, key));        Config.sunlight -= 50;       }       break;      case 1:       if (baseModel instanceof EmplacePea) {        gameLayout4plant1.add(new Pea(point.x, point.y,          key));        Config.sunlight -= 100;       } else {        gameLayout4plant1.add(new Flower(point.x,          point.y, key));        Config.sunlight -= 50;       }       break;      case 2:       if (baseModel instanceof EmplacePea) {        gameLayout4plant2.add(new Pea(point.x, point.y,          key));        Config.sunlight -= 100;       } else {        gameLayout4plant2.add(new Flower(point.x,          point.y, key));        Config.sunlight -= 50;       }       break;      case 3:       if (baseModel instanceof EmplacePea) {        gameLayout4plant3.add(new Pea(point.x, point.y,          key));        Config.sunlight -= 100;       } else {        gameLayout4plant3.add(new Flower(point.x,          point.y, key));        Config.sunlight -= 50;       }       break;      case 4:       if (baseModel instanceof EmplacePea) {        gameLayout4plant4.add(new Pea(point.x, point.y,          key));        Config.sunlight -= 100;       } else {        gameLayout4plant4.add(new Flower(point.x,          point.y, key));        Config.sunlight -= 50;       }       break;      default:       break;      }     }    }   }  } } // 判斷此處是否已經有植物的方法 // key是標示,raceWayIndex用于確定應查哪一個跑道 private boolean isPlantExist(int key, int raceWayIndex) {  switch (raceWayIndex) {  case 0:   for (BaseModel model : gameLayout4plant0) {    // 首先 如果這個區域是繼承了Plant接口的子類(因為子彈和陽光不繼承Plant做以區別)    if (model instanceof Plant) {     // 然后此處的key與傳入的key相等     if (key == ((Plant) model).getmapIndex()) {      // 那么返回此處不能種植植物      return false;     }    }   }   break;  case 1:   for (BaseModel model : gameLayout4plant1) {    // 首先 如果這個區域是繼承了Plant接口的子類(因為子彈和陽光不繼承Plant做以區別)    if (model instanceof Plant) {     // 然后此處的key與傳入的key相等     if (key == ((Plant) model).getmapIndex()) {      // 那么返回此處不能種植植物      return false;     }    }   }   break;  case 2:   for (BaseModel model : gameLayout4plant2) {    // 首先 如果這個區域是繼承了Plant接口的子類(因為子彈和陽光不繼承Plant做以區別)    if (model instanceof Plant) {     // 然后此處的key與傳入的key相等     if (key == ((Plant) model).getmapIndex()) {      // 那么返回此處不能種植植物      return false;     }    }   }   break;  case 3:   for (BaseModel model : gameLayout4plant3) {    // 首先 如果這個區域是繼承了Plant接口的子類(因為子彈和陽光不繼承Plant做以區別)    if (model instanceof Plant) {     // 然后此處的key與傳入的key相等     if (key == ((Plant) model).getmapIndex()) {      // 那么返回此處不能種植植物      return false;     }    }   }   break;  case 4:   for (BaseModel model : gameLayout4plant4) {    // 首先 如果這個區域是繼承了Plant接口的子類(因為子彈和陽光不繼承Plant做以區別)    if (model instanceof Plant) {     // 然后此處的key與傳入的key相等     if (key == ((Plant) model).getmapIndex()) {      // 那么返回此處不能種植植物      return false;     }    }   }   break;  default:   break;  }  return true; } // 被Flower請求,用于產生陽光 public void giveBrith2Sun(int locationX, int locationY) {  // TODO giveBrith2Sun  // 先設置鎖住  synchronized (surfaceHolder) {   gameLayout3.add(new Sun(locationX, locationY));  } } // 被Pea請求,用于產生子彈 public void giveBirth2Bullet(int locationX, int locationY) {  // TODO Auto-generated method stub  // 先設置鎖住  synchronized (surfaceHolder) {// 加鎖   Point point;   for (Integer key : Config.plantPoints.keySet()) {    // 找距離locationX與locationY最近而且處于目標地域上    point = Config.plantPoints.get(key);    if ((Math.abs(locationX - point.x) < Config.deviceWidth / 11 / 2)      && (Math.abs(locationY - point.y) < Config.deviceHeight / 6 / 2)) {     // 跑道標示     int raceWayIndex = 6;     for (int i = 0; i < Config.raceWayYpoints.length; i++) {      // 遍歷跑道的Y值      if (point.y == Config.raceWayYpoints[i]) {       // 如果Y值相等那么跑道確定       raceWayIndex = i;      }     }     switch (raceWayIndex) {     case 0:      gameLayout4plant0.add(new Bullet(locationX, locationY));      break;     case 1:      gameLayout4plant1.add(new Bullet(locationX, locationY));      break;     case 2:      gameLayout4plant2.add(new Bullet(locationX, locationY));      break;     case 3:      gameLayout4plant3.add(new Bullet(locationX, locationY));      break;     case 4:      gameLayout4plant4.add(new Bullet(locationX, locationY));      break;     default:      break;     }    }   }  } } // 僵尸控制器產生相應,加入僵尸 public void apply4AddZombie() {  // TODO apply4AddZombie  // 先解鎖  synchronized (surfaceHolder) {   int raceWay = 0;   // 0~4的隨機數來進行跑到初始化   // Math.random()產生的是0~1的不包括1的隨機double型數字   raceWay = (int) (Math.random() * 5);   // Config.deviceWidth + 20是為了讓僵尸逐步走入屏幕   // Config.raceWayYpoints[raceWay] - Config.heightYDistance   // 是為了讓僵尸與植物的底對齊   switch (raceWay) {   case 0:    gamelayout4zombie0      .add(new Zombie(Config.deviceWidth + 20,        Config.raceWayYpoints[raceWay]          - Config.heightYDistance, raceWay));    break;   case 1:    gamelayout4zombie1      .add(new Zombie(Config.deviceWidth + 20,        Config.raceWayYpoints[raceWay]          - Config.heightYDistance, raceWay));    break;   case 2:    gamelayout4zombie2      .add(new Zombie(Config.deviceWidth + 20,        Config.raceWayYpoints[raceWay]          - Config.heightYDistance, raceWay));    break;   case 3:    gamelayout4zombie3      .add(new Zombie(Config.deviceWidth + 20,        Config.raceWayYpoints[raceWay]          - Config.heightYDistance, raceWay));    break;   case 4:    gamelayout4zombie4      .add(new Zombie(Config.deviceWidth + 20,        Config.raceWayYpoints[raceWay]          - Config.heightYDistance, raceWay));    break;   default:    break;   }  } } // 處理碰撞檢測,碰撞檢測的發起者是僵尸 public void checkCollision(Zombie zombie, int raceWay) {  // TODO Auto-generated method stub  synchronized (surfaceHolder) {   switch (raceWay) {   case 0:    for (BaseModel model : gameLayout4plant0) {     if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)       - (zombie.getLocationX() + zombie.getModelWidth() / 2)) < Math       .abs((model.getModelWidth() + zombie         .getModelWidth()) / 2)) {      if (model instanceof Plant) {       // 植物死       model.setAlife(false);      } else if (model instanceof Bullet) {       // 子彈死       model.setAlife(false);       // 僵尸死       zombie.setAlife(false);       model.setAlife(true);      }     }    }    break;   case 1:    for (BaseModel model : gameLayout4plant1) {     if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)       - (zombie.getLocationX() + zombie.getModelWidth() / 2)) < Math       .abs((model.getModelWidth() + zombie         .getModelWidth()) / 2)) {      if (model instanceof Plant) {       // 植物死       model.setAlife(false);      } else if (model instanceof Bullet) {       // 子彈死       model.setAlife(false);       // 僵尸死       zombie.setAlife(false);       model.setAlife(true);      }     }    }    break;   case 2:    for (BaseModel model : gameLayout4plant2) {     if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)       - (zombie.getLocationX() + zombie.getModelWidth() / 2)) < Math       .abs((model.getModelWidth() + zombie         .getModelWidth()) / 2)) {      if (model instanceof Plant) {       // 植物死       model.setAlife(false);      } else if (model instanceof Bullet) {       // 子彈死       model.setAlife(false);       // 僵尸死       zombie.setAlife(false);       model.setAlife(true);      }     }    }    break;   case 3:    for (BaseModel model : gameLayout4plant3) {     if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)       - (zombie.getLocationX() + zombie.getModelWidth() / 2)) < Math       .abs((model.getModelWidth() + zombie         .getModelWidth()) / 2)) {      if (model instanceof Plant) {       // 植物死       model.setAlife(false);      } else if (model instanceof Bullet) {       // 子彈死       model.setAlife(false);       // 僵尸死       zombie.setAlife(false);       model.setAlife(true);      }     }    }    break;   case 4:    for (BaseModel model : gameLayout4plant4) {     if (Math.abs((model.getLocationX() + model.getModelWidth() / 2)       - (zombie.getLocationX() + zombie.getModelWidth() / 2)) < Math       .abs((model.getModelWidth() + zombie         .getModelWidth()) / 2)) {      if (model instanceof Plant) {       // 植物死       model.setAlife(false);      } else if (model instanceof Bullet) {       // 子彈死       model.setAlife(false);       // 僵尸死       zombie.setAlife(false);       model.setAlife(true);      }     }    }    break;   default:    break;   }  } }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产日韩在线观看av| 国产精品色悠悠| 亚洲第一视频在线观看| 色综合伊人色综合网| 亚洲第一免费网站| 亚洲性生活视频| 亚洲欧洲一区二区三区在线观看| 久久久视频免费观看| 91久久在线播放| 欧美电影免费观看电视剧大全| 精品久久久久久中文字幕大豆网| 国产精品中文在线| 久久久av亚洲男天堂| 国产精品99久久99久久久二8| 国产精品一区二区av影院萌芽| 国产精品伦子伦免费视频| 久久91超碰青草是什么| 韩国美女主播一区| 久久在线观看视频| 色狠狠av一区二区三区香蕉蜜桃| 欧美日韩亚洲一区二区| www.欧美精品一二三区| 日本精品在线视频| 2021国产精品视频| 欧美最猛性xxxxx免费| 欧美精品成人在线| 亚洲人成伊人成综合网久久久| 亚洲欧美国产精品久久久久久久| 国产精品最新在线观看| 亚洲va欧美va在线观看| 55夜色66夜色国产精品视频| 国产91对白在线播放| 91精品久久久久久久久青青| 欧美最顶级丰满的aⅴ艳星| 国产精品pans私拍| 日韩av电影国产| 亚洲mm色国产网站| 成年无码av片在线| 68精品国产免费久久久久久婷婷| 国产视频精品va久久久久久| 久久av在线播放| 国产精品视频永久免费播放| 国产在线精品自拍| 欧美国产精品人人做人人爱| 欧美激情第6页| 亚洲成人动漫在线播放| 国产综合视频在线观看| 日本欧美精品在线| 欧美精品videosex牲欧美| 欧美一区二区三区精品电影| 国产精品美女久久久免费| 国产xxx69麻豆国语对白| 久久97精品久久久久久久不卡| 亚洲精品国产品国语在线| 欧美激情免费看| 久久天天躁狠狠躁夜夜爽蜜月| 久久6免费高清热精品| 亚洲精品美女在线观看| 97人洗澡人人免费公开视频碰碰碰| 91精品国产乱码久久久久久蜜臀| 欧美激情精品久久久久久免费印度| 日产日韩在线亚洲欧美| 九九热精品视频在线播放| 欧美视频在线视频| 精品久久久久久久久久久| 亚洲女同性videos| 在线精品视频视频中文字幕| 亚洲人成在线电影| 欧美中文字幕在线播放| 91精品国产综合久久久久久久久| 亚洲欧美日韩精品久久| 国产欧美日韩中文字幕在线| 中文字幕亚洲一区在线观看| 国产成+人+综合+亚洲欧洲| 亚洲bt欧美bt日本bt| 欧美日韩国产限制| 91香蕉亚洲精品| 国产一区二区三区在线免费观看| 久久久久久国产免费| 欧美在线激情视频| 成人做爽爽免费视频| 欧美日韩亚洲精品内裤| 欧美成人四级hd版| 欧美日韩国产精品一区| 国模精品视频一区二区| 在线观看久久av| 国产91网红主播在线观看| 精品久久久精品| 国产精品小说在线| 久久精品91久久久久久再现| 亚洲精品色婷婷福利天堂| 国产精品久久久久aaaa九色| 国产丝袜高跟一区| 久久久999国产精品| 欧美裸身视频免费观看| 俺去了亚洲欧美日韩| 88国产精品欧美一区二区三区| 欧美一级片在线播放| 欧美激情va永久在线播放| 91色视频在线导航| 欧美精品国产精品日韩精品| 成人免费看吃奶视频网站| 在线视频日本亚洲性| 日韩欧美黄色动漫| 成人免费在线网址| 亚洲直播在线一区| 国产欧美亚洲视频| 丝袜情趣国产精品| 久久欧美在线电影| 日韩hd视频在线观看| 亚洲视频在线免费观看| 69久久夜色精品国产69乱青草| 92裸体在线视频网站| 国产成人涩涩涩视频在线观看| 国产精品色午夜在线观看| 国产精品美女视频网站| 国产欧美久久久久久| 亚洲一区二区福利| 国产成人aa精品一区在线播放| 欧美在线一区二区视频| 日韩av日韩在线观看| 91久久精品日日躁夜夜躁国产| 国产激情久久久久| 欧美一级电影久久| 黑人精品xxx一区一二区| 宅男66日本亚洲欧美视频| 国产成人91久久精品| 亚洲美女免费精品视频在线观看| 5278欧美一区二区三区| 欧美视频在线观看免费网址| 九九九热精品免费视频观看网站| 欧美日韩xxxxx| 在线视频日韩精品| 日韩有码片在线观看| 欧美一级视频一区二区| 久久久精品2019中文字幕神马| 久久频这里精品99香蕉| 欧美激情视频在线免费观看 欧美视频免费一| 久热精品视频在线观看| 在线亚洲午夜片av大片| 欧美激情亚洲另类| 欧美—级高清免费播放| 亚洲丁香婷深爱综合| 欧美一区二区三区四区在线| 日韩中文字在线| 国产91九色视频| 久热国产精品视频| 一区二区三区无码高清视频| 久久免费视频在线| 欧美精品九九久久| 久久国产加勒比精品无码| 亚洲欧美国产精品va在线观看| 成人春色激情网| 欧美亚洲视频一区二区| 精品高清一区二区三区| 国产精品视频男人的天堂| 国产一区二区三区三区在线观看| 国产精品网址在线| 亚洲成人教育av| 在线亚洲欧美视频| 亚洲精品视频网上网址在线观看| 国产精品国产三级国产aⅴ浪潮| 国产精品观看在线亚洲人成网| 国产精彩精品视频|