flappy bird

来源:互联网 发布:迅雷9网络诊断工具 编辑:程序博客网 时间:2024/04/29 08:18

背景:

  最近火爆全球的游戏flappy bird让笔者叹为观止,于是花了一天的时间山寨了一个一模一样的游戏,现在把游戏的思路和源码分享出来,代码是基于javascript语言,cocos2d-x游戏引擎,cocos2d-x editor手游开发工具完成的,请读者轻砸;

  ps:运行demo必须配置好cocos2d-x editor,暂不支持其他工具


代码下载:


  csdn下载:http://download.csdn.net/detail/touchsnow/6912707

  百度云盘:http://pan.baidu.com/s/1pJnWDb9

  金山快盘 :http://www.kuaipan.cn/file/id_25348935635745384.htm?source=1

  

效果图:







开发工具:

   cocos2dx editor,它是开发跨平台的手机游戏工具,运行window系统上,javascript脚本语言,基于cocos2d-x跨平台游戏引擎, 集合代码编辑,场景设计,动画制作,字体设计,还有粒子,物理系统,地图等等的,而且调试方便,和实时模拟;

cocos2dx editor介绍和教程:http://blog.csdn.net/touchsnow/article/details/19070665;

cocos2dx-editor官方博客:http://blog.makeapp.co/?cat=8;


