├── .gitattributes ├── .gitignore ├── .idea ├── .name ├── encodings.xml ├── flappy-bird.iml ├── misc.xml ├── modules.xml ├── scopes │ └── scope_settings.xml ├── vcs.xml └── workspace.xml ├── README.md ├── assets ├── background.png ├── bird.png ├── flap.wav ├── fonts │ └── flappyfont │ │ ├── flappyfont.fnt │ │ └── flappyfont.png ├── game-over.png ├── gameover.png ├── get-ready.png ├── ground-hit.wav ├── ground.png ├── instructions.png ├── medals.png ├── ouch.wav ├── particle.png ├── pipe-hit.wav ├── pipes.png ├── poop.png ├── preloader.gif ├── score.wav ├── scoreboard.png ├── start-button.png ├── test.png └── title.png ├── index.html ├── js ├── game.js ├── game.sublime-workspace ├── phaser.js └── phaser.min.js ├── project.sublime-project └── project.sublime-workspace /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | flappy-bird -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/flappy-bird.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 59 | 60 | 61 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | CoffeeScript 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 115 | 116 | 117 | 118 | 121 | 122 | 125 | 126 | 127 | 128 | 131 | 132 | 135 | 136 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 1398439149160 164 | 1398439149160 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 195 | 198 | 199 | 200 | 202 | 203 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | flappy-bird 2 | =========== 3 | 4 | a html5 game developed with phaser 5 | 6 | demo 7 | 8 | 教程: 9 | -------------------------------------------------------------------------------- /assets/background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/background.png -------------------------------------------------------------------------------- /assets/bird.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/bird.png -------------------------------------------------------------------------------- /assets/flap.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/flap.wav -------------------------------------------------------------------------------- /assets/fonts/flappyfont/flappyfont.fnt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /assets/fonts/flappyfont/flappyfont.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/fonts/flappyfont/flappyfont.png -------------------------------------------------------------------------------- /assets/game-over.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/game-over.png -------------------------------------------------------------------------------- /assets/gameover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/gameover.png -------------------------------------------------------------------------------- /assets/get-ready.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/get-ready.png -------------------------------------------------------------------------------- /assets/ground-hit.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/ground-hit.wav -------------------------------------------------------------------------------- /assets/ground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/ground.png -------------------------------------------------------------------------------- /assets/instructions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/instructions.png -------------------------------------------------------------------------------- /assets/medals.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/medals.png -------------------------------------------------------------------------------- /assets/ouch.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/ouch.wav -------------------------------------------------------------------------------- /assets/particle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/particle.png -------------------------------------------------------------------------------- /assets/pipe-hit.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/pipe-hit.wav -------------------------------------------------------------------------------- /assets/pipes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/pipes.png -------------------------------------------------------------------------------- /assets/poop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/poop.png -------------------------------------------------------------------------------- /assets/preloader.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/preloader.gif -------------------------------------------------------------------------------- /assets/score.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/score.wav -------------------------------------------------------------------------------- /assets/scoreboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/scoreboard.png -------------------------------------------------------------------------------- /assets/start-button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/start-button.png -------------------------------------------------------------------------------- /assets/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/test.png -------------------------------------------------------------------------------- /assets/title.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaping/flappy-bird/947ca3d081aa3c8476b12f68ef9f409b0ab08e3e/assets/title.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | flappy bird 10 | 14 | 15 | 16 | 17 |
18 | 19 |

20 | 用Phaser来制作一个html5游戏——flappy bird 21 |

