Cocos2d-x简单游戏<植物大战僵尸>代码实现|第七部分:僵尸类<后续会提供源码下载链接>

这个植物大战僵尸的小游戏Demo 虽然下,但是基本包括了:

  1.植物的选取、僵尸的攻击、发射子弹;

  2.太阳的生成、碰撞检测等功能;

 

 

 第一部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第一部分:开始场景

 第二部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第二部分:菜单场景

 第三部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第三部分:通关场景

 第四部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第四部分:通关失败场景

 第五部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第五部分:公用类

 第六部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第六部分:植物类

 第七部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第七部分:僵尸类

 第八部分:Cocos2d-x简单游戏<植物大战僵尸>代码实现|第八部分:子弹类

 第九部分<A>Cocos2d-x简单游戏<植物大战僵尸>代码实现|第九部分:游戏场景GameScene.h

 第九部分<B>Cocos2d-x简单游戏<植物大战僵尸>代码实现|第九部分:游戏场景GameScene.cpp


#ifndef __MyTest__Zombies__

#define __MyTest__Zombies__


#include <iostream>

#include "CommonHeader.h"



#define LIFEVALUE 10;

typedef enum

{

    Zombie=0,//正常僵尸

    BucketheadZombie,//头上戴水桶的僵尸

    ConeheadZombie,//头上戴路障牌的僵尸

    FlagZombie,//手拿红旗的僵尸

    PoleVaultingZombie,//手拿撑杆的僵尸

    PoleVaultingZombieWalk,

}ZoombieType;

class Zoombie:publicCCSprite

{

public:

   ZoombieType zoombieType;

   double blood;//血量

   char plistStr[50];//加载的纹理图册的名字

   char attackStr[50];//不同类型攻击纹理图册

   char dieStr[50];//加载死亡的纹理图集名字

    Zoombie(ZoombieType zoombieType);//构造函数

   void runZombieAction(char* plist);//僵尸正常行走的action

   void loadSpriteFrameFiles(); //预先加载纹理图集

   void changeIsJump();

   void unPoleVaultingZombieRun();

    

   bool isAttack;

   bool isJump;

   void runAttackAction();//运行攻击的方法

   void runDieAction();//死亡

    //    void reduceBlood();  //减少血量

   void startMove(); //开始移动

   void remove();

   void runBoomDie();//被炸死

   void continueMove();

   void Jump();//僵尸跳过植物

   void Jump2();//僵尸跳过植物

};





#endif /* defined(__MyTest__Zombies__) */



#include "Zombies.h"

Zoombie::Zoombie(ZoombieType zoombieType)

{

    this->loadSpriteFrameFiles();

   isAttack =false;

   switch (zoombieType) {

       case0:

           this->zoombieType=zoombieType;

           this->blood=1*LIFEVALUE;

            this->initWithSpriteFrameName("Zombie1.png");

           this->setScale(0.5);

           sprintf(plistStr,"Zombie");

           break;

       case1:

           this->zoombieType=zoombieType;

            this->initWithSpriteFrameName("BucketheadZombie1.png");

           this->setScale(0.5);

           this->blood=1.5*LIFEVALUE;

            sprintf(plistStr,"BucketheadZombie");

           break;

       case2:

           this->zoombieType=zoombieType;

            this->initWithSpriteFrameName("ConeheadZombie1.png");

           this->blood=LIFEVALUE;

           this->setScale(0.5);

           sprintf(plistStr,"ConeheadZombie");

           break;

       case3:

           this->zoombieType=zoombieType;

            this->initWithSpriteFrameName("FlagZombie1.png");

           this->blood=LIFEVALUE;

           this->setScale(0.5);

           sprintf(plistStr,"FlagZombie");

           break;

       case4:

           this->zoombieType=zoombieType;

            this->initWithSpriteFrameName("PoleVaultingZombie1.png");

           this->setScale(0.5);

           this->blood=LIFEVALUE;

           this->isJump =false;

            sprintf(plistStr,"PoleVaultingZombie");

           break;

       case5:

           this->zoombieType=zoombieType;

            this->initWithSpriteFrameName("PoleVaultingZombieWalk1.png");

           this->setScale(0.5);

           this->blood=LIFEVALUE;

           this->isJump =false;

            sprintf(plistStr,"PoleVaultingZombieWalk");

           break;

            

       default:

           break;

    }

   this->setScaleY(0.3);

    runZombieAction(plistStr);

}


voidZoombie::loadSpriteFrameFiles()