思路和源码:


  1 场景设计MainLayer.ccbx,如下图;主要分三层,开始场景、主场景、游戏结束场景,通过显示隐藏控制三个场景的切换。

 

   MainLayer.ccbx代码

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <Document  
  4.         jsControlled="true"  
  5.         jsController="MainLayer"  
  6.         resolution="default"  
  7.         >  
  8.   
  9.     <Resolutions>  
  10.         <Resolution centeredOrigin="false" ext="iphone" height="1280" width="720" name="default" scale="1"/>  
  11.         <Resolution centeredOrigin="false" ext="iphone" height="720" width="1280" name="default1" scale="1"/>  
  12.     </Resolutions>  
  13.   
  14.     <Animations>  
  15.         <Animation autoPlay="true"  
  16.                    id="0"  
  17.                    name="Default Timeline"  
  18.                    length="10"  
  19.                    chainedId="0"  
  20.                    offset="0.0"  
  21.                    position="0.0"  
  22.                    resolution="30"  
  23.                    scale="128">  
  24.             <CallbackChannel>  
  25.             </CallbackChannel>  
  26.             <SoundChannel>  
  27.             </SoundChannel>  
  28.         </Animation>  
  29.   
  30.     </Animations>  
  31.     <Layer  
  32.             positionX="0" positionY="0.0"  
  33.             sizeType="Percent"  
  34.             width="100" height="100"  
  35.             anchorPointX="0.5" anchorPointY="0.5" ignoreAnchorPoint="true"  
  36.             scaleX="1" scaleY="1"  
  37.             >  
  38.   
  39.       <Sprite positionType="LeftBottom" width="720.0" height="1280.0" positionX="0" positionY="0" anchorPointX="0"  
  40.                                          anchorPointY="0" src="Resources/bg.png" name="" var="" target="None" scaleX="1" scaleY="1" visible="true"/>  
  41.       <LayerColor positionType="LeftBottom" width="720" height="1280" positionX="0" positionY="0" anchorPointX="0"  
  42.                   anchorPointY="0" color="#fff2e8ff" visible="false"/>  
  43.       <Menu positionType="LeftBottom" width="40" height="40" positionX="356.0" positionY="237.0" anchorPointX="0.5"  
  44.               anchorPointY="0.5" scaleX="2.4" scaleY="1.725">  
  45.       </Menu>  
  46.       <Sprite positionType="LeftBottom" width="840.0" height="281.0" positionX="0" positionY="0" anchorPointX="0"  
  47.                                          anchorPointY="0" src="Resources/ground.png" var="ground" target="Doc"/>  
  48.       <Node positionType="LeftBottom" width="40" height="40" positionX="800" positionY="250" anchorPointX="0"  
  49.             anchorPointY="0" var="hoseNode" target="Doc">  
  50.         <Sprite positionType="LeftBottom" width="86.0" height="60.0" positionX="-500" positionY="400" anchorPointX="0.5"  
  51.                                          anchorPointY="0.5" src="Resources/flappy_packer.plist/bird3.png" var="test" target="Doc" visible="false"/>  
  52.         <Sprite positionType="LeftBottom" width="86.0" height="60.0" positionX="-550" positionY="500" anchorPointX="0.5"  
  53.                 anchorPointY="0.5" src="Resources/flappy_packer.plist/bird1.png" var="bird" target="Doc" scaleX="1" scaleY="1" rotation="0" visible="true"/>  
  54.       </Node>  
  55.       <Node positionType="LeftBottom" width="40" height="40" positionX="303.0" positionY="500" anchorPointX="0.5"  
  56.             anchorPointY="0.5" var="readyNode" target="Doc" visible="true">  
  57.         <Sprite positionType="LeftBottom" width="508.0" height="158.0" positionX="95.0" positionY="584.0" anchorPointX="0.5"  
  58.                                          anchorPointY="0.5" src="Resources/flappy_packer.plist/getready.png"/>  
  59.         <Sprite positionType="LeftBottom" width="286.0" height="246.0" positionX="73.0" positionY="236.0" anchorPointX="0.5"  
  60.                                          anchorPointY="0.5" src="Resources/flappy_packer.plist/click.png"/>  
  61.       </Node>  
  62.       <Node positionType="LeftBottom" width="40" height="40" positionX="300" positionY="500" anchorPointX="0.5"  
  63.             anchorPointY="0.5" var="overNode" target="Doc" visible="true">  
  64.         <Sprite positionType="LeftBottom" width="590.0" height="298.0" positionX="72.0" positionY="219.0" anchorPointX="0.5"  
  65.                                          anchorPointY="0.5" src="Resources/flappy_packer.plist/base.png">  
  66.           <Sprite positionType="LeftBottom" width="508.0" height="158.0" positionX="286.0" positionY="458.0" anchorPointX="0.5"  
  67.                                          anchorPointY="0.5" src="Resources/flappy_packer.plist/gameover.png"/>  
  68.         </Sprite>  
  69.         <Menu positionType="LeftBottom" width="40" height="40" positionX="0" positionY="0" anchorPointX="0.5"  
  70.               anchorPointY="0.5">  
  71.           <MenuItem positionType="LeftBottom" width="290" height="176" positionX="-65.0" positionY="-92.0" anchorPointX="0.5"  
  72.                     anchorPointY="0.5" normalImage="Resources/flappy_packer.plist/start.png" target="Doc" onClick="onStartClicked"/>  
  73.           <MenuItem positionType="LeftBottom" width="290" height="176" positionX="230.0" positionY="-92.0" anchorPointX="0.5"  
  74.                     anchorPointY="0.5" target="Doc" normalImage="Resources/flappy_packer.plist/grade.png" onClick="onGradeClicked"/>  
  75.         </Menu>  
  76.       </Node>  
  77.     </Layer>  
  78.   
  79.   
  80. </Document>  

 

