Android植物大战僵尸小游戏
程序员文章站
2023-12-14 22:08:22
android植物大战僵尸小游戏全部内容如下:
相关下载:android植物大战僵尸小游戏
具体代码如下所示:
package com.example....
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; } } } }