{

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Zombie_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("BucketheadZombie_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("ConeheadZombie_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("FlagZombie_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("PoleVaultingZombie_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("PoleVaultingZombieWalk_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("PoleVaultingZombieJump_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("PoleVaultingZombieJump2_default.plist");

}


voidZoombie::continueMove()

{

    this->stopAllActions();

    switch (zoombieType) {

       case0:

            

           sprintf(plistStr,"Zombie");

           break;

       case1:

            

            sprintf(plistStr,"BucketheadZombie");

           break;

       case2:

            

           sprintf(plistStr,"ConeheadZombie");

           break;

       case3:

            

           sprintf(plistStr,"FlagZombie");

           break;

       case4:

            sprintf(plistStr,"PoleVaultingZombie");

           break;

       case5:

            sprintf(plistStr,"PoleVaultingZombieWalk");

           break;

       default:

           break;

    }

    runZombieAction(plistStr);

    

}


//根据僵尸类型类型的不同执行不同的动作

voidZoombie::runZombieAction(char* plist)

{

   char name[50];

   sprintf(name,"%s_default.plist",plist);

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name);

    CCDictionary* dic=CCDictionary::createWithContentsOfFile(name);

    dic->retain();

    CCDictionary* a=(CCDictionary*)dic->objectForKey("frames");

    a->retain();

   int num=a->allKeys()->count();

   CCArray* allFrames=CCArray::createWithCapacity(10);

    allFrames->retain();

   for (int i=0; i<num; i++) {

       char frame[50];

       sprintf(frame,"%s%d.png",plist,i+1);

        

        CCSpriteFrame* frames=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame);

        allFrames->addObject(frames);

    }

   CCAnimation* animation=CCAnimation::createWithSpriteFrames(allFrames,0.08f);

    allFrames->release();

   CCAnimate* animate=CCAnimate::create(animation);

    CCRepeatForever* forever=CCRepeatForever::create(animate);

   this->runAction(forever);

}


voidZoombie::runDieAction()

{

   this->blood =0;

   int type=this->zoombieType;

    this->stopAllActions();

   switch (type) {

       case0:

       case1:

       case2:

           sprintf(dieStr,"ZombieDie");

           break;

       case3:

           sprintf(dieStr,"ZombieDie");

           break;

       case4:

            sprintf(dieStr,"PoleVaultingZombieDie");

           break;

       default:

           break;

    }

   char name[50];

    sprintf(name,"%s_default.plist",dieStr);

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name);

    CCDictionary* dic=CCDictionary::createWithContentsOfFile(name);

    dic->retain();

    CCDictionary* a=(CCDictionary*)dic->objectForKey("frames");

    a->retain();

   int num=a->allKeys()->count();

   CCArray* allFrames=CCArray::createWithCapacity(10);

    allFrames->retain();

   for (int i=0; i<num; i++) {

       char frame[50];

       sprintf(frame,"%s%d.png",dieStr,i+1);

        

        CCSpriteFrame* frames=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame);

        allFrames->addObject(frames);

    }

   CCAnimation* animation=CCAnimation::createWithSpriteFrames(allFrames,0.2f);

    allFrames->release();

   CCAnimate* animate=CCAnimate::create(animation);

    //CCRepeatForever* forever=CCRepeatForever::create(animate);

    

    CCCallFunc* func=CCCallFunc::create(this,callfunc_selector(Zoombie::remove));

   CCSequence* seq=CCSequence::create(animate,func,NULL);

    //CCRepeatForever* forever=CCRepeatForever::create(animate);

   this->runAction(seq);

    //this->removeFromParent();

    

}


voidZoombie::runAttackAction()

{int type=this->zoombieType;

    this->stopAllActions();

   switch (type) {

       case0:

           sprintf(attackStr,"ZombieAttack");

           break;

       case1:

            sprintf(attackStr,"BucketheadZombieAttack");

           break;

       case2:

            sprintf(attackStr,"ConeheadZombieAttack");

           break;

       case3:

            sprintf(attackStr,"FlagZombieAttack");

           break;

       case4:

            sprintf(attackStr,"PoleVaultingZombieAttack");

           break;

       case5:

            sprintf(attackStr,"PoleVaultingZombieAttack");

           break;

       default:

           break;

    }

    this->runZombieAction(attackStr);

    

}


voidZoombie::runBoomDie()