2 代码编写MainLayer.js

   首先,小鸟在向前飞,其实是底部的路和水管在向左移动,相对的你就感觉小鸟在向右飞了;路循环移动代码:

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MainLayer.prototype.groundRun = function ()  
  2. {  
  3.     var action1 = cc.MoveTo.create(0.5, cc.p(-120, 0));  
  4.     var action2 = cc.MoveTo.create(0, cc.p(0, 0));  
  5.     var action = cc.Sequence.create(action1, action2);  
  6.     this.ground.runAction(cc.RepeatForever.create(action));  
  7. }  

   初始化高低不同的水管,每一关卡都由上下两水管和空隙组成。总长度相同,空隙也一定,随机取下面水管的长度,就形成错落有致的水管关卡;

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MainLayer.prototype.newHose = function (num)  
  2. {  
  3.     var hoseHeight = 830;  
  4.     var acrossHeight = 300;  
  5.     var downHeight = 100 + getRandom(400);  
  6.     var upHeight = 1100 - downHeight - acrossHeight;  
  7.   
  8.     var hoseX = 400 * num;  
  9.   
  10.     var HoseName = FP_MAIN_TEXTURE.HOSE;  
  11.     var ccSpriteDown = cc.Sprite.createWithSpriteFrameName(HoseName[0]);  
  12.     ccSpriteDown.setZOrder(1);  
  13.     ccSpriteDown.setAnchorPoint(cc.p(0, 0));  
  14.     ccSpriteDown.setPosition(cc.p(hoseX, 0));  
  15.     ccSpriteDown.setScaleY(downHeight / hoseHeight);  
  16.   
  17.     var ccSpriteUp = cc.Sprite.createWithSpriteFrameName(HoseName[1]);  
  18.     ccSpriteUp.setZOrder(1);  
  19.     ccSpriteUp.setAnchorPoint(cc.p(0, 0));  
  20.     ccSpriteUp.setPosition(cc.p(hoseX, downHeight + acrossHeight));  
  21.     ccSpriteUp.setScaleY(upHeight / hoseHeight);  
  22.   
  23.     this.hoseNode.addChild(ccSpriteDown);  
  24.     this.hoseNode.addChild(ccSpriteUp);  
  25.     this.hoseSpriteList.push(ccSpriteDown);  
  26.     this.hoseSpriteList.push(ccSpriteUp);  
  27.     return null;  
  28. }  

  一开始进入游戏让底部路不断移动,初始化水管,显示准备游戏场景;

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MainLayer.prototype.onEnter = function ()  
  2. {  
  3.     cc.AnimationCache.getInstance().addAnimations("Resources/flappy_frame.plist");  
  4.     this.groundRun();  
  5.     this.ground.setZOrder(10);  
  6.     this.birdReadyAction();  
  7.     this.bird.setZOrder(20);  
  8.     this.readyNode.setVisible(true);  
  9.     this.overNode.setVisible(false);  
  10.   
  11.     for (var i = 0; i < 30; i++) {  
  12.         this.newHose(i);  
  13.     }  
  14. }  

 点击屏幕,小鸟向上飞60dp,然后更快的速度下落(移动动画),同时闪动翅膀(帧动画);

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MainLayer.prototype.birdRiseAction = function ()  
  2. {  
  3.     var riseHeight = 60;  
  4.     var birdX = this.bird.getPositionX();  
  5.     var birdY = this.bird.getPositionY();  
  6.     var time = birdY / 600;  
  7.   
  8.     var actionFrame = cc.Animate.create(cc.AnimationCache.getInstance().getAnimation("fly"));  
  9.     var flyAction = cc.Repeat.create(actionFrame, 90000);  
  10.     var riseAction1 = cc.MoveTo.create(0.2, cc.p(birdX, birdY + riseHeight));  
  11.     var riseAction2 = cc.RotateTo.create(0, -30);  
  12.     var riseAction = cc.Spawn.create(riseAction1, riseAction2);  
  13.     var fallAction1 = cc.MoveTo.create(time, cc.p(birdX, 50));  
  14.     var fallAction2 = cc.Sequence.create(cc.DelayTime.create(time / 6), cc.RotateTo.create(0, 30));  
  15.     var fallAction = cc.Spawn.create(fallAction1, fallAction2);  
  16.   
  17.     this.bird.stopAllActions();  
  18.     this.bird.runAction(cc.Spawn.create(  
  19.             cc.Sequence.create(riseAction, cc.DelayTime.create(0.1), fallAction),  
  20.             flyAction)  
  21.     );  
  22. }  

 检测碰撞,如果小鸟碰到地面和水管,发生碰撞,这里碰撞直接用cocos2d-x 里面的图片和图片交叉函数 cc.rectIntersectsRect;

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MainLayer.prototype.checkCollision = function ()  
  2. {  
  3.     if (this.bird.getPositionY() < 60) {  
  4.         cc.log("floor");  
  5.         this.birdFallAction();  
  6.         return;  
  7.     }  
  8.     for (var i = 0; i < this.hoseSpriteList.length; i++) {  
  9.         var hose = this.hoseSpriteList[i];  
  10.         if (!this.isInScreen(hose)) {  
  11.             // continue;  
  12.         }  
  13.   
  14.         if (cc.rectIntersectsRect(hose.getBoundingBox(), this.bird.getBoundingBox())) {  
  15.             cc.log("hose positionX==" + hose.getBoundingBox().x);  
  16.             cc.log("this.bird positionX==" + this.bird.getBoundingBox().x);  
  17.             cc.log("i==" + i);  
  18.             cc.log("birdFallAction");  
  19.             this.birdFallAction();  
  20.             return;  
  21.         }  
  22.     }  
  23. }  

