anroid植物大战僵尸简易版小游戏
# 前言
提示:
小游戏植物大战僵尸,这个是随便做的,不是大项目,所以做的没那么认真
提示:以下是本篇文章正文内容,下面案例可供参考
一.游戏背景
游戏植物大战僵尸作为一个很火的电脑游戏,现在应用到手机做一个app会成为许多人热捧的一款游戏 项目展示二、代码内容解析
1.项目展示
代码如下(示例):
开局界面如下
游戏进行界面:
大体上就是这样
,植物原地不懂,但可以摇动,僵尸可以走动,僵尸会吃掉植物在引动过程中,有一个僵尸通过草坪边缘就算僵尸成功,僵尸数量没有上线。
## 2.图片调用的说明
<font color=#999AAA >
主界面的设置
> package com.cyzyedu.pvz;
>
>
> import android.app.Activity; import android.app.DirectAction; import
> android.os.Bundle;
>
> import org.cocos2d.layers.CCScene; import
> org.cocos2d.nodes.CCDirector; import
> org.cocos2d.opengl.CCGLSurfaceView;
>
> public class MainActivity extends Activity {
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> CCGLSurfaceView view = new CCGLSurfaceView(this);
> setContentView(view);
>
> CCDirector director = CCDirector.sharedDirector();
> director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
> director.attachInView(view);
> CCScene scene = CCScene.node(); // FirstPlayer firstPlayer = new FirstPlayer();
> scene.addChild(new GameLayer());
> //下面必须设置,否则出错
> // 宽度1280给后面留出马路,高度跟地图高度一致,这样才能显示全图
> director.setScreenSize(1280,768);
>
> director.runWithScene(scene);
> director.setDisplayFPS(true);
> }
>
> @Override
> protected void onDestroy() {
> super.onDestroy();
> System.exit(0);
> } }
将会呈现如下的效果:
如上图
植物的摇动和僵尸的移动是通过9张分别植物在不同状态下的动作图片在循环中不断被调用
植物:
> public Plants(CGPoint point) {
> super("Peashooter/Frame00.png");
> setAnchorPoint(0, 0);
> setPosition(point);
> shake();
> }
> public void shake() {
> CCAction animate = CommonUtils.animate("Peashooter/Frame%02d.png", 12, true);
> this.runAction(animate);
> }
僵尸:
> public Pea() {
> super("pea/bullet.png");
> this.setActive(false);
> }
> public void move() {
> float t = (1280 - getPosition().x) / speed;
> CCMoveTo ccMoveTo = CCMoveTo.action(t, ccp(1280, getPosition().y));
> CCSequence ccSequence = CCSequence.actions(ccMoveTo, CCCallFunc.action(this, "destroy"));
> this.runAction(ccSequence);
> }
僵尸和植物的声明值攻击速度的描述,其实都是僵尸和植物类的成员
private int attack = 10;
private int speed = 100;
当植物和僵尸的血量低于100时,即销毁这个植物或者这个僵尸
public void destroy() {
this.active = false;
removeSelf();
}
僵尸随机出现
* 随机出现僵尸
public void generateZombies(float f) { CGPoint mapPosition = map.getPosition(); if (mapPosition.x == 0 && mapPosition.y == 0) { ArrayList<CGPoint> pathList = CommonUtils.loadPoint(map, "path"); Random random = new Random(); int line = random.nextInt(5); CGPoint startPoint = pathList.get(line * 2); CGPoint endPoint = pathList.get(line * 2 + 1); Zombies zombies = new Zombies(startPoint, endPoint); map.addChild(zombies); zombiesArrayList.add(zombies); } }
子弹由植物射出
public void attack(Plants plants) {
this.plants = plants;
this.stopAllActions();
CCAction animate =
CommonUtils.animate(“zombies/attack/Frame%02d.png”, 7, true);
this.runAction(animate);
CCScheduler scheduler = CCScheduler.sharedScheduler();
scheduler.schedule(“attackPlant”, this, 1, false);
}
子弹攻击到僵尸
public void attackPlant(float f) {
if (plants != null) {
plants.attacked(attack);
if (plants.getLife()<0){
this.stopAllActions();
CCScheduler.sharedScheduler().unschedule(“attackPlant”,this);
setAttack(false);
move();
}
}
}
僵尸或者植物死亡即生命死亡
public void attacked(int attack) {
life -= attack;
if(life<=0){
destroy();
}
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了植物大战僵尸游戏的简要解析。
作者:ggj只爱服务器
网址:https://editor.csdn.net/md?not_checkout=1&articleId=121892619