{

    this->stopAllActions();

   char name[50];

    sprintf(name,"BoomDie_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name);

    CCDictionary* dic=CCDictionary::createWithContentsOfFile(name);

    dic->retain();

    CCDictionary* a=(CCDictionary*)dic->objectForKey("frames");

    a->retain();

   int num=a->allKeys()->count();

   CCArray* allFrames=CCArray::createWithCapacity(10);

    allFrames->retain();

   for (int i=0; i<num; i++) {

       char frame[50];

       sprintf(frame,"BoomDie%d.png",i+1);

        

        CCSpriteFrame* frames=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame);

        allFrames->addObject(frames);

    }

   CCAnimation* animation=CCAnimation::createWithSpriteFrames(allFrames,0.1);

    allFrames->release();

   CCAnimate* animate=CCAnimate::create(animation);

    CCCallFunc* func=CCCallFunc::create(this,callfunc_selector(Zoombie::remove));

   CCSequence* seq=CCSequence::create(animate,func,NULL);

    //CCRepeatForever* forever=CCRepeatForever::create(animate);

   this->runAction(seq);

    

}


voidZoombie::startMove()

{

    //要根据距离计算出时间(让速度是一个恒定的值)

   float x =this->getPosition().x;

    //    float y = this->getPosition().y;

   float distance = (x+70);

   float time = distance*0.1;

   CCMoveTo* move=CCMoveTo::create(time,ccp(-70,this->getPosition().y));

    CCCallFunc* func=CCCallFunc::create(this,callfunc_selector(Zoombie::remove));

   CCSequence* seq=CCSequence::create(move,func,NULL);

   this->runAction(seq);

}


voidZoombie::remove()

{

    // this->removeFromParentAndCleanup(true);

    this->removeFromParent();

}


voidZoombie::Jump()

{

   this->isJump =true;

    this->stopAllActions();

   char name[50];

    sprintf(name,"PoleVaultingZombieJump_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name);

    CCDictionary* dic=CCDictionary::createWithContentsOfFile(name);

    dic->retain();

    CCDictionary* a=(CCDictionary*)dic->objectForKey("frames");

    a->retain();

   int num=a->allKeys()->count();

   CCArray* allFrames=CCArray::createWithCapacity(10);

    allFrames->retain();

   for (int i=0; i<num; i++) {

       char frame[50];

        sprintf(frame,"PoleVaultingZombieJump%d.png",i+1);

        

        CCSpriteFrame* frames=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame);

        allFrames->addObject(frames);

    }

   CCAnimation* animation=CCAnimation::createWithSpriteFrames(allFrames,0.2);

    allFrames->release();

   CCAnimate* animate=CCAnimate::create(animation);

    CCCallFunc* func=CCCallFunc::create(this,callfunc_selector(Zoombie::Jump2));

   CCMoveBy* moveBy =CCMoveBy::create(1.5,ccp(-25,0));

   CCSpawn* spawn =CCSpawn::create(animate,moveBy,NULL);

   CCSequence* seq=CCSequence::create(spawn,func,NULL);

    //CCRepeatForever* forever=CCRepeatForever::create(animate);

   this->runAction(seq);

    

}


voidZoombie::Jump2()

{

    this->stopAllActions();

    this->initWithSpriteFrameName("PoleVaultingZombieJump21.png");

   char name[50];

    sprintf(name,"PoleVaultingZombieJump2_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name);

    CCDictionary* dic=CCDictionary::createWithContentsOfFile(name);

    dic->retain();

    CCDictionary* a=(CCDictionary*)dic->objectForKey("frames");

    a->retain();

   CCPoint point =this->getPosition();

   this->setPosition(ccp(point.x-45, point.y));

   int num=a->allKeys()->count();

   CCArray* allFrames=CCArray::createWithCapacity(10);

    allFrames->retain();

   for (int i=0; i<num; i++) {

       char frame[50];

        sprintf(frame,"PoleVaultingZombieJump2%d.png",i+1);

        

        CCSpriteFrame* frames=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame);

        allFrames->addObject(frames);

    }

   CCAnimation* animation=CCAnimation::createWithSpriteFrames(allFrames,0.1);

    allFrames->release();

   CCAnimate* animate=CCAnimate::create(animation);

    CCCallFunc* func1 =CCCallFunc::create(this,callfunc_selector(Zoombie::changeIsJump));

    CCCallFunc* func2 =CCCallFunc::create(this,callfunc_selector(Zoombie::unPoleVaultingZombieRun));

   CCSequence* seq =CCSequence::create(animate, func1, func2,NULL);

   this->runAction(seq);

    

}


voidZoombie::changeIsJump()

{

    //this->isJump = false;

}


voidZoombie::unPoleVaultingZombieRun()