碰撞后,小鸟先下落,游戏结束场景显示;

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. MainLayer.prototype.birdFallAction = function ()  
  2. {  
  3.     this.gameMode = OVER;  
  4.     this.bird.stopAllActions();  
  5.     this.ground.stopAllActions();  
  6.     var birdX = this.bird.getPositionX();  
  7.     var birdY = this.bird.getPositionY();  
  8.     var time = birdY / 2000;  
  9.     this.bird.runAction(cc.Sequence.create(  
  10.             cc.DelayTime.create(0.1),  
  11.             cc.Spawn.create(cc.RotateTo.create(time, 90), cc.MoveTo.create(time, cc.p(birdX, 50))))  
  12.     );  
  13.     this.overNode.setVisible(true);  
  14. }  

游戏的难度主要在于多个水管的移动,小鸟触摸动画,检测碰撞,我把Mainlayer.js所有代码贴出来;

[javascript] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. FP_MAIN_TEXTURE = {  
  2.     FRAME_ANIMS: "beanstalk/Resources/bs_main_anims.plist",  
  3.     HOSE: ["holdback1.png""holdback2.png"]  
  4. }  
  5.   
  6. READY = 1;  
  7. START = 2;  
  8. OVER = 3;  
  9.   
  10. var MainLayer = function ()  
  11. {  
  12.     cc.log("MainLayer");  
  13.     this.bird = this.bird || {};  
  14.     this.ground = this.ground || {};  
  15.     this.hoseNode = this.hoseNode || {};  
  16.     this.readyNode = this.readyNode || {};  
  17.     this.overNode = this.overNode || {};  
  18.   
  19.     this.passTime = 0;  
  20.     this.hoseSpriteList = [];  
  21.     this.isStart = false;  
  22.   
  23.     this.gameMode = READY;  
  24. };  
  25.   
  26. MainLayer.prototype.onDidLoadFromCCB = function ()  
  27. {  
  28.     if (sys.platform == 'browser') {  
  29.         this.onEnter();  
  30.     }  
  31.     else {  
  32.         this.rootNode.onEnter = function ()  
  33.         {  
  34.             this.controller.onEnter();  
  35.         };  
  36.     }  
  37.   
  38.     this.rootNode.schedule(function (dt)  
  39.     {  
  40.         this.controller.onUpdate(dt);  
  41.     });  
  42.   
  43.     this.rootNode.onExit = function ()  
  44.     {  
  45.         this.controller.onExit();  
  46.     };  
  47.   
  48.     this.rootNode.onTouchesBegan = function (touches, event)  
  49.     {  
  50.         this.controller.onTouchesBegan(touches, event);  
  51.         return true;  
  52.     };  
  53.   
  54.     this.rootNode.onTouchesMoved = function (touches, event)  
  55.     {  
  56.         this.controller.onTouchesMoved(touches, event);  
  57.         return true;  
  58.     };  
  59.     this.rootNode.onTouchesEnded = function (touches, event)  
  60.     {  
  61.         this.controller.onTouchesEnded(touches, event);  
  62.         return true;  
  63.     };  
  64.     this.rootNode.setTouchEnabled(true);  
  65. };  
  66.   
  67. MainLayer.prototype.onEnter = function ()  
  68. {  
  69.     cc.AnimationCache.getInstance().addAnimations("Resources/flappy_frame.plist");  
  70.     this.groundRun();  
  71.     this.ground.setZOrder(10);  
  72.     this.birdReadyAction();  
  73.     this.bird.setZOrder(20);  
  74.     this.readyNode.setVisible(true);  
  75.     this.overNode.setVisible(false);  
  76.   
  77.     for (var i = 0; i < 30; i++) {  
  78.         this.newHose(i);  
  79.     }  
  80. }  
  81.   
  82. MainLayer.prototype.newHose = function (num)  
  83. {  
  84.     var hoseHeight = 830;  
  85.     var acrossHeight = 300;  
  86.     var downHeight = 100 + getRandom(400);  
  87.     var upHeight = 1100 - downHeight - acrossHeight;  
  88.   
  89.     var hoseX = 400 * num;  
  90.   
  91.     var HoseName = FP_MAIN_TEXTURE.HOSE;  
  92.     var ccSpriteDown = cc.Sprite.createWithSpriteFrameName(HoseName[0]);  
  93.     ccSpriteDown.setZOrder(1);  
  94.     ccSpriteDown.setAnchorPoint(cc.p(0, 0));  
  95.     ccSpriteDown.setPosition(cc.p(hoseX, 0));  
  96.     ccSpriteDown.setScaleY(downHeight / hoseHeight);  
  97.   
  98.     var ccSpriteUp = cc.Sprite.createWithSpriteFrameName(HoseName[1]);  
  99.     ccSpriteUp.setZOrder(1);  
  100.     ccSpriteUp.setAnchorPoint(cc.p(0, 0));  
  101.     ccSpriteUp.setPosition(cc.p(hoseX, downHeight + acrossHeight));  
  102.     ccSpriteUp.setScaleY(upHeight / hoseHeight);  
  103.   
  104.     this.hoseNode.addChild(ccSpriteDown);  
  105.     this.hoseNode.addChild(ccSpriteUp);  
  106.     this.hoseSpriteList.push(ccSpriteDown);  
  107.     this.hoseSpriteList.push(ccSpriteUp);  
  108.     return null;  
  109. }  
  110.   
  111. MainLayer.prototype.groundRun = function ()  
  112. {  
  113.     var action1 = cc.MoveTo.create(0.5, cc.p(-120, 0));  
  114.     var action2 = cc.MoveTo.create(0, cc.p(0, 0));  
  115.     var action = cc.Sequence.create(action1, action2);  
  116.     this.ground.runAction(cc.RepeatForever.create(action));  
  117. }  
  118.   
  119. MainLayer.prototype.birdReadyAction = function ()  
  120. {  
  121.     var birdX = this.bird.getPositionX();  
  122.     var birdY = this.bird.getPositionY();  
  123.     var time = birdY / 2000;  
  124.     var actionFrame = cc.Animate.create(cc.AnimationCache.getInstance().getAnimation("fly"));  
  125.     var flyAction = cc.Repeat.create(actionFrame, 90000);  
  126.     this.bird.runAction(cc.Sequence.create(  
  127.             flyAction)  
  128.     );  
  129. }  
  130.   
  131. MainLayer.prototype.birdFallAction = function ()  
  132. {  
  133.     this.gameMode = OVER;  
  134.     this.bird.stopAllActions();  
  135.     this.ground.stopAllActions();  
  136.     var birdX = this.bird.getPositionX();  
  137.     var birdY = this.bird.getPositionY();  
  138.     var time = birdY / 2000;  
  139.     this.bird.runAction(cc.Sequence.create(  
  140.             cc.DelayTime.create(0.1),  
  141.             cc.Spawn.create(cc.RotateTo.create(time, 90), cc.MoveTo.create(time, cc.p(birdX, 50))))  
  142.     );  
  143.     this.overNode.setVisible(true);  
  144. }  
  145.   
  146. MainLayer.prototype.birdRiseAction = function ()  
  147. {  
  148.     var riseHeight = 60;  
  149.     var birdX = this.bird.getPositionX();  
  150.     var birdY = this.bird.getPositionY();  
  151.     var time = birdY / 600;  
  152.   
  153.     var actionFrame = cc.Animate.create(cc.AnimationCache.getInstance().getAnimation("fly"));  
  154.     var flyAction = cc.Repeat.create(actionFrame, 90000);  
  155.     var riseAction1 = cc.MoveTo.create(0.2, cc.p(birdX, birdY + riseHeight));  
  156.     var riseAction2 = cc.RotateTo.create(0, -30);  
  157.     var riseAction = cc.Spawn.create(riseAction1, riseAction2);  
  158.     var fallAction1 = cc.MoveTo.create(time, cc.p(birdX, 50));  
  159.     var fallAction2 = cc.Sequence.create(cc.DelayTime.create(time / 6), cc.RotateTo.create(0, 30));  
  160.     var fallAction = cc.Spawn.create(fallAction1, fallAction2);  
  161.   
  162.     this.bird.stopAllActions();  
  163.     this.bird.runAction(cc.Spawn.create(  
  164.             cc.Sequence.create(riseAction, cc.DelayTime.create(0.1), fallAction),  
  165.             flyAction)  
  166.     );  
  167. }  
  168.   
  169. MainLayer.prototype.onUpdate = function (dt)  
  170. {  
  171.     if (this.gameMode != START) {  
  172.         return;  
  173.     }  
  174.     this.passTime += dt;  
  175.   
  176.     this.hoseNode.setPositionX(800 - 200 * this.passTime);  
  177.     this.bird.setPositionX(-500 + 200 * this.passTime);  
  178.     this.checkCollision();  
  179. }  
  180.   
  181. MainLayer.prototype.checkCollision = function ()  
  182. {  
  183.     if (this.bird.getPositionY() < 60) {  
  184.         cc.log("floor");  
  185.         this.birdFallAction();  
  186.         return;  
  187.     }  
  188.     for (var i = 0; i < this.hoseSpriteList.length; i++) {  
  189.         var hose = this.hoseSpriteList[i];  
  190.         if (!this.isInScreen(hose)) {  
  191.             // continue;  
  192.         }  
  193.   
  194.         if (cc.rectIntersectsRect(hose.getBoundingBox(), this.bird.getBoundingBox())) {  
  195.             cc.log("hose positionX==" + hose.getBoundingBox().x);  
  196.             cc.log("this.bird positionX==" + this.bird.getBoundingBox().x);  
  197.             cc.log("i==" + i);  
  198.             cc.log("birdFallAction");  
  199.             this.birdFallAction();  
  200.             return;  
  201.         }  
  202.     }  
  203. }  
  204.   
  205. MainLayer.prototype.isInScreen = function (sprite)  
  206. {  
  207.     return (sprite.getPositionX() > 0 && sprite.getPositionX() < 720);  
  208. }  
  209.   
  210. MainLayer.prototype.onExitClicked = function ()  
  211. {  
  212. }  
  213.   
  214. MainLayer.prototype.onStartClicked = function ()  
  215. {  
  216.     cc.Director.getInstance().resume();  
  217.     cc.BuilderReader.runScene("""MainLayer");  
  218. }  
  219.   
  220. MainLayer.prototype.onExit = function ()  
  221. {  
  222.     cc.log("onExit");  
  223. }  
  224.   
  225. MainLayer.prototype.onTouchesBegan = function (touches, event)  
  226. {  
  227.     var loc = touches[0].getLocation();  
  228. }  
  229.   
  230. MainLayer.prototype.onTouchesMoved = function (touches, event)  
  231. {  
  232. }  
  233.   
  234. MainLayer.prototype.onTouchesEnded = function (touches, event)  
  235. {  
  236.     if (this.gameMode == OVER) {  
  237.         return;  
  238.     }  
  239.   
  240.     if (this.gameMode == READY) {  
  241.         this.gameMode = START;  
  242.         this.readyNode.setVisible(false);  
  243.     }  
  244.   
  245.     var loc = touches[0].getLocation();  
  246.     this.birdRiseAction();  
  247.   
  248. }  
  249.   
  250. function isInRect(ccRect, ccTouchBeganPos)  
  251. {  
  252.     if (ccTouchBeganPos.x > ccRect.x && ccTouchBeganPos.x < (ccRect.x + ccRect.width)) {  
  253.         if (ccTouchBeganPos.y > ccRect.y && ccTouchBeganPos.y < (ccRect.y + ccRect.height)) {  
  254.             return true;  
  255.         }  
  256.     }  
  257.     return false;  
  258. }  
  259.   
  260. function getRandom(maxSize)  
  261. {  
  262.     return Math.floor(Math.random() * maxSize) % maxSize;  
  263. }  
0 0