#include "cocos2d.h"
#include "FlyBirdGame.h"
#include "resource.h";
USING_NS_CC;
using namespace std;
Scene* FlyBirdGame::createScene()
{
auto scene = Scene::create();
auto layer = FlyBirdGame::create();
scene->addChild(layer);
return scene;
}
bool FlyBirdGame::init()
{
if (!Layer::init())
{
return false;
}
initUI();
return true;
}
void FlyBirdGame::initUI()
{
// win size
auto winSize = Director::getInstance()->getVisibleSize();
// game bg
auto bg = Sprite::create(bird_bg);
bg->setPosition(winSize.width / 2, winSize.height / 2);
bg->setScale(winSize.width / bg->getContentSize().width, winSize.height / bg->getContentSize().height);
this->addChild(bg);
// logo
auto logo = Sprite::create(bird_logo);
logo->setPosition(ccp(winSize.width / 2, winSize.height / 2 + logo->getContentSize().height * 2));
logo->setTag(TAG_LOGO);
this->addChild(logo, 1);
// over logo
auto overLogo = Sprite::create(bird_gameover);
overLogo->setPosition(ccp(winSize.width / 2, winSize.height / 2 + overLogo->getContentSize().height));
overLogo->setTag(TAG_OVER);
overLogo->setVisible(false);
this->addChild(overLogo, 1);
// start btn
auto startBtn = MenuItemImage::create(bird_start_btn, bird_start_btn_pressed, CC_CALLBACK_1(FlyBirdGame::gameStart, this));
auto menu = Menu::create(startBtn, NULL);
menu->setTag(TAG_START_BTN);
this->addChild(menu);
// hero
auto hero = Sprite::create(bird_hero);
hero->setPosition(winSize.width / 3, winSize.height*0.8);
hero->setVisible(false);
hero->setTag(TAG_HERO);
this->addChild(hero, 1);
Animation* an = Animation::create();
an->addSpriteFrameWithFileName(bird_hero);
an->addSpriteFrameWithFileName(bird_hero2);
an->addSpriteFrameWithFileName(bird_hero3);
an->setDelayPerUnit(0.5f / 3.0f);
an->setLoops(-1);
Animate* anim = Animate::create(an);
hero->runAction(anim);
// score
auto score = LabelBMFont::create("0", "fonts/futura-48.fnt");
score->setPosition(Point(winSize.width / 2, winSize.height / 4 * 3));
addChild(score, 1);
score->setVisible(false);
score->setTag(TAG_SCORE);
// Obstacle
obstacle = new Obstacle();
this->addChild(obstacle);
// update
scheduleUpdate();
// touch
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesEnded = CC_CALLBACK_2(FlyBirdGame::onTouchesEnded, this);
listener->onTouchesBegan = CC_CALLBACK_2(FlyBirdGame::onTouchesBegan, this);
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// init status
GAME_STATUS = GAME_STATUS_START;
}
void FlyBirdGame::gameStart(Object* pSender)
{
this->getChildByTag(TAG_START_BTN)->setVisible(false);
this->getChildByTag(TAG_LOGO)->setVisible(false);
this->getChildByTag(TAG_SCORE)->setVisible(true);
this->getChildByTag(TAG_HERO)->setVisible(true);
obstacle->GAME_STATUS = GAME_STATUS_PLAYING;
GAME_STATUS = GAME_STATUS_PLAYING;
isFlying = false;
score = 0;
velocity = -3;
}
void FlyBirdGame::update(float time)
{
auto winSize = Director::getInstance()->getVisibleSize();
auto hero = this->getChildByTag(TAG_HERO);
Rect rHero = ((Sprite*)hero)->getBoundingBox();
CCLog("time=%f", time);
switch (GAME_STATUS)
{
case GAME_STATUS_PLAYING:
obstacle->update();
// update bird positionY
if (hero->getPositionY() > 0 && hero->getPositionY() < winSize.height)
{
velocity -= gravity;
hero->setPositionY(hero->getPositionY() + velocity);
}
//if (isFlying&&hero->getPositionY() < winSize.height)
//{
// hero->setPositionY(hero->getPositionY() + velocity);
//}
//else if (hero->getPositionY()>0)
//{
// hero->setPositionY(hero->getPositionY() - velocity);
//}
//check collision
for (int i = 0; i < obstacle->obstacleList->count(); i++)
{
Sprite* obstacleSprite = (Sprite*)obstacle->obstacleList->getObjectAtIndex(i);
bool pia = rHero.intersectsRect(obstacleSprite->getBoundingBox());
if (pia == true)
{
GAME_STATUS = GAME_STATUS_GAME_OVER;
break;
}
int oPosX = obstacleSprite->getPositionX() + obstacleSprite->getContentSize().width / 2;
int heroX = hero->getPositionX() - hero->getContentSize().width;
if (oPosX == heroX)
{
score++;
auto scoreSprite = (LabelBMFont*)this->getChildByTag(TAG_SCORE);
String* s = String::createWithFormat("%d", score / 2);
scoreSprite->setString(s->getCString());
}
}
break;
case GAME_STATUS_GAME_OVER:
this->getChildByTag(TAG_OVER)->setVisible(true);
break;
case GAME_STATUS_RESTART:
//reset game
obstacle->removeAllChildren();
obstacle->obstacleList->removeAllObjects();
// reset hero
hero->setPosition(winSize.width / 5, winSize.height*0.8);
hero->setVisible(false);
// show btn
this->getChildByTag(TAG_START_BTN)->setVisible(true);
// show logo
this->getChildByTag(TAG_LOGO)->setVisible(true);
// hide over log
this->getChildByTag(TAG_OVER)->setVisible(false);
// hide score
this->getChildByTag(TAG_SCORE)->setVisible(false);
break;
}
}
void FlyBirdGame::onTouchesEnded(const vector<Touch*>& touches, Event* event)
{
Touch* touch = touches[0];
Point location = touch->getLocation();
if (GAME_STATUS == GAME_STATUS_PLAYING)
{
isFlying = false;
}
}
void FlyBirdGame::onTouchesBegan(const vector<Touch*>& touches, Event* event)
{
Touch* touch = touches[0];
Point location = touch->getLocation();
if (GAME_STATUS == GAME_STATUS_PLAYING)
{
CCLog("GAME_STATUS_PLAYING");
isFlying = true;
velocity = 5;
}
else if (GAME_STATUS == GAME_STATUS_GAME_OVER)
{
GAME_STATUS = GAME_STATUS_RESTART;
CCLog("GAME_STATUS_GAME_OVER");
}
}
Rect FlyBirdGame::spriteRect(Sprite* s)
{
Point pos = s->getPosition();
Size cs = s->getContentSize();
return CCRectMake(pos.x - cs.width / 2, pos.y - cs.height / 2, cs.width, cs.height / 2);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Flappy游戏cocos2dx3.0源码.rar (37个子文件)
懒骨头仿Flappy游戏coocs2dx3.0源码
Classes
resource.h 1KB
AppDelegate.cpp 2KB
demo
bird
Obstacle.h 271B
FlyBirdGame.cpp 6KB
FlyBirdGame.h 588B
Obstacle.cpp 2KB
AppDelegate.h 947B
Resources
button.png 3KB
ui
DemoHead_UI0.png 242KB
DemoHead_UI1.png 79KB
DemoHead_UI1.plist 1KB
DemoHead_UI0.plist 3KB
DemoHead_UI.ExportJson 19KB
bg.png 455KB
HelloWorld.png 136KB
anim
lazyanim3.ExportJson 32KB
lazyanim30.png 43KB
lazyanim30.plist 5KB
demo
loadui
testui_1.ExportJson 6KB
testui0.plist 1KB
testui0.png 2KB
fonts
futura-48.fnt 12KB
Marker Felt.ttf 25KB
futura-48.png 174KB
button_p.png 3KB
CloseNormal.png 6KB
CloseSelected.png 5KB
bird
brid_start_btn_pressed.png 1KB
bird_hero2.png 1KB
bird_hero3.png 1KB
bird_gameover.png 2KB
bird_start_btn.png 1KB
bird_bg.png 6KB
obstacle_down.png 3KB
bird_hero.png 1KB
bird_logo.png 1KB
obstacle_up.png 3KB
共 37 条
- 1
普通网友
- 粉丝: 963
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- YOLO图片标注xml转txt代码
- 安卓壳可以用于大屏开机打开网址
- paddlepaddle-gpu-2.5.2-cp38-cp38-win-amd64.whl
- Babel Street Analytics Java 客户端库.zip
- 图像处理中的White Patch算法来实现白平衡,MATLAB实现
- 在android studio 中使用jni来进行编程
- 开机自动启动VMWARE workstation16虚拟机
- Python 爬虫:把廖雪峰的教程转换成 PDF 电子书
- 2024 年 Java 开发人员路线图.zip
- matplotlib-3.7.5-cp38-cp38-win-amd64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页