{

    this->stopAllActions();

   char name[50];

    sprintf(name,"PoleVaultingZombieWalk_default.plist");

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(name);

    CCDictionary* dic=CCDictionary::createWithContentsOfFile(name);

    dic->retain();

    CCDictionary* a=(CCDictionary*)dic->objectForKey("frames");

    a->retain();

   int num=a->allKeys()->count();

   CCArray* allFrames=CCArray::createWithCapacity(10);

    allFrames->retain();

   for (int i=0; i<num; i++) {

       char frame[50];

        sprintf(frame,"PoleVaultingZombieWalk%d.png",i+1);

        

        CCSpriteFrame* frames=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(frame);

        allFrames->addObject(frames);

    }

   CCAnimation* animation=CCAnimation::createWithSpriteFrames(allFrames,0.1);

    allFrames->release();

   CCAnimate* animate=CCAnimate::create(animation);

    CCCallFunc* func=CCCallFunc::create(this,callfunc_selector(Zoombie::startMove));

   CCSpawn* spawn =CCSpawn::create(CCRepeat::create(animate,100), func,NULL);

   CCSequence* seq=CCSequence::create(animate,func,NULL);

   this->runAction(spawn);

    

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
植物大战僵尸游戏需要掌握cocos2d-x引擎的基础知识,包括场景、图层、精灵、动画等,同时还需要了解游戏的规则和逻辑。下面是一个简单植物大战僵尸游戏实现思路: 1. 创建游戏场景和游戏图层 2. 加载游戏背景、植物、僵尸等资源 3. 实现植物的种植和僵尸的出现 4. 实现植物攻击僵尸僵尸攻击植物 5. 实现游戏结束和胜利的判定 具体实现细节可以参考下面的代码示例: 1. 创建游戏场景和游戏图层 ``` auto scene = Scene::create(); auto layer = Layer::create(); scene->addChild(layer); ``` 2. 加载游戏资源 ``` auto bg = Sprite::create("bg.png"); auto sunflower = Sprite::create("sunflower.png"); auto zombie = Sprite::create("zombie.png"); ``` 3. 实现植物的种植和僵尸的出现 ``` auto addSunflower = CallFunc::create([&](){ auto sunflower = Sprite::create("sunflower.png"); sunflower->setPosition(Vec2(100, 100)); layer->addChild(sunflower); }); auto addZombie = CallFunc::create([&](){ auto zombie = Sprite::create("zombie.png"); zombie->setPosition(Vec2(500, 100)); layer->addChild(zombie); }); auto sequence = Sequence::create(addSunflower, DelayTime::create(5.0f), addZombie, nullptr); layer->runAction(RepeatForever::create(sequence)); ``` 4. 实现植物攻击僵尸僵尸攻击植物 ``` auto sunflowerAttack = CallFunc::create([&](){ // 植物攻击 auto bullet = Sprite::create("bullet.png"); bullet->setPosition(sunflower->getPosition()); layer->addChild(bullet); auto move = MoveTo::create(1.0f, zombie->getPosition()); auto remove = RemoveSelf::create(); bullet->runAction(Sequence::create(move, remove, nullptr)); }); auto zombieAttack = CallFunc::create([&](){ // 僵尸攻击 auto attack = Sprite::create("attack.png"); attack->setPosition(zombie->getPosition()); layer->addChild(attack); auto remove = RemoveSelf::create(); attack->runAction(Sequence::create(DelayTime::create(1.0f), remove, nullptr)); }); auto sunflowerSequence = Sequence::create(sunflowerAttack, DelayTime::create(1.0f), nullptr); sunflower->runAction(RepeatForever::create(sunflowerSequence)); auto zombieSequence = Sequence::create(zombieAttack, DelayTime::create(1.0f), nullptr); zombie->runAction(RepeatForever::create(zombieSequence)); ``` 5. 实现游戏结束和胜利的判定 ``` bool isGameOver = false; bool isGameWin = false; auto checkGameOver = CallFunc::create([&](){ if (isGameOver) { // 游戏结束 // ... } }); auto checkGameWin = CallFunc::create([&](){ if (isGameWin) { // 游戏胜利 // ... } }); auto gameOverSequence = Sequence::create(DelayTime::create(10.0f), checkGameOver, nullptr); auto gameWinSequence = Sequence::create(DelayTime::create(10.0f), checkGameWin, nullptr); layer->runAction(gameOverSequence); layer->runAction(gameWinSequence); ``` 以上就是一个简单植物大战僵尸游戏实现思路,具体实现还需要根据自己的需求进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GFanStudio-LeeSir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值