#ifndef __Plane__Backrounde__
#define __Plane__Backrounde__
#include "cocos2d.h"
using namespace cocos2d;
#include
class Backround:public CCLayerColor
{
private:
float height;
CCSize size;
public:
virtual bool init();
void update(float dt);
static Backround*create();
};
#endif
#ifndef __Plane__Backrounde__
#define __Plane__Backrounde__
#include "cocos2d.h"
using namespace cocos2d;
#include
class Backround:public CCLayerColor
{
private:
float height;
CCSize size;
public:
virtual bool init();
void update(float dt);
static Backround*create();
};
#endif
#ifndef __Plane__sprite__
#define __Plane__sprite__
#include "cocos2d.h"
using namespace cocos2d;
#include
class Sprite:public CCSprite
{
public:
bool initwithImage(const char *pszSpriteFrameName);
static Sprite*create(const char *pszSpriteFrameName);
};
#endif
#include "sprite.h"
bool Sprite::initwithImage(const char *pszSpriteFrameName)
{
if (!CCSprite::initWithSpriteFrameName(pszSpriteFrameName))
{
return false;
}
return true;
}
Sprite*Sprite::create(const char *pszSpriteFrameName)
{
Sprite * pRet = new Sprite();
if (pRet&&pRet->initWithSpriteFrameName(pszSpriteFrameName))
{
pRet->autorelease();
return pRet;
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
return NULL;
}
}
#ifndef __Plane__Enemy__
#define __Plane__Enemy__
#include "cocos2d.h"
using namespace cocos2d;
#include
class Enemy:public CCSprite
{
private:
CCTexture2D *text2d;
public:
bool init(int tag);
static Enemy* create(int tag);
void update(float dt);
};
#endif
#include "Enemy.h"
#include "player.h"
#include "Gamescene.h"
#include "SimpleAudioEngine.h"
Enemy*Enemy::create(int tag)
{
Enemy*pRet = new Enemy();
if (pRet&&pRet->init(tag))
{
pRet->autorelease();
return pRet;
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
return NULL;
}
}
bool Enemy::init(int tag)
{
if(tag==0)
{
text2d = CCTextureCache::sharedTextureCache()->addImage("enemy_duck.png");
}
else if(tag==1)
{
text2d = CCTextureCache::sharedTextureCache()->addImage("enemy_pig.png");
}
else
{
text2d = CCTextureCache::sharedTextureCache()->addImage("enemy_bug.png");
}
if (!CCSprite::initWithTexture(text2d))
{
return false;
}
CCArray * frames = CCArray::create();
float spritewith= text2d->getContentSize().width/10;
float hight = text2d->getContentSize().height;
for(int i=0;i<<span style="color: #0433ff">10;i++)
{
CCRect rect = CCRectMake(spritewith*i, 0, spritewith, hight);
CCSpriteFrame * sprite = CCSpriteFrame::createWithTexture(text2d, rect);
frames->addObject(sprite);
}
CCAnimation * anim = CCAnimation::createWithSpriteFrames(frames,0.08f);
CCAnimate * animate = CCAnimate::create(anim);
CCRepeatForever *repeate = CCRepeatForever::create(animate);
this->runAction(repeate);
this->schedule(schedule_selector(Enemy::update), 1.0f);
return true;
}
void Enemy::update(float dt)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
CCMoveTo *action = CCMoveTo::create(1.0f, location);
this->runAction(action);
}
#ifndef __Plane__Bullet__
#define __Plane__Bullet__
#include "cocos2d.h"
USING_NS_CC;
class Enemy;
#include
class Bullet:public CCSprite
{
private:
CCPoint velocity;
public:
virtual bool init();
static Bullet*create();
void update();
void text(CCArray *enemyarray);
};
#endif
#include "Bullet.h"
#include "Enemy.h"
#include "Gamescene.h"
#include "SimpleAudioEngine.h"
#include "player.h"
bool Bullet::init()
{
if (!CCSprite::initWithSpriteFrameName("p_bullet.png"))
{
return false;
}
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("effect_boom.mp3");
this->schedule(schedule_selector(Bullet::update), 1.0/50);
return true;
}
void Bullet::update()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCPoint pos = this->getPosition();
pos.y+=2;
if (pos.y>size.height+this->getContentSize().height)
{
GameScene::shared()->removeChild(this);
this->unscheduleAllSelectors();
}
this->setPosition(pos);
}
Bullet*Bullet::create()
{
Bullet *pRet = new Bullet();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
return NULL;
}
}
void Bullet::text(CCArray *enemyarray)
{
static int j = 0;
Player * player = GameScene::shared()->getplayer();
CCRect bulltrect = this->boundingBox();
CCArray * todelete = CCArray::create();
todelete->retain();
for(int i = 0;i<enemyarray->count();i++)
{
Enemy * enemy = (Enemy *)enemyarray->objectAtIndex(i);
CCRect enemytext = enemy->boundingBox();
bool collision = bulltrect.intersectsRect(enemytext);
if (collision)
{
enemyarray->removeObjectAtIndex(i);
GameScene::shared()->removeChild(enemy, true);
GameScene::shared()->removeChild(this,true);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("effect_boom.mp3");
CCParticleSystemQuad * ex = CCParticleSystemQuad::create("particle_boom.plist");
ex->setPosition(this->getPosition());
GameScene::shared()->addChild(ex);
j++;
CCString * s= CCString::createWithFormat("%d",j);
player->getscore()->setString(s->getCString());
}
}
if (j>50)
{
GameScene::shared()->gameWin();
}
this->schedule(schedule_selector(Bullet::update),1.0/50);
}
#ifndef __Plane__player__
#define __Plane__player__
#include "cocos2d.h"
USING_NS_CC;
class GameScene;
#include <iostream>
class Player:public CCSprite
{
public:
bool initwithImage(const char *pszSpriteFrameName);
static Player*create(const char *pszSpriteFrameName);
void update(float dt);
CCLabelAtlas * getscore();
void press();
void updatelife(CCArray*enemyarray);
private:
CCLabelAtlas * score;
int life;
CCLabelAtlas * lifevalue;
};
#endif
#include "player.h"
#include "Gamescene.h"
#include "Bullet.h"
#include "Enemy.h"
#include "SimpleAudioEngine.h"
Player* Player::create(const char *pszSpriteFrameName)
{
Player * pRet = new Player();
if (pRet&&pRet->initwithImage(pszSpriteFrameName))
{
pRet->autorelease();
return pRet;
}
else
{
CC_SAFE_RELEASE_NULL(pRet);
return NULL;
}
}
bool Player::initwithImage(const char *pszSpriteFrameName)
{
if (!CCSprite::initWithSpriteFrameName(pszSpriteFrameName))
{
return false;
}
this->schedule(schedule_selector(Player::update),0.75f);
score = CCLabelAtlas::create("0", "fps_images.png", 12, 32, '.');
score->setPosition(ccp(270, 440));
score->setScale(1.5);
GameScene::shared()->addChild(score);
life = 3;
lifevalue = CCLabelAtlas::create("111", "icon_hp.png", 28, 26, '1');
GameScene::shared()->addChild(lifevalue);
lifevalue->setPosition(ccp(260, 15));
lifevalue->setScale(0.5);
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("explosion_small.caf");
return true;
}
void Player::update(float dt)
{
GameScene::shared()->addbullet();
}
void Player::press()
{
GameScene::shared()->addbullet();
}
void Player::updatelife(CCArray*enemyarray)
{
CCRect playerrect = this->boundingBox();
for(int i = 0;i<enemyarray->count();i++)
{
Enemy * enemy = (Enemy *)enemyarray->objectAtIndex(i);
CCRect enemytext = enemy->boundingBox();
bool collision = enemytext.intersectsRect(playerrect);
if (collision)
{
enemyarray->removeObjectAtIndex(i);
GameScene::shared()->removeChild(enemy, true);
this->life--;
CCParticleSystemQuad * ex = CCParticleSystemQuad::create("ExplodingRing.plist");
ex->setPosition(this->getPosition());
GameScene::shared()->addChild(ex);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explosion_small.caf");
}
}
switch (life)
{
case 0:
this->lifevalue->setString("");
break;
case 1:
this->lifevalue->setString("1");
break;
case 2:
this->lifevalue->setString("11");
break;
case 3:
this->lifevalue->setString("111");
break;
default:
break;
}
if (!life)
{
GameScene::shared()->gameover();
}
}
CCLabelAtlas * Player::getscore()
{
return score;
}
#include "score.h"
#include "sprite.h"
#include "Gamebegain.h"
#include "player.h"
#include "Gamescene.h"
bool Score::init()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
if(!CCLayer::init())
{
return false;
}
Sprite * scoreblack = Sprite::create("score_bg.png");
scoreblack->setPosition(ccp(size.width*0.5f,size.height*0.5f));
this->addChild(scoreblack);
CCMenuItemFont * s = CCMenuItemFont::create("Back", this, menu_selector(Score::back));
CCMenu * m = CCMenu::create(s,NULL);
m->setPosition(ccp(size.width*0.9f,20));
this->addChild(m);
score = CCUserDefault::sharedUserDefault()->getIntegerForKey("scorevalue");
static int j = score;
if (score<j)
{
score = j;
}
CCString*l = CCString::createWithFormat("%d",score);
CCLabelTTF * value = CCLabelTTF::create(l->getCString(), "Helvetica-BoldOblique", 60);
value->setColor(ccc3(255, 255, 0));
value->setPosition(ccp(size.width*0.5f,size.height*0.5f));
this->addChild(value);
return true;
}
CCScene*Score::scene()
{
CCScene * scene = CCScene::create();
Score * layer = Score::create();
scene->addChild(layer);
return scene;
}
void Score::back()
{
CCTransitionFlipY * s = CCTransitionFlipY::create(1.0, GameBegain::scene());
CCDirector::sharedDirector()->replaceScene(s);
}