Path: blob/main/public/games/files/flappy-bird/index.coffee
1036 views
DEBUG = false1SPEED = 1602GRAVITY = 11003FLAP = 3204SPAWN_RATE = 1 / 12005OPENING = 1006SCALE = 178HEIGHT = 3849WIDTH = 28810GAME_HEIGHT = 33611GROUND_HEIGHT = 6412GROUND_Y = HEIGHT - GROUND_HEIGHT1314parent = document.querySelector("#screen")15gameStarted = undefined16gameOver = undefined1718deadTubeTops = []19deadTubeBottoms = []20deadInvs = []2122bg = null23# credits = null24tubes = null25invs = null26bird = null27ground = null2829score = null30scoreText = null31instText = null32gameOverText = null3334flapSnd = null35scoreSnd = null36hurtSnd = null37fallSnd = null38swooshSnd = null3940tubesTimer = null4142githubHtml = """<iframe src="http://ghbtns.com/github-btn.html?user=hyspace&repo=flappy&type=watch&count=true&size=large"43allowtransparency="true" frameborder="0" scrolling="0" width="150" height="30"></iframe>"""4445floor = Math.floor4647main = ->48spawntube = (openPos, flipped) ->49tube = null5051tubeKey = if flipped then "tubeTop" else "tubeBottom"52if flipped53tubeY = floor(openPos - OPENING / 2 - 320)54else55tubeY = floor(openPos + OPENING / 2)5657if deadTubeTops.length > 0 and tubeKey == "tubeTop"58tube = deadTubeTops.pop().revive()59tube.reset(game.world.width, tubeY)60else if deadTubeBottoms.length > 0 and tubeKey == "tubeBottom"61tube = deadTubeBottoms.pop().revive()62tube.reset(game.world.width, tubeY)63else64tube = tubes.create(game.world.width, tubeY, tubeKey)65tube.body.allowGravity = false6667# Move to the left68tube.body.velocity.x = -SPEED69tube7071spawntubes = ->72# check dead tubes73tubes.forEachAlive (tube) ->74if tube.x + tube.width < game.world.bounds.left75deadTubeTops.push tube.kill() if tube.key == "tubeTop"76deadTubeBottoms.push tube.kill() if tube.key == "tubeBottom"77return78invs.forEachAlive (invs) ->79deadInvs.push invs.kill() if invs.x + invs.width < game.world.bounds.left80return8182tubeY = game.world.height / 2 + (Math.random()-0.5) * game.world.height * 0.28384# Bottom tube85bottube = spawntube(tubeY)8687# Top tube (flipped)88toptube = spawntube(tubeY, true)8990# Add invisible thingy91if deadInvs.length > 092inv = deadInvs.pop().revive().reset(toptube.x + toptube.width / 2, 0)93else94inv = invs.create(toptube.x + toptube.width / 2, 0)95inv.width = 296inv.height = game.world.height97inv.body.allowGravity = false98inv.body.velocity.x = -SPEED99return100101addScore = (_, inv) ->102invs.remove inv103score += 1104scoreText.setText score105scoreSnd.play()106return107108setGameOver = ->109gameOver = true110bird.body.velocity.y = 100 if bird.body.velocity.y > 0111bird.animations.stop()112bird.frame = 1113instText.setText "TOUCH\nTO TRY AGAIN"114instText.renderable = true115hiscore = window.localStorage.getItem("hiscore")116hiscore = (if hiscore then hiscore else score)117hiscore = (if score > parseInt(hiscore, 10) then score else hiscore)118window.localStorage.setItem "hiscore", hiscore119gameOverText.setText "GAMEOVER\n\nHIGH SCORE\n\n" + hiscore120gameOverText.renderable = true121122# Stop all tubes123tubes.forEachAlive (tube) ->124tube.body.velocity.x = 0125return126127invs.forEach (inv) ->128inv.body.velocity.x = 0129return130131132# Stop spawning tubes133game.time.events.remove(tubesTimer)134135# Make bird reset the game136game.time.events.add 1000, ->137game.input.onTap.addOnce ->138reset()139swooshSnd.play()140141hurtSnd.play()142return143144flap = ->145start() unless gameStarted146unless gameOver147# bird.body.velocity.y = -FLAP148bird.body.gravity.y = 0;149bird.body.velocity.y = -100;150tween = game.add.tween(bird.body.velocity).to(y:-FLAP, 25, Phaser.Easing.Bounce.In,true);151tween.onComplete.add ->152bird.body.gravity.y = GRAVITY153flapSnd.play()154return155156preload = ->157assets =158spritesheet:159bird: [160"assets/bird.png"1613616226163]164165image:166tubeTop: ["assets/tube1.png"]167tubeBottom: ["assets/tube2.png"]168ground: ["assets/ground.png"]169bg: ["assets/bg.png"]170171audio:172flap: ["assets/sfx_wing.mp3"]173score: ["assets/sfx_point.mp3"]174hurt: ["assets/sfx_hit.mp3"]175fall: ["assets/sfx_die.mp3"]176swoosh: ["assets/sfx_swooshing.mp3"]177178Object.keys(assets).forEach (type) ->179Object.keys(assets[type]).forEach (id) ->180game.load[type].apply game.load, [id].concat(assets[type][id])181return182183return184185return186187create = ->188console.log("%chttps://github.com/hyspace/flappy", "color: black; font-size: x-large");189ratio = window.innerWidth / window.innerHeight190document.querySelector('#github').innerHTML = githubHtml if ratio > 1.15 or ratio < 0.7191document.querySelector('#loading').style.display = 'none'192193# Set world dimensions194Phaser.Canvas.setSmoothingEnabled(game.context, false)195game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL196game.stage.scale.setScreenSize(true)197game.world.width = WIDTH198game.world.height = HEIGHT199200# Draw bg201bg = game.add.tileSprite(0, 0, WIDTH, HEIGHT, 'bg')202203# Credits 'yo204# credits = game.add.text(game.world.width / 2, HEIGHT - GROUND_Y + 50, "",205# font: "8px \"Press Start 2P\""206# fill: "#fff"207# stroke: "#430"208# strokeThickness: 4209# align: "center"210# )211# credits.anchor.x = 0.5212213214# # Add clouds group215# clouds = game.add.group()216217# Add tubes218tubes = game.add.group()219220# Add invisible thingies221invs = game.add.group()222223# Add bird224bird = game.add.sprite(0, 0, "bird")225bird.anchor.setTo 0.5, 0.5226bird.animations.add "fly", [227022812292230], 10, true231bird.body.collideWorldBounds = true232bird.body.setPolygon(23324,1,23434,16,23530,32,23620,24,23712,34,2382,12,23914,2240)241242# Add ground243ground = game.add.tileSprite(0, GROUND_Y, WIDTH, GROUND_HEIGHT, "ground")244ground.tileScale.setTo SCALE, SCALE245246# Add score text247scoreText = game.add.text(game.world.width / 2, game.world.height / 4, "",248font: "16px \"Press Start 2P\""249fill: "#fff"250stroke: "#430"251strokeThickness: 4252align: "center"253)254scoreText.anchor.setTo 0.5, 0.5255256# Add instructions text257instText = game.add.text(game.world.width / 2, game.world.height - game.world.height / 4, "",258font: "8px \"Press Start 2P\""259fill: "#fff"260stroke: "#430"261strokeThickness: 4262align: "center"263)264instText.anchor.setTo 0.5, 0.5265266# Add game over text267gameOverText = game.add.text(game.world.width / 2, game.world.height / 2, "",268font: "16px \"Press Start 2P\""269fill: "#fff"270stroke: "#430"271strokeThickness: 4272align: "center"273)274gameOverText.anchor.setTo 0.5, 0.5275gameOverText.scale.setTo SCALE, SCALE276277# Add sounds278flapSnd = game.add.audio("flap")279scoreSnd = game.add.audio("score")280hurtSnd = game.add.audio("hurt")281fallSnd = game.add.audio("fall")282swooshSnd = game.add.audio("swoosh")283284# Add controls285game.input.onDown.add flap286287# RESET!288reset()289return290291reset = ->292gameStarted = false293gameOver = false294score = 0295# credits.renderable = true296# credits.setText "see console log\nfor github url"297scoreText.setText "Flappy Bird"298instText.setText "TOUCH TO FLAP\nbird WINGS"299gameOverText.renderable = false300bird.body.allowGravity = false301bird.reset game.world.width * 0.3, game.world.height / 2302bird.angle = 0303bird.animations.play "fly"304tubes.removeAll()305invs.removeAll()306return307308start = ->309310# credits.renderable = false311bird.body.allowGravity = true312bird.body.gravity.y = GRAVITY313314# SPAWN tubeS!315tubesTimer = game.time.events.loop 1 / SPAWN_RATE, spawntubes316317318# Show score319scoreText.setText score320instText.renderable = false321322# START!323gameStarted = true324return325326update = ->327if gameStarted328if !gameOver329# Make bird dive330bird.angle = (90 * (FLAP + bird.body.velocity.y) / FLAP) - 180331bird.angle = -30 if bird.angle < -30332if bird.angle > 80333bird.angle = 90334bird.animations.stop()335bird.frame = 1336else337bird.animations.play()338339# Check game over340game.physics.overlap bird, tubes, ->341setGameOver()342fallSnd.play()343setGameOver() if not gameOver and bird.body.bottom >= GROUND_Y344345# Add score346game.physics.overlap bird, invs, addScore347348else349# rotate the bird to make sure its head hit ground350tween = game.add.tween(bird).to(angle: 90, 100, Phaser.Easing.Bounce.Out, true);351if bird.body.bottom >= GROUND_Y + 3352bird.y = GROUND_Y - 13353bird.body.velocity.y = 0354bird.body.allowGravity = false355bird.body.gravity.y = 0356357else358bird.y = (game.world.height / 2) + 8 * Math.cos(game.time.now / 200)359bird.angle = 0360361362# Scroll ground363ground.tilePosition.x -= game.time.physicsElapsed * SPEED unless gameOver364return365366render = ->367if DEBUG368game.debug.renderSpriteBody bird369tubes.forEachAlive (tube) ->370game.debug.renderSpriteBody tube371return372373invs.forEach (inv) ->374game.debug.renderSpriteBody inv375return376377return378379state =380preload: preload381create: create382update: update383render: render384385game = new Phaser.Game(WIDTH, HEIGHT, Phaser.CANVAS, parent, state, false, false)386return387388WebFontConfig =389google:390families: [ 'Press+Start+2P::latin' ]391active: main392(->393wf = document.createElement('script')394wf.src = (if 'https:' == document.location.protocol then 'https' else 'http') +395'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'396wf.type = 'text/javascript'397wf.async = 'true'398s = document.getElementsByTagName('script')[0]399s.parentNode.insertBefore(wf, s)400)()401402