这个植物大战僵尸的小游戏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);
}