Cocos2d-x FlappyBird

 HelloWorldScene.cpp

  1 #include "HelloWorldScene.h"
  2 
  3 USING_NS_CC;
  4 
  5 CCScene* HelloWorld::scene()
  6 {
  7     // 'scene' is an autorelease object
  8     CCScene *scene = CCScene::create();
  9 
 10     // 'layer' is an autorelease object
 11     HelloWorld *layer = HelloWorld::create();
 12 
 13     //添加一个背景颜色图层
 14     CCSize s = CCDirector::sharedDirector()->getWinSize();
 15 
 16     CCLayer *colorlayer = CCLayerColor::create(ccc4(0x00, 0xff, 0xff, 0xff), s.width, s.height);
 17 
 18     colorlayer->ignoreAnchorPointForPosition(false);
 19 
 20     colorlayer->setPosition(s.width / 2, s.height / 2);
 21 
 22     scene->addChild(colorlayer, 1, colorlayer->getTag());
 23     
 24     // add layer as a child to scene
 25     //将主图层添加都背景图层中
 26     colorlayer->addChild(layer);
 27 
 28     // return the scene
 29     return scene;
 30 }
 31 
 32 // on "init" you need to initialize your instance
 33 bool HelloWorld::init()
 34 {
 35     //////////////////////////////
 36     // 1. super init first
 37     if ( !CCLayer::init() )
 38     {
 39         return false;
 40     }
 41 
 42     //取得屏幕的大小
 43     screenSize = CCDirector::sharedDirector()->getVisibleSize();
 44 
 45     //初始化世界
 46     initWorld();
 47 
 48     //添加小鸟、障碍物容器及地面
 49     addBird();
 50     addBarContainer();
 51     addGround();
 52 
 53     //每隔一秒添加一组障碍物
 54     scheduleUpdate();
 55     schedule(schedule_selector(HelloWorld::addBar), 1.0f);
 56 
 57     //设置允许触摸
 58     setTouchEnabled(true);
 59 
 60     return true;
 61 }
 62 
 63 void HelloWorld::initWorld()
 64 {
 65     //重力加速度:纵向9.8,横向 0
 66     world = new b2World(b2Vec2(0, -9.8f));
 67     //添加监视
 68     world->SetContactListener(this);
 69 }
 70 
 71 void HelloWorld::startGame(float dt){
 72     scheduleUpdate();
 73     schedule(schedule_selector(HelloWorld::addBar), 1);
 74 }
 75 
 76 void HelloWorld::stopGame(){
 77     unscheduleUpdate();
 78     unschedule(schedule_selector(HelloWorld::addBar));
 79 }
 80 
 81 
 82 void HelloWorld::addBird()
 83 {
 84     bird = B2Sprite::create("bird.png");
 85     CCSize size = bird->getContentSize();
 86 
 87     //添加小鸟
 88     b2BodyDef bodyDef;
 89     bodyDef.type = b2_dynamicBody;
 90     bodyDef.position = b2Vec2(screenSize.width/2/RATIO, screenSize.height/2/RATIO);
 91     b2Body * birdBody = world->CreateBody(&bodyDef);
 92 
 93     //设置边界
 94     b2PolygonShape birdShape;
 95     birdShape.SetAsBox(size.width/2/RATIO, size.height/2/RATIO);
 96 
 97     //碰撞
 98     b2FixtureDef birdFixtureDef;
 99     birdFixtureDef.shape= &birdShape;
100     birdBody->CreateFixture(&birdFixtureDef);
101 
102     bird->setPTMRatio(RATIO);
103     bird->setB2Body(birdBody);
104     addChild(bird);
105 }
106 
107 void HelloWorld::addBar(float dt)
108 {
109     float offset = -rand()%5;
110 
111     //downbar
112     B2Sprite *downBar = B2Sprite::create("down_bar.png");
113     CCSize downBarSize = downBar->getContentSize();
114 
115     b2BodyDef downBarBodyDef;
116     downBarBodyDef.type = b2_kinematicBody;
117     downBarBodyDef.position = b2Vec2(screenSize.width/RATIO + 2, downBarSize.height/RATIO/2 + offset);
118     downBarBodyDef.linearVelocity = b2Vec2(-5.0f, 0);
119 
120     b2PolygonShape downBarShape;
121     downBarShape.SetAsBox(downBarSize.width/RATIO/2, downBarSize.height/RATIO/2);
122 
123     b2FixtureDef downBarFixtureDef;
124     downBarFixtureDef.shape = &downBarShape;
125 
126     b2Body *downBarBody = world->CreateBody(&downBarBodyDef);
127     downBarBody->CreateFixture(&downBarFixtureDef);
128 
129     downBar->setB2Body(downBarBody);
130     downBar->setPTMRatio(RATIO);
131 
132     //upbar
133     B2Sprite *upBar = B2Sprite::create("up_bar.png");
134     CCSize upBarSize = upBar->getContentSize();
135 
136     b2BodyDef upBarBodyDef;
137     upBarBodyDef.type = b2_kinematicBody;
138     upBarBodyDef.position = b2Vec2(screenSize.width/RATIO + 2, upBarSize.height/RATIO/2 + offset + downBarSize.height/RATIO + 2.5f);
139     upBarBodyDef.linearVelocity = b2Vec2(-5, 0);
140 
141     b2PolygonShape upBarShape;
142     upBarShape.SetAsBox(upBarSize.width/RATIO/2, upBarSize.height/RATIO/2);
143 
144     b2FixtureDef upBarFixtureDef;
145     upBarFixtureDef.shape = &upBarShape;
146 
147     b2Body *upBarBody = world->CreateBody(&upBarBodyDef);
148     upBarBody->CreateFixture(&upBarFixtureDef);
149 
150     upBar->setB2Body(upBarBody);
151     upBar->setPTMRatio(RATIO);
152 
153     barContainer->addChild(upBar);
154     barContainer->addChild(downBar);
155 }
156 
157 void HelloWorld::addBarContainer()
158 {
159     //将所有的障碍物包裹在一个容器中
160     barContainer = CCSprite::create();
161     addChild(barContainer);
162 }
163 
164 void HelloWorld::addGround()
165 {
166     B2Sprite *ground = B2Sprite::create("ground.png");
167     CCSize size = ground->getContentSize();
168 
169     b2BodyDef bDef;
170     bDef.type = b2_staticBody;
171     bDef.position = b2Vec2(size.width/2/RATIO, size.height/2/RATIO);
172     b2Body *groundBody = world->CreateBody(&bDef);
173 
174     b2PolygonShape groundShape;
175     groundShape.SetAsBox(size.width/2/RATIO, size.height/2/RATIO);
176 
177     b2FixtureDef groundFixtureDef;
178     groundFixtureDef.shape = &groundShape;
179     groundBody->CreateFixture(&groundFixtureDef);
180 
181     ground->setPTMRatio(RATIO);
182     ground->setB2Body(groundBody);
183     addChild(ground);
184 }
185 
186 void HelloWorld::update(float dt)
187 {
188     world->Step(dt, 8, 3);
189 
190     CCSprite *sprite;
191 
192     //取得世界中的所有的body
193     b2Body *node = world->GetBodyList();
194 
195     //遍历世界中的body
196     while(node)
197     {
198         b2Body *body = node;
199         node = node->GetNext();
200         sprite = (CCSprite*)body->GetUserData();
201 
202         //超出屏幕左边界,销毁
203         if (body->GetPosition().x < -1)
204         {
205             if (sprite != NULL)
206             {
207                 //在屏幕上清除
208                 sprite->removeFromParent();
209             }
210             //在内存中销毁
211             world->DestroyBody(body);
212         }
213     }
214 }
215 
216 void HelloWorld::BeginContact(b2Contact *contact){
217     //碰撞检测,有一个为鸟游戏就结束
218     if (contact->GetFixtureA()->GetBody()->GetUserData()==bird || contact->GetFixtureB()->GetBody()->GetUserData()==bird)
219     {
220         //游戏结束,弹出dialog
221         stopGame();
222         CCMessageBox("Game Over", "Alert");
223     }
224 }
225 
226 void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
227 {
228     //触碰屏幕时,小鸟向上运动,重力加速度:纵向 5,横向 0
229     bird->getB2Body()->SetLinearVelocity(b2Vec2(0, 5.0f));
230 }
231 
232 
233 void HelloWorld::menuCloseCallback(CCObject* pSender)
234 {
235 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
236     CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
237 #else
238     CCDirector::sharedDirector()->end();
239 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
240     exit(0);
241 #endif
242 #endif
243 }

