forked from volpeo/tp_git
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from loziamina/master
create pull request
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
var game = new Phaser.Game(400, 490, Phaser.AUTO, 'game'); | ||
|
||
var mainState = { | ||
|
||
preload: function() { | ||
game.stage.backgroundColor = '#FF6A5E'; | ||
bird = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyAQMAAAAk8RryAAAABlBMVEXSvicAAABogyUZAAAAGUlEQVR4AWP4DwYHMOgHDEDASCN6lMYV7gChf3AJ/eB/pQAAAABJRU5ErkJggg=="; | ||
pipe = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyAQMAAAAk8RryAAAABlBMVEV0vy4AAADnrrHQAAAAGUlEQVR4AWP4DwYHMOgHDEDASCN6lMYV7gChf3AJ/eB/pQAAAABJRU5ErkJggg=="; | ||
game.load.image('bird', bird); | ||
game.load.image('pipe', pipe); | ||
}, | ||
|
||
create: function() { | ||
game.physics.startSystem(Phaser.Physics.ARCADE); | ||
|
||
this.pipes = game.add.group(); | ||
this.pipes.enableBody = true; | ||
this.pipes.createMultiple(20, 'pipe'); | ||
this.timer = this.game.time.events.loop(1500, this.addRowOfPipes, this); | ||
|
||
this.bird = this.game.add.sprite(100, 245, 'bird'); | ||
game.physics.arcade.enable(this.bird); | ||
this.bird.body.gravity.y = 1000; | ||
|
||
// New anchor position | ||
this.bird.anchor.setTo(-0.2, 0.5); | ||
|
||
var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); | ||
spaceKey.onDown.add(this.jump, this); | ||
|
||
this.score = 0; | ||
this.labelScore = this.game.add.text(20, 20, "0", { font: "30px Arial", fill: "#ffffff" }); | ||
|
||
// Add the jump sound | ||
this.jumpSound = this.game.add.audio('jump'); | ||
}, | ||
|
||
update: function() { | ||
if (this.bird.inWorld == false) | ||
this.restartGame(); | ||
|
||
game.physics.arcade.overlap(this.bird, this.pipes, this.hitPipe, null, this); | ||
|
||
// Rotate the bird | ||
if (this.bird.angle < 20) | ||
this.bird.angle += 1; | ||
}, | ||
|
||
jump: function() { | ||
// If the bird is dead, he can't jump | ||
if (this.bird.alive == false) | ||
return; | ||
|
||
this.bird.body.velocity.y = -350; | ||
|
||
// Jump animation | ||
game.add.tween(this.bird).to({angle: -20}, 100).start(); | ||
|
||
}, | ||
|
||
hitPipe: function() { | ||
// If the bird has already hit a pipe, we have nothing to do | ||
if (this.bird.alive == false) | ||
return; | ||
|
||
// Set the alive property of the bird to false | ||
this.bird.alive = false; | ||
|
||
// Prevent new pipes from appearing | ||
this.game.time.events.remove(this.timer); | ||
|
||
// Go through all the pipes, and stop their movement | ||
this.pipes.forEachAlive(function(p){ | ||
p.body.velocity.x = 0; | ||
}, this); | ||
}, | ||
|
||
restartGame: function() { | ||
game.state.start('main'); | ||
}, | ||
|
||
addOnePipe: function(x, y) { | ||
var pipe = this.pipes.getFirstDead(); | ||
|
||
pipe.reset(x, y); | ||
pipe.body.velocity.x = -200; | ||
pipe.checkWorldBounds = true; | ||
pipe.outOfBoundsKill = true; | ||
}, | ||
|
||
addRowOfPipes: function() { | ||
var hole = Math.floor(Math.random()*5)+1; | ||
|
||
for (var i = 0; i < 8; i++) | ||
if (i != hole && i != hole +1) | ||
this.addOnePipe(400, i*60+10); | ||
|
||
this.score += 1; | ||
this.labelScore.text = this.score; | ||
}, | ||
}; | ||
|
||
game.state.add('main', mainState); | ||
game.state.start('main'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<script src="http://cdnjs.cloudflare.com/ajax/libs/phaser/2.1.1/phaser.min.js"></script> | ||
<script src="game.js"></script> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Flappy Clone</title> | ||
</head> | ||
<body> | ||
<p>Appuyer sur la barre espace pour sauter</p> | ||
<div id="game"></div> | ||
</body> | ||
</html> |