近期才開始学cocos2dx,买了几本书还有看大神(主要是 笨木头)的博客。然后就自己尝试用cocos2d-x实现了一下...

(新手,勿喷...)

先看执行效果 http://pan.baidu.com/s/1hqBsCYS

转载请注明出处:http://blog.csdn.net/joueu

你看那仅仅笨鸟好逼真啊(事实上是叫FlappyRect啦)

TollgateScene.cpp

//
// TollgateScene.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "TollgateScene.h"
#include "Pipe.h"
#include "shareHeader.h"
#include "MenuScene.h" bool TollgateScene::init()
{
if (!Layer::init()) {
return false;
} createOprUI(); Size visibleSize = Director::getInstance() -> getVisibleSize();
Point visiblePoint = Director::getInstance() -> getVisibleOrigin();
m_bird = Bird::create();
m_bird -> setPosition(visibleSize.width * 0.2f, visibleSize.height * 0.5f);
this -> addChild(m_bird); m_pipesLayer = PipesLayer ::create();
m_pipesLayer -> setPosition(0, 170);
this -> addChild(m_pipesLayer); schedule(schedule_selector(TollgateScene::logic)); // auto contactListener = EventListenerPhysicsContact::create();
// contactListener -> onContactBegin = CC_CALLBACK_1(TollgateScene::onContactBegin, this);
// _eventDispatcher -> addEventListenerWithSceneGraphPriority(contactListener, this); auto contactListener = EventListenerPhysicsContact::create();
contactListener -> onContactBegin = CC_CALLBACK_1(TollgateScene::onContactBegin, this);
_eventDispatcher -> addEventListenerWithSceneGraphPriority(contactListener, this); return true;
} void TollgateScene::createOprUI()
{
Node *UI = timeline::NodeReader::getInstance() -> loadNodeWithFile("Flappy_1.ExportJson");
m_flyBtn = (Button *)Helper::seekWidgetByName((Widget *)UI, "flyBtn");
m_flyBtn -> addTouchEventListener(this, toucheventselector(TollgateScene::flyBtnPressed)); Size btnSize = Size(640,210);
PhysicsBody *body = PhysicsBody::createBox(btnSize);
body -> setDynamic(false);
body -> setCollisionBitmask(1);
body -> setCategoryBitmask(1);
body -> setContactTestBitmask(1);
m_flyBtn -> setPhysicsBody(body); // m_bg1 = (Sprite *)Helper::seekWidgetByName((Widget *)UI, "borderImg"); m_bg1 =(Sprite *) UI -> getChildren().at(2);
m_bg2 = Sprite::create("border.png");
m_bg2 -> setPosition(960,m_bg1 -> getPositionY()); this -> addChild(m_bg2);
this -> addChild(UI);
} void TollgateScene::flyBtnPressed(Ref *sender,TouchEventType type)
{
switch (type) {
case TouchEventType::TOUCH_EVENT_ENDED:
m_bird -> jump();
break; default:
break;
}
} Scene *TollgateScene::scene()
{ Scene *scene = Scene::createWithPhysics(); Vect gravity(0,-500.0f); scene -> getPhysicsWorld() -> setGravity(gravity); TollgateScene *layer = TollgateScene::create(); scene -> addChild(layer); return scene;
} void TollgateScene::logic(float dt)
{
int PosX1 = m_bg1 -> getPositionX();
int PosX2 = m_bg2 -> getPositionX(); int iSpeed = 4; PosX1 -= iSpeed;
PosX2 -= iSpeed; int visibleWidth = Director::getInstance() -> getVisibleSize().width; if (PosX1 < - 0.5f * visibleWidth) {
PosX2 = 0.5f * visibleWidth;
PosX1 = 1.5f * visibleWidth;
}
if (PosX2 < -0.5f * visibleWidth) {
PosX1 = 0.5f * visibleWidth;
PosX2 = 1.5f * visibleWidth;
} m_bg1 -> setPositionX(PosX1);
m_bg2 -> setPositionX(PosX2);
} TollgateScene::~TollgateScene()
{ } bool TollgateScene::onContactBegin(PhysicsContact &contact)
{
auto nodeA =(Sprite *) contact.getShapeA() -> getBody() -> getNode();
auto nodeB = (Sprite *)contact.getShapeB() -> getBody() -> getNode(); if (nodeA == NULL && nodeB == NULL) {
return false;
} if ((nodeA -> getTag() == shareHeader_BirdTag && nodeB -> getTag() == shareHeader_PipeTag) ||
(nodeA -> getTag() == shareHeader_PipeTag && nodeB -> getTag() == shareHeader_BirdTag)) {
Director::getInstance() -> replaceScene(TransitionSlideInT::create(1.0f,MenuScene::scene()));
}
return true;
} void TollgateScene::onExit()
{
Layer :: onExit();
_eventDispatcher -> removeEventListenersForTarget(this);
}

TollgateScene.h

//
// TollgateScene.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__TollgateScene__
#define __FlappyRect__TollgateScene__ #include "cocos2d.h"
#include "cocostudio/Cocostudio.h"
#include "editor-support/cocostudio/CCSGUIReader.h"
#include "ui/CocosGUI.h"
#include "Bird.h"
#include "PipesLayer.h" using namespace cocos2d::ui;
using namespace cocostudio;
USING_NS_CC; class TollgateScene : public Layer
{
public:
CREATE_FUNC(TollgateScene); virtual bool init(); static Scene *scene(); ~TollgateScene();
virtual void onExit() override;
private:
void flyBtnPressed(Ref *sender,TouchEventType type);
void createOprUI();
Button *m_flyBtn;
Bird *m_bird;
void logic(float dt); Sprite *m_bg1;
Sprite *m_bg2; PipesLayer *m_pipesLayer; bool onContactBegin (PhysicsContact &contact);
}; #endif /* defined(__FlappyRect__TollgateScene__) */

PipesLayer.cpp

//
// PipesLayer.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "PipesLayer.h" PipesLayer::PipesLayer()
{ } PipesLayer::~PipesLayer()
{ } bool PipesLayer::init()
{
// srand(time(0));
m_TimeCount = 0;
schedule(schedule_selector(PipesLayer::logic));
return true;
} int gameRand(int vMin,int vMax){
return (2.0f*random() / 0xFFFFFFFFu) * ((vMax+1)-vMin) + vMin;
} void PipesLayer::logic(float dt)
{
Size visibleSize = Director::getInstance() -> getVisibleSize();
m_TimeCount += dt;
if (m_TimeCount > 4)
{
// log("%d",rand() % 700);
// int randomPosition = rand() % 700;
int randomPosition = gameRand(80, 600); m_TimeCount -= 4;
Pipe *pipe = Pipe::create(randomPosition);
pipe -> setPosition(Vec2(visibleSize.width * 1.5f, this -> getContentSize().height * 0.5f));
this -> addChild(pipe);
m_vector.pushBack(pipe);
} for (Pipe *p : m_vector){
p -> setPositionX(p -> getPositionX() - shareHeader_speed);
if (p -> getPositionX() < -visibleSize.width * 0.5) {
m_vector.eraseObject(p);
// p -> getPhysicsBody() -> removeFromWorld();
p -> removeFromParent();
}
}
}

PipesLayer.h

//
// PipesLayer.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__PipesLayer__
#define __FlappyRect__PipesLayer__ #include "cocos2d.h"
#include "Pipe.h"
#include "shareHeader.h"
USING_NS_CC; class PipesLayer:public Node
{
public:
PipesLayer();
~PipesLayer();
virtual bool init();
CREATE_FUNC(PipesLayer);
void logic(float dt); private:
float m_TimeCount;
Vector<Pipe *>m_vector;
}; #endif /* defined(__FlappyRect__PipesLayer__) */

Brid.cpp

//
// Bird.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "Bird.h"
#include "shareHeader.h"
Bird::Bird()
{ } Bird::~Bird()
{ } bool Bird::init()
{ if (!Layer::init()) {
return false;
}
this -> setTag(shareHeader_BirdTag);
Sprite *sprite = Sprite::create("rect.jpg"); Size spriteSize = sprite -> getContentSize(); this -> setContentSize(spriteSize); PhysicsBody *body = PhysicsBody::createBox(spriteSize); // body -> setDynamic(false);
// body -> setRotationEnable(false);
body -> setCategoryBitmask(1);
body -> setCollisionBitmask(1);
body -> setContactTestBitmask(1); this -> setPhysicsBody(body);
this -> addChild(sprite); return true;
} void Bird::jump()
{
// this -> getPhysicsBody() -> applyImpulse(Vect(0,100000));
this -> getPhysicsBody() -> setVelocity(Vect(0,300));
}

Brid.h

//
// Bird.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__Bird__
#define __FlappyRect__Bird__ #include "cocos2d.h" USING_NS_CC; class Bird : public Layer
{
public:
Bird();
~Bird();
CREATE_FUNC(Bird);
virtual bool init();
void jump();
}; #endif /* defined(__FlappyRect__Bird__) */

Pipe.cpp

<pre name="code" class="cpp">//
// Pipe.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "Pipe.h"
#include "shareHeader.h" bool Pipe::init(int randomPosition)
{
m_lowerPipe = Sprite::create("pipe.png");
float pipeHeight = m_lowerPipe -> getContentSize().height;
float pipeWidth = m_lowerPipe -> getContentSize().width;
m_lowerPipe -> setScaleY( (randomPosition) / pipeHeight);
m_lowerPipe -> setPosition(Vec2(pipeWidth * 0.5f, pipeHeight * 0.5f *m_lowerPipe -> getScaleY()));
this -> setContentSize(Size(pipeWidth,shareHeader_gameSceneHeight)); m_lowerPipe -> setTag(shareHeader_PipeTag); m_upperPipe = Sprite::create("pipe.png");
m_upperPipe -> setScaleY((shareHeader_gameSceneHeight - (m_lowerPipe -> getScaleY() * pipeHeight) - shareHeader_emptyHeight)/ pipeHeight);
m_upperPipe -> setPosition(Vec2(pipeWidth * 0.5f,shareHeader_gameSceneHeight - pipeHeight * m_upperPipe -> getScaleY() * 0.5));
m_upperPipe -> setFlippedY(true);
m_upperPipe -> setTag(shareHeader_PipeTag); PhysicsBody *lowerbody = PhysicsBody::createBox(Size(pipeWidth,pipeHeight *m_lowerPipe -> getScaleY()));
lowerbody -> setDynamic(false);
lowerbody -> setCategoryBitmask(1);
lowerbody -> setCollisionBitmask(1);
lowerbody -> setContactTestBitmask(1);
m_lowerPipe -> setPhysicsBody(lowerbody); PhysicsBody *upperbody = PhysicsBody::createBox(Size(pipeWidth,pipeHeight *m_upperPipe -> getScaleY()));
upperbody -> setDynamic(false);
upperbody -> setCategoryBitmask(1);
upperbody -> setCollisionBitmask(1);
upperbody -> setContactTestBitmask(1);
m_upperPipe -> setPhysicsBody(upperbody); this -> addChild(m_upperPipe);
this -> addChild(m_lowerPipe);
return true;
} Pipe *Pipe::create(int randomPosition)
{
auto pipe = new Pipe();
if (pipe && pipe -> init(randomPosition)) {
pipe -> autorelease();
}else
{
CC_SAFE_DELETE(pipe);
}
return pipe;
} Pipe::~Pipe()
{
}

Pipe.h

//
// Pipe.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__Pipe__
#define __FlappyRect__Pipe__ #include "cocos2d.h" USING_NS_CC; class Pipe: public Node
{
public:
static Pipe*create(int randomPosition);
bool init(int randomPosition);
~Pipe();
private:
Sprite *m_upperPipe;
Sprite *m_lowerPipe;
}; #endif /* defined(__FlappyRect__Pipe__) */

MenuScene.cpp

//
// MenuScene.cpp
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #include "MenuScene.h"
#include "TollgateScene.h" bool MenuScene::init()
{
auto visibleSize = Director :: getInstance() -> getVisibleSize();
auto labelStart = Label::create("Start Game", "Monaco", 40);
MenuItemLabel *label = MenuItemLabel::create(labelStart,CC_CALLBACK_1(MenuScene::labelPressed, this));
auto menu = Menu::create(label, NULL);
this -> addChild(menu);
return true;
} Scene *MenuScene::scene()
{
Scene *scene = Scene::create(); MenuScene *layer = MenuScene::create(); scene -> addChild(layer); return scene;
} void MenuScene::labelPressed(Ref *sender)
{
Director::getInstance() -> replaceScene(TransitionFade::create(1.0f, TollgateScene::scene()));
}

MenuScene.h

//
// MenuScene.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef __FlappyRect__MenuScene__
#define __FlappyRect__MenuScene__ #include "cocos2d.h" USING_NS_CC; class MenuScene :public Layer
{
public:
CREATE_FUNC(MenuScene);
virtual bool init();
static Scene *scene();
private:
void labelPressed(Ref *sender);
}; #endif /* defined(__FlappyRect__MenuScene__) */

shareHeader.h

//
// shareHeader.h
// FlappyRect
//
// Created by Joueu on 14-10-7.
//
// #ifndef FlappyRect_shareHeader_h
#define FlappyRect_shareHeader_h #define shareHeader_bottomHeight 170
#define shareHeader_gameSceneHeight 960
#define shareHeader_emptyHeight 250
#define shareHeader_speed 3
#define shareHeader_BirdTag 1001
#define shareHeader_PipeTag 1002 #endif

AppDelegate.cpp

#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "TollgateScene.h"
#include "MenuScene.h"
USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate()
{
} bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
} glview -> setDesignResolutionSize(640, 1136, ResolutionPolicy::EXACT_FIT);
// turn on display FPS
director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60); // create a scene. it's an autorelease object
// auto scene = HelloWorld::createScene();
auto scene = MenuScene::scene(); // run
director->runWithScene(scene); return true;
} // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
} // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation(); // if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

因为写得比較仓促,就没有写凝视了,不要介意哈...

图片资源在这里:图片资源

用cocos2d-x 3.2 实现的FlappyBird的更多相关文章

  1. Cocos2d-x 2.3.3版本 FlappyBird

    Cocos2d-x 2.3.3版本 FlappyBird   本篇博客基于Cocos2d-x 2.3.3, 介绍怎样开发一款之前非常火的一款游戏FlappyBird.本篇博客内容大纲例如以下:   1 ...

  2. 小尝试一下 cocos2d

    好奇 cocos2d 到底是怎样一个框架,正好有个项目需要一个游戏框架,所以稍微了解了一下.小结一下了解到的情况. 基本概念 首先呢,因为 cocos2d 是基于 pyglet 做的,你完全可以直接用 ...

  3. 采用cocos2d-x lua 制作数字滚动效果样例

    require "Cocos2d"require "Cocos2dConstants"local testscene = class("testsce ...

  4. Cocos2d 利用继承Draw方法制作可显示三维数据(宠物三维等)的三角形显示面板

    很久没有写博客了,这段时间比较忙,又是搬家又是做自己的项目,还有太多琐碎的事情缠身,好不容易抽出时间把最近自己做的一些简单例子记录一下. 在我的项目中,我需要一个显示面板来显示游戏中的一个三维数据,例 ...

  5. cocos2dx 实现flappybird

    前两天在博客园看到网友实现的一个网页版的flappy bird,挂在360游戏平台,玩了一会儿得分超低,就很想自己做一个.刚好这两天炫舞的活都清了,就弄一下玩玩. 效果图 布局类GameScene.h ...

  6. [Canvas前端游戏开发]——FlappyBird详解

    一直想自己做点小东西,直到最近看了本<HTML5游戏开发>,才了解游戏开发中的一点点入门知识. 本篇就针对学习的几个样例,自己动手实践,做了个FlappyBird,源码共享在度盘 :也可以 ...

  7. iPhone开发与cocos2d 经验谈

    转CSDN jilongliang : 首先,对于一个完全没有mac开发经验,甚至从没摸过苹果系统的开发人员来说,首先就是要熟悉apple的那一套开发框架(含开发环境IDE.开发框架uikit,还有开 ...

  8. cocos2d学习记录

    视频 - http://www.manew.com/forum-105-3.html一个论坛帖 - http://www.zhihu.com/question/21114802官网 - http:// ...

  9. Android下Cocos2d创建HelloWorld工程

    最近在搭建Cocos2d的环境,结果各种问题,两人弄了一天才能搞好一个环境-! -_-!! 避免大家也可能会遇到我这种情况,所以写一个随笔,让大家也了解下如何搭建吧- 1.环境安装准备 下载 tadp ...

  10. 学生信息管理系统(cocos2d引擎)——数据结构课程设计

    老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!! 给我的感想就是全都是指针! 添加图片精灵: CCSprite*  spBG = CCSprite::create("&qu ...

随机推荐

  1. 【Tips】Endnote导入IEEE Xplore文献方法《转载》

    1. 在IEEE XPlore中点击“Download Citation”: 2. 选中“Citation & Abstract”和“EndNote,Procite,RefMan”两个选项: ...

  2. 浅析嵌入式Linux系统的构成和启动过程

    在我们的周围,大量的嵌入式设备都是基于Linux系统来构建的,嵌入式Linux与主机Linux相比有着自己的一些特点,本文就嵌入式Linux系统的构成和启动过程做一些总结. 一.嵌入式Linux系统构 ...

  3. 树莓派的.bashrc和.bash_aliases文件

    在你的home文件夹中,你能够找到一个包括用户配置的隐藏文件.bashrc. 你能够依据自己的须要改动这个文件. 文件里为你提供了一些实用的调整设置.默认情况下当中一些设置是被凝视掉的. 比如,一些l ...

  4. 52. 模版和设计元素——Lotus Notes的代码重用

    不论是理论上还是实用上,代码重用都是编程的一个重要议题.可以从两个角度来讨论代码重用. 一是逻辑上代码以怎样的方式被重用.既可以通过面向对象的思想普及以来耳熟能详的继承的方式.比如先建了一个车的基类, ...

  5. Perl BEGIN块和END块

    [root@wx03 5]# cat a5.pl END {print cccccccccccc."\n"}; print "aaaaaaaaaaaaa\n"; ...

  6. ORACLE 安装Oracle12遇到的问题

    0.全然卸载Oracle10(Windows) 在Windows下多次安装Oracle会造成混乱.重装Oracle的话一定先要干净卸载曾经的Oracle. 一.有必要时先备份 二.卸载步骤 1.用DB ...

  7. hdu3986Harry Potter and the Final Battle

    给你一个无向图,然后找出当中的最短路, 除去最短路中的随意一条边,看最糟糕的情况下, 新的图中,第一个点到末点的最短路长度是多少. 我的做法是: 首先找出最短路,然后记录路径, 再一条一条边的删, 删 ...

  8. 百度贴吧客户端(Android)网络通信行为分析

    百度贴吧安卓客户端网络通信行为分析 本文由CSDN-蚍蜉撼青松[主页:http://blog.csdn.net/howeverpf]原创,转载请注明出处! 一.实验环境与结果概述 1.1 实验环境   ...

  9. 与众不同 windows phone (4) - Launcher(启动器)

    原文:与众不同 windows phone (4) - Launcher(启动器) [索引页][源码下载] 与众不同 windows phone (4) - Launcher(启动器) 作者:weba ...

  10. windows线程同步的总结

    一 线程 1)如果你正在编写C/C++代码,决不应该调用CreateThread.相反,应该使用VisualC++运行期库函数_beginthreadex,退出也应该使用_endthreadex.如果 ...