HelloWorldScene.h

 1 #ifndef __HELLOWORLD_SCENE_H__
 2 #define __HELLOWORLD_SCENE_H__
 3 
 4 #include "cocos2d.h"
 5 #include "Box2DBox2D.h"
 6 #include "B2Sprite.h"
 7 
 8 #define RATIO 72.0f
 9 
10 class HelloWorld : public cocos2d::CCLayer,public b2ContactListener
11 {
12 public:
13     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
14     virtual bool init();  
15 
16     // there's no 'id' in cpp, so we recommend returning the class instance pointer
17     static cocos2d::CCScene* scene();
18     
19     // a selector callback
20     void menuCloseCallback(CCObject* pSender);
21     
22     // implement the "static node()" method manually
23     CREATE_FUNC(HelloWorld);
24 
25     virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
26     virtual void BeginContact(b2Contact* contact);
27 
28     virtual void update(float dt);
29 
30     b2World *world;
31     B2Sprite *bird;
32 
33     CCSize screenSize;
34 
35     CCSprite *barContainer;
36 
37 private:
38     void addBird();
39     void addGround();
40     void initWorld();
41     void addBar(float dt);
42     void addBarContainer();
43     void startGame(float dt);
44     void stopGame();
45 };
46 
47 #endif // __HELLOWORLD_SCENE_H__
原文地址:https://www.cnblogs.com/zhangtingkuo/p/3621741.html