22 | 23 | -------------------------------------------------------------------------------- /js/game.js: -------------------------------------------------------------------------------- 1 | var game = new Phaser.Game(320,505,Phaser.AUTO,'game'); //实例化game 2 | game.States = {}; //存放state对象 3 | 4 | game.States.boot = function(){ 5 | this.preload = function(){ 6 | if(!game.device.desktop){//移动设备适应 7 | this.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT; 8 | this.scale.forcePortrait = true; 9 | this.scale.refresh(); 10 | } 11 | game.load.image('loading','assets/preloader.gif'); 12 | }; 13 | this.create = function(){ 14 | game.state.start('preload'); //跳转到资源加载页面 15 | }; 16 | } 17 | 18 | game.States.preload = function(){ 19 | this.preload = function(){ 20 | var preloadSprite = game.add.sprite(35,game.height/2,'loading'); //创建显示loading进度的sprite 21 | game.load.setPreloadSprite(preloadSprite); 22 | //以下为要加载的资源 23 | game.load.image('background','assets/background.png'); //背景 24 | game.load.image('ground','assets/ground.png'); //地面 25 | game.load.image('title','assets/title.png'); //游戏标题 26 | game.load.spritesheet('bird','assets/bird.png',34,24,3); //鸟 27 | game.load.image('btn','assets/start-button.png'); //按钮 28 | game.load.spritesheet('pipe','assets/pipes.png',54,320,2); //管道 29 | game.load.bitmapFont('flappy_font', 'assets/fonts/flappyfont/flappyfont.png', 'assets/fonts/flappyfont/flappyfont.fnt'); 30 | game.load.audio('fly_sound', 'assets/flap.wav');//飞翔的音效 31 | game.load.audio('score_sound', 'assets/score.wav');//得分的音效 32 | game.load.audio('hit_pipe_sound', 'assets/pipe-hit.wav'); //撞击管道的音效 33 | game.load.audio('hit_ground_sound', 'assets/ouch.wav'); //撞击地面的音效 34 | 35 | game.load.image('ready_text','assets/get-ready.png'); 36 | game.load.image('play_tip','assets/instructions.png'); 37 | game.load.image('game_over','assets/gameover.png'); 38 | game.load.image('score_board','assets/scoreboard.png'); 39 | } 40 | this.create = function(){ 41 | game.state.start('menu'); 42 | } 43 | } 44 | 45 | game.States.menu = function(){ 46 | this.create = function(){ 47 | game.add.tileSprite(0,0,game.width,game.height,'background').autoScroll(-10,0); //背景图 48 | game.add.tileSprite(0,game.height-112,game.width,112,'ground').autoScroll(-100,0); //地板 49 | var titleGroup = game.add.group(); //创建存放标题的组 50 | titleGroup.create(0,0,'title'); //标题 51 | var bird = titleGroup.create(190, 10, 'bird'); //添加bird到组里 52 | bird.animations.add('fly'); //添加动画 53 | bird.animations.play('fly',12,true); //播放动画 54 | titleGroup.x = 35; 55 | titleGroup.y = 100; 56 | game.add.tween(titleGroup).to({ y:120 },1000,null,true,0,Number.MAX_VALUE,true); //标题的缓动动画 57 | var btn = game.add.button(game.width/2,game.height/2,'btn',function(){//开始按钮 58 | game.state.start('play'); 59 | }); 60 | btn.anchor.setTo(0.5,0.5); 61 | } 62 | } 63 | 64 | game.States.play = function(){ 65 | this.create = function(){ 66 | this.bg = game.add.tileSprite(0,0,game.width,game.height,'background');//背景图 67 | this.pipeGroup = game.add.group(); 68 | this.pipeGroup.enableBody = true; 69 | this.ground = game.add.tileSprite(0,game.height-112,game.width,112,'ground'); //地板 70 | this.bird = game.add.sprite(50,150,'bird'); //鸟 71 | this.bird.animations.add('fly'); 72 | this.bird.animations.play('fly',12,true); 73 | this.bird.anchor.setTo(0.5, 0.5); 74 | game.physics.enable(this.bird,Phaser.Physics.ARCADE); //开启鸟的物理系统 75 | this.bird.body.gravity.y = 0; //鸟的重力,未开始游戏,先先让他不动 76 | game.physics.enable(this.ground,Phaser.Physics.ARCADE);//地面 77 | this.ground.body.immovable = true; //固定不动 78 | 79 | this.soundFly = game.add.sound('fly_sound'); 80 | this.soundScore = game.add.sound('score_sound'); 81 | this.soundHitPipe = game.add.sound('hit_pipe_sound'); 82 | this.soundHitGround = game.add.sound('hit_ground_sound'); 83 | this.scoreText = game.add.bitmapText(game.world.centerX-20, 30, 'flappy_font', '0', 36); 84 | 85 | this.readyText = game.add.image(game.width/2, 40, 'ready_text'); //get ready 文字 86 | this.playTip = game.add.image(game.width/2,300,'play_tip'); //提示点击 87 | this.readyText.anchor.setTo(0.5, 0); 88 | this.playTip.anchor.setTo(0.5, 0); 89 | 90 | this.hasStarted = false; //游戏是否已开始 91 | game.time.events.loop(900, this.generatePipes, this); 92 | game.time.events.stop(false); 93 | game.input.onDown.addOnce(this.statrGame, this); 94 | }; 95 | this.update = function(){ 96 | if(!this.hasStarted) return; //游戏未开始 97 | game.physics.arcade.collide(this.bird,this.ground, this.hitGround, null, this); //与地面碰撞 98 | game.physics.arcade.overlap(this.bird, this.pipeGroup, this.hitPipe, null, this); //与管道碰撞 99 | if(this.bird.angle < 90) this.bird.angle += 2.5; //下降时头朝下 100 | this.pipeGroup.forEachExists(this.checkScore,this); //分数检测和更新 101 | } 102 | 103 | this.statrGame = function(){ 104 | this.gameSpeed = 200; //游戏速度 105 | this.gameIsOver = false; 106 | this.hasHitGround = false; 107 | this.hasStarted = true; 108 | this.score = 0; 109 | this.bg.autoScroll(-(this.gameSpeed/10),0); 110 | this.ground.autoScroll(-this.gameSpeed,0); 111 | this.bird.body.gravity.y = 1150; //鸟的重力 112 | this.readyText.destroy(); 113 | this.playTip.destroy(); 114 | game.input.onDown.add(this.fly, this); 115 | game.time.events.start(); 116 | } 117 | 118 | this.stopGame = function(){ 119 | this.bg.stopScroll(); 120 | this.ground.stopScroll(); 121 | this.pipeGroup.forEachExists(function(pipe){ 122 | pipe.body.velocity.x = 0; 123 | }, this); 124 | this.bird.animations.stop('fly', 0); 125 | game.input.onDown.remove(this.fly,this); 126 | game.time.events.stop(true); 127 | } 128 | 129 | this.fly = function(){ 130 | this.bird.body.velocity.y = -350; 131 | game.add.tween(this.bird).to({angle:-30}, 100, null, true, 0, 0, false); //上升时头朝上 132 | this.soundFly.play(); 133 | } 134 | 135 | this.hitPipe = function(){ 136 | if(this.gameIsOver) return; 137 | this.soundHitPipe.play(); 138 | this.gameOver(); 139 | } 140 | this.hitGround = function(){ 141 | if(this.hasHitGround) return; //已经撞击过地面 142 | this.hasHitGround = true; 143 | this.soundHitGround.play(); 144 | this.gameOver(true); 145 | } 146 | this.gameOver = function(show_text){ 147 | this.gameIsOver = true; 148 | this.stopGame(); 149 | if(show_text) this.showGameOverText(); 150 | }; 151 | 152 | this.showGameOverText = function(){ 153 | this.scoreText.destroy(); 154 | game.bestScore = game.bestScore || 0; 155 | if(this.score > game.bestScore) game.bestScore = this.score; //最好分数 156 | this.gameOverGroup = game.add.group(); //添加一个组 157 | var gameOverText = this.gameOverGroup.create(game.width/2,0,'game_over'); //game over 文字图片 158 | var scoreboard = this.gameOverGroup.create(game.width/2,70,'score_board'); //分数板 159 | var currentScoreText = game.add.bitmapText(game.width/2 + 60, 105, 'flappy_font', this.score+'', 20, this.gameOverGroup); //当前分数 160 | var bestScoreText = game.add.bitmapText(game.width/2 + 60, 153, 'flappy_font', game.bestScore+'', 20, this.gameOverGroup); //最好分数 161 | var replayBtn = game.add.button(game.width/2, 210, 'btn', function(){//重玩按钮 162 | game.state.start('play'); 163 | }, this, null, null, null, null, this.gameOverGroup); 164 | gameOverText.anchor.setTo(0.5, 0); 165 | scoreboard.anchor.setTo(0.5, 0); 166 | replayBtn.anchor.setTo(0.5, 0); 167 | this.gameOverGroup.y = 30; 168 | } 169 | 170 | this.generatePipes = function(gap){ //制造管道 171 | gap = gap || 100; //上下管道之间的间隙宽度 172 | var position = (505 - 320 - gap) + Math.floor((505 - 112 - 30 - gap - 505 + 320 + gap) * Math.random()); 173 | var topPipeY = position-360; 174 | var bottomPipeY = position+gap; 175 | 176 | if(this.resetPipe(topPipeY,bottomPipeY)) return; 177 | 178 | var topPipe = game.add.sprite(game.width, topPipeY, 'pipe', 0, this.pipeGroup); 179 | var bottomPipe = game.add.sprite(game.width, bottomPipeY, 'pipe', 1, this.pipeGroup); 180 | this.pipeGroup.setAll('checkWorldBounds',true); 181 | this.pipeGroup.setAll('outOfBoundsKill',true); 182 | this.pipeGroup.setAll('body.velocity.x', -this.gameSpeed); 183 | } 184 | 185 | this.resetPipe = function(topPipeY,bottomPipeY){//重置出了边界的管道,做到回收利用 186 | var i = 0; 187 | this.pipeGroup.forEachDead(function(pipe){ 188 | if(pipe.y<=0){ //topPipe 189 | pipe.reset(game.width, topPipeY); 190 | pipe.hasScored = false; //重置为未得分 191 | }else{ 192 | pipe.reset(game.width, bottomPipeY); 193 | } 194 | pipe.body.velocity.x = -this.gameSpeed; 195 | i++; 196 | }, this); 197 | return i == 2; //如果 i==2 代表有一组管道已经出了边界,可以回收这组管道了 198 | } 199 | 200 | this.checkScore = function(pipe){//负责分数的检测和更新 201 | if(!pipe.hasScored && pipe.y<=0 && pipe.x<=this.bird.x-17-54){ 202 | pipe.hasScored = true; 203 | this.scoreText.text = ++this.score; 204 | this.soundScore.play(); 205 | return true; 206 | } 207 | return false; 208 | } 209 | } 210 | 211 | //添加state到游戏 212 | game.state.add('boot',game.States.boot); 213 | game.state.add('preload',game.States.preload); 214 | game.state.add('menu',game.States.menu); 215 | game.state.add('play',game.States.play); 216 | game.state.start('boot'); //启动游戏 217 | 218 | -------------------------------------------------------------------------------- /js/game.sublime-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "auto_complete": 3 | { 4 | "selected_items": 5 | [ 6 | [ 7 | "g", 8 | "game (?)" 9 | ], 10 | [ 11 | "b", 12 | "body" 13 | ], 14 | [ 15 | "P", 16 | "Phaser" 17 | ], 18 | [ 19 | "c", 20 | "createCursorKeys" 21 | ], 22 | [ 23 | "con", 24 | "condition" 25 | ] 26 | ] 27 | }, 28 | "buffers": 29 | [ 30 | ], 31 | "build_system": "", 32 | "command_palette": 33 | { 34 | "height": 392.0, 35 | "selected_items": 36 | [ 37 | [ 38 | "te", 39 | "TernJS: Reload" 40 | ], 41 | [ 42 | "Package Control: ", 43 | "Package Control: List Packages" 44 | ] 45 | ], 46 | "width": 504.0 47 | }, 48 | "console": 49 | { 50 | "height": 0.0, 51 | "history": 52 | [ 53 | ] 54 | }, 55 | "distraction_free": 56 | { 57 | "menu_visible": true, 58 | "show_minimap": false, 59 | "show_open_files": false, 60 | "show_tabs": false, 61 | "side_bar_visible": false, 62 | "status_bar_visible": false 63 | }, 64 | "file_history": 65 | [ 66 | "/E/wamp/www/GAME/demo/flappy-bird/js/game.js", 67 | "/D/Program Files/Sublime Text3/Data/Packages/TernJS/TernJS.sublime-settings", 68 | "/E/wamp/www/GAME/demo/flappy-bird/js/phaser.js", 69 | "/E/wamp/www/GAME/demo/flappy-bird/index.html", 70 | "/D/Program Files/Sublime Text3/Data/Packages/SublimeCodeIntel/SublimeCodeIntel.sublime-settings", 71 | "/C/Users/Administrator/.codeintel/config", 72 | "/C/Users/Administrator/Desktop/phaser.js", 73 | "/D/Program Files/Sublime Text3/Data/Packages/User/SublimeCodeIntel.sublime-settings", 74 | "/E/wamp/www/GAME/demo/flappy-bird/.codeintel/config", 75 | "/C/Users/Administrator/Desktop/a.html" 76 | ], 77 | "find": 78 | { 79 | "height": 0.0 80 | }, 81 | "find_in_files": 82 | { 83 | "height": 0.0, 84 | "where_history": 85 | [ 86 | ] 87 | }, 88 | "find_state": 89 | { 90 | "case_sensitive": false, 91 | "find_history": 92 | [ 93 | ], 94 | "highlight": true, 95 | "in_selection": false, 96 | "preserve_case": false, 97 | "regex": false, 98 | "replace_history": 99 | [ 100 | ], 101 | "reverse": false, 102 | "show_context": true, 103 | "use_buffer2": true, 104 | "whole_word": false, 105 | "wrap": true 106 | }, 107 | "groups": 108 | [ 109 | { 110 | "sheets": 111 | [ 112 | ] 113 | } 114 | ], 115 | "incremental_find": 116 | { 117 | "height": 0.0 118 | }, 119 | "input": 120 | { 121 | "height": 35.0 122 | }, 123 | "layout": 124 | { 125 | "cells": 126 | [ 127 | [ 128 | 0, 129 | 0, 130 | 1, 131 | 1 132 | ] 133 | ], 134 | "cols": 135 | [ 136 | 0.0, 137 | 1.0 138 | ], 139 | "rows": 140 | [ 141 | 0.0, 142 | 1.0 143 | ] 144 | }, 145 | "menu_visible": true, 146 | "project": "game.sublime-project", 147 | "replace": 148 | { 149 | "height": 0.0 150 | }, 151 | "save_all_on_build": true, 152 | "select_file": 153 | { 154 | "height": 0.0, 155 | "selected_items": 156 | [ 157 | ], 158 | "width": 0.0 159 | }, 160 | "select_project": 161 | { 162 | "height": 500.0, 163 | "selected_items": 164 | [ 165 | ], 166 | "width": 380.0 167 | }, 168 | "select_symbol": 169 | { 170 | "height": 0.0, 171 | "selected_items": 172 | [ 173 | ], 174 | "width": 0.0 175 | }, 176 | "settings": 177 | { 178 | }, 179 | "show_minimap": true, 180 | "show_open_files": false, 181 | "show_tabs": true, 182 | "side_bar_visible": true, 183 | "side_bar_width": 150.0, 184 | "status_bar_visible": true, 185 | "template_settings": 186 | { 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /project.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "follow_symlinks": true, 6 | "path": "E:\\wamp\\www\\GAME\\demo\\flappy-bird" 7 | } 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /project.sublime-workspace: -------------------------------------------------------------------------------- 1 | { 2 | "auto_complete": 3 | { 4 | "selected_items": 5 | [ 6 | [ 7 | "addO", 8 | "addOnce(a, b, c)" 9 | ], 10 | [ 11 | "add", 12 | "add(a, b, c)" 13 | ], 14 | [ 15 | "ga", 16 | "gameIsOver" 17 | ], 18 | [ 19 | "sou", 20 | "soundHitGround" 21 | ], 22 | [ 23 | "hi", 24 | "hitGround" 25 | ], 26 | [ 27 | "for", 28 | "forEachExists" 29 | ], 30 | [ 31 | "pip", 32 | "pipeGroup" 33 | ], 34 | [ 35 | "set", 36 | "setTo(x, y)" 37 | ], 38 | [ 39 | "an", 40 | "anchor" 41 | ], 42 | [ 43 | "fl", 44 | "flappy_font" 45 | ], 46 | [ 47 | "center", 48 | "centerY (num)" 49 | ], 50 | [ 51 | "bi", 52 | "bitmapText(x, y, font, text, size, group)" 53 | ], 54 | [ 55 | "b", 56 | "bitmapFont(key, textureURL, xmlURL, xmlData, xSpacing, ySpacing)" 57 | ], 58 | [ 59 | "s", 60 | "stop(name, resetFrame)" 61 | ], 62 | [ 63 | "ov", 64 | "overlapCallback (?)" 65 | ], 66 | [ 67 | "stop", 68 | "stopGame" 69 | ], 70 | [ 71 | "o", 72 | "overlapCallback" 73 | ], 74 | [ 75 | "pi", 76 | "pipeGroup" 77 | ], 78 | [ 79 | "start", 80 | "statrGame" 81 | ], 82 | [ 83 | "p", 84 | "pointer {}" 85 | ], 86 | [ 87 | "mou", 88 | "mousePointer {}" 89 | ], 90 | [ 91 | "re", 92 | "return" 93 | ], 94 | [ 95 | "bo", 96 | "bottomPipe {}" 97 | ], 98 | [ 99 | "to", 100 | "topPipeY (num)" 101 | ], 102 | [ 103 | "top", 104 | "topPipeY" 105 | ], 106 | [ 107 | "forE", 108 | "forEachDead(callback, callbackContext)" 109 | ], 110 | [ 111 | "pipe", 112 | "pipeGroup (?)" 113 | ], 114 | [ 115 | "ch", 116 | "checkWorldBounds (bool)" 117 | ], 118 | [ 119 | "enable", 120 | "enableBody (bool)" 121 | ], 122 | [ 123 | "ge", 124 | "generatePipes (?)" 125 | ], 126 | [ 127 | "gr", 128 | "group(parent, name, addToStage, enableBody, physicsBodyType)" 129 | ], 130 | [ 131 | "g", 132 | "ground (?)" 133 | ], 134 | [ 135 | "tit", 136 | "titleGroup {}" 137 | ], 138 | [ 139 | "ph", 140 | "physics" 141 | ], 142 | [ 143 | "S", 144 | "Sprites (?)" 145 | ], 146 | [ 147 | "P", 148 | "Physics(game, config)" 149 | ], 150 | [ 151 | "Re", 152 | "RegExp (fn)" 153 | ], 154 | [ 155 | "c", 156 | "createCursorKeys" 157 | ], 158 | [ 159 | "con", 160 | "condition" 161 | ] 162 | ] 163 | }, 164 | "buffers": 165 | [ 166 | { 167 | "file": "index.html", 168 | "settings": 169 | { 170 | "buffer_size": 248, 171 | "line_ending": "Windows" 172 | } 173 | }, 174 | { 175 | "file": "js/game.js", 176 | "settings": 177 | { 178 | "buffer_size": 7839, 179 | "line_ending": "Windows" 180 | } 181 | }, 182 | { 183 | "file": "js/code.js", 184 | "settings": 185 | { 186 | "buffer_size": 9328, 187 | "line_ending": "Windows" 188 | } 189 | } 190 | ], 191 | "build_system": "", 192 | "command_palette": 193 | { 194 | "height": 392.0, 195 | "selected_items": 196 | [ 197 | [ 198 | "tern", 199 | "TernJS: Reload" 200 | ], 201 | [ 202 | "ter", 203 | "TernJS: Jump to definition" 204 | ], 205 | [ 206 | "te", 207 | "TernJS: Jump to definition" 208 | ], 209 | [ 210 | "t", 211 | "TernJS: Reload" 212 | ], 213 | [ 214 | "Package Control: ", 215 | "Package Control: List Packages" 216 | ] 217 | ], 218 | "width": 504.0 219 | }, 220 | "console": 221 | { 222 | "height": 0.0, 223 | "history": 224 | [ 225 | ] 226 | }, 227 | "distraction_free": 228 | { 229 | "menu_visible": true, 230 | "show_minimap": false, 231 | "show_open_files": false, 232 | "show_tabs": false, 233 | "side_bar_visible": false, 234 | "status_bar_visible": false 235 | }, 236 | "file_history": 237 | [ 238 | "/C/Users/Administrator/AppData/Roaming/Titanium/mobilesdk/win32/3.2.1.GA/android/android.py", 239 | "/C/Users/Administrator/AppData/Roaming/Titanium/mobilesdk/win32/3.2.1.GA/module/android/build.xml", 240 | "/E/wamp/www/GAME/demo/flappy-bird/js/game.js", 241 | "/E/wamp/www/GAME/demo/flappy-bird/js/game2.js", 242 | "/E/wamp/www/GAME/demo/flappy-bird/project.sublime-project", 243 | "/C/Users/Administrator/Desktop/111.sublime-project", 244 | "/D/Program Files/Sublime Text3/Data/Packages/User/Preferences.sublime-settings", 245 | "/D/Program Files/Sublime Text3/Data/Packages/Default/Preferences.sublime-settings", 246 | "/E/wamp/www/GAME/demo/flappy-bird/js/sea.js", 247 | "/D/Program Files/Sublime Text3/Data/Packages/tern_for_sublime/JavaScript.sublime-settings", 248 | "/D/Program Files/Sublime Text3/Data/Packages/TernJS/TernJS.sublime-settings", 249 | "/E/wamp/www/GAME/demo/flappy-bird/js/phaser.js", 250 | "/E/wamp/www/GAME/demo/flappy-bird/index.html", 251 | "/D/Program Files/Sublime Text3/Data/Packages/SublimeCodeIntel/SublimeCodeIntel.sublime-settings", 252 | "/C/Users/Administrator/.codeintel/config", 253 | "/C/Users/Administrator/Desktop/phaser.js", 254 | "/D/Program Files/Sublime Text3/Data/Packages/User/SublimeCodeIntel.sublime-settings", 255 | "/E/wamp/www/GAME/demo/flappy-bird/.codeintel/config", 256 | "/C/Users/Administrator/Desktop/a.html" 257 | ], 258 | "find": 259 | { 260 | "height": 0.0 261 | }, 262 | "find_in_files": 263 | { 264 | "height": 0.0, 265 | "where_history": 266 | [ 267 | ] 268 | }, 269 | "find_state": 270 | { 271 | "case_sensitive": false, 272 | "find_history": 273 | [ 274 | ], 275 | "highlight": true, 276 | "in_selection": false, 277 | "preserve_case": false, 278 | "regex": false, 279 | "replace_history": 280 | [ 281 | ], 282 | "reverse": false, 283 | "show_context": true, 284 | "use_buffer2": true, 285 | "whole_word": false, 286 | "wrap": true 287 | }, 288 | "groups": 289 | [ 290 | { 291 | "selected": 1, 292 | "sheets": 293 | [ 294 | { 295 | "buffer": 0, 296 | "file": "index.html", 297 | "semi_transient": false, 298 | "settings": 299 | { 300 | "buffer_size": 248, 301 | "regions": 302 | { 303 | }, 304 | "selection": 305 | [ 306 | [ 307 | 168, 308 | 168 309 | ] 310 | ], 311 | "settings": 312 | { 313 | "encoding_helper_encoding": "UTF-8", 314 | "origin_encoding": "UTF-8", 315 | "syntax": "Packages/HTML/HTML.tmLanguage" 316 | }, 317 | "translation.x": 0.0, 318 | "translation.y": 0.0, 319 | "zoom_level": 1.0 320 | }, 321 | "type": "text" 322 | }, 323 | { 324 | "buffer": 1, 325 | "file": "js/game.js", 326 | "semi_transient": false, 327 | "settings": 328 | { 329 | "buffer_size": 7839, 330 | "regions": 331 | { 332 | }, 333 | "selection": 334 | [ 335 | [ 336 | 7556, 337 | 7579 338 | ] 339 | ], 340 | "settings": 341 | { 342 | "encoding_helper_encoding": "UTF-8", 343 | "origin_encoding": "UTF-8", 344 | "syntax": "Packages/JavaScript/JavaScript.tmLanguage", 345 | "translate_tabs_to_spaces": false 346 | }, 347 | "translation.x": 0.0, 348 | "translation.y": 3680.0, 349 | "zoom_level": 1.0 350 | }, 351 | "type": "text" 352 | }, 353 | { 354 | "buffer": 2, 355 | "file": "js/code.js", 356 | "semi_transient": false, 357 | "settings": 358 | { 359 | "buffer_size": 9328, 360 | "regions": 361 | { 362 | }, 363 | "selection": 364 | [ 365 | [ 366 | 865, 367 | 865 368 | ] 369 | ], 370 | "settings": 371 | { 372 | "encoding_helper_encoding": "UTF-8", 373 | "syntax": "Packages/JavaScript/JavaScript.tmLanguage", 374 | "translate_tabs_to_spaces": false 375 | }, 376 | "translation.x": 0.0, 377 | "translation.y": 0.0, 378 | "zoom_level": 1.0 379 | }, 380 | "type": "text" 381 | } 382 | ] 383 | } 384 | ], 385 | "incremental_find": 386 | { 387 | "height": 0.0 388 | }, 389 | "input": 390 | { 391 | "height": 35.0 392 | }, 393 | "layout": 394 | { 395 | "cells": 396 | [ 397 | [ 398 | 0, 399 | 0, 400 | 1, 401 | 1 402 | ] 403 | ], 404 | "cols": 405 | [ 406 | 0.0, 407 | 1.0 408 | ], 409 | "rows": 410 | [ 411 | 0.0, 412 | 1.0 413 | ] 414 | }, 415 | "menu_visible": true, 416 | "project": "project.sublime-project", 417 | "replace": 418 | { 419 | "height": 0.0 420 | }, 421 | "save_all_on_build": true, 422 | "select_file": 423 | { 424 | "height": 0.0, 425 | "selected_items": 426 | [ 427 | ], 428 | "width": 0.0 429 | }, 430 | "select_project": 431 | { 432 | "height": 500.0, 433 | "selected_items": 434 | [ 435 | ], 436 | "width": 380.0 437 | }, 438 | "select_symbol": 439 | { 440 | "height": 0.0, 441 | "selected_items": 442 | [ 443 | ], 444 | "width": 0.0 445 | }, 446 | "settings": 447 | { 448 | }, 449 | "show_minimap": false, 450 | "show_open_files": false, 451 | "show_tabs": true, 452 | "side_bar_visible": true, 453 | "side_bar_width": 150.0, 454 | "status_bar_visible": true, 455 | "template_settings": 456 | { 457 | } 458 | } 459 | --------------------------------------------------------------------------------