Last active
August 29, 2015 14:08
-
-
Save jessefreeman/a087ab7e3373da8ed5e1 to your computer and use it in GitHub Desktop.
FlappyJetroid Workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SPEED = 200; | |
var GRAVITY = 900; | |
var JET = 420; | |
var OPENING = 200; | |
var SPAWN_RATE = 1.25; | |
var state = { | |
preload: function(){ | |
this.load.image('wall', '/assets/wall.png'); | |
this.load.image('background', '/assets/background-texture.png'); | |
this.load.spritesheet('player', "/assets/player.png", 48, 48); | |
this.load.audio("jet", "/assets/jet.wav"); | |
this.load.audio("score", "/assets/score.wav"); | |
this.load.audio("hurt", "/assets/hurt.wav"); | |
}, | |
create: function(){ | |
this.jetSnd = this.add.audio("jet"); | |
this.scoreSnd = this.add.audio("score"); | |
this.hurtSnd = this.add.audio("hurt"); | |
this.background = this.add.tileSprite(0,0, this.world.width, this.world.height, 'background'); | |
this.walls = this.add.group(); | |
this.physics.startSystem(Phaser.Physics.ARCADE); | |
this.physics.arcade.gravity.y = GRAVITY; | |
this.player = this.add.sprite(0,0, 'player'); | |
this.player.animations.add('fly', [0,1,2], 10, true); | |
this.physics.arcade.enableBody(this.player); | |
this.player.body.collideWorldBounds = true; | |
this.scoreText = this.add.text( | |
this.world.centerX, | |
this.world.height/5, | |
"", | |
{ | |
size: "32px", | |
fill: '#FFF', | |
align: 'center' | |
} | |
) | |
this.scoreText.anchor.setTo(.5,.5); | |
this.input.onDown.add(this.jet, this); | |
//this.spawnWall(300, true); | |
//this.spawnWalls(); | |
this.reset(); | |
}, | |
update: function(){ | |
if(this.gameStarted){ | |
if(this.player.body.velocity.y > -20){ | |
this.player.frame = 3 | |
}else{ | |
this.player.animations.play("fly"); | |
} | |
if(!this.gameOver){ | |
if(this.player.body.bottom >= this.world.bounds.bottom){ | |
this.setGameOver(); | |
} | |
this.physics.arcade.collide(this.player, this.walls, this.setGameOver, null, this); | |
this.walls.forEachAlive( | |
function(wall){ | |
if(wall.x + wall.width < game.world.bounds.left){ | |
wall.kill(); | |
}else if(!wall.scored && wall.x <= state.player.x){ | |
state.addScore(wall); | |
} | |
} | |
) | |
} | |
}else{ | |
this.player.y = this.world.centerY + (8 * Math.cos(this.time.now/200)); | |
} | |
}, | |
reset: function(){ | |
this.gameStarted = false; | |
this.gameOver = false; | |
this.score = 0; | |
this.player.body.allowGravity = false; | |
this.player.reset(this.world.width/4, this.world.centerY); | |
this.player.animations.play('fly'); | |
this.background.autoScroll(-SPEED *.8, 0); | |
this.scoreText.setText("TOUCH TO\nSTART GAME"); | |
this.walls.removeAll(); | |
}, | |
start: function(){ | |
this.player.body.allowGravity = true; | |
this.scoreText.setText("SCORE\n"+this.score); | |
this.gameStarted = true; | |
this.wallTimer = this.game.time.events.loop(Phaser.Timer.SECOND * SPAWN_RATE, this.spawnWalls, this); | |
this.wallTimer.timer.start(); | |
}, | |
jet: function(){ | |
if(!this.gameStarted){ | |
this.start(); | |
} | |
if(!this.gameOver){ | |
this.player.body.velocity.y = -JET; | |
this.jetSnd.play(); | |
}else if(this.time.now > this.timeOver + 400){ | |
this.reset(); | |
} | |
}, | |
setGameOver: function(){ | |
this.gameOver = true; | |
this.scoreText.setText("FINAL SCORE\n"+this.score+"\n\nTOUCH TO\nTRY AGAIN"); | |
this.background.autoScroll(0,0); | |
this.timeOver = this.time.now; | |
this.walls.forEachAlive( | |
function(wall){ | |
wall.body.velocity.x = wall.body.velocity.y = 0; | |
} | |
) | |
this.player.body.velocity.x = 0; | |
this.wallTimer.timer.stop(); | |
this.hurtSnd.play(); | |
}, | |
spawnWall: function(y, flipped){ | |
var wall = this.walls.create( | |
game.width, | |
y + (flipped ? -OPENING : OPENING) /2, | |
'wall' | |
) | |
this.physics.arcade.enableBody(wall); | |
wall.body.allowGravity = false; | |
wall.body.immovable = true; | |
wall.body.velocity.x = -SPEED; | |
if(flipped){ | |
wall.scale.y = -1; | |
wall.body.offset.y = -wall.body.height; | |
} | |
return wall; | |
}, | |
spawnWalls: function(){ | |
var wallY = this.rnd.integerInRange(game.height *.3, game.height *.7); | |
var botWall = this.spawnWall(wallY); | |
var topWall = this.spawnWall(wallY, true); | |
}, | |
addScore: function(wall){ | |
wall.scored = true; | |
this.score += .5; | |
this.scoreText.setText("SCORE\n"+this.score); | |
this.scoreSnd.play(); | |
} | |
} | |
var game = new Phaser.Game( | |
320, | |
568, | |
Phaser.AUTO, | |
'game', | |
state | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment