Académique Documents
Professionnel Documents
Culture Documents
HowtomakeaFlappyBirdinHTML5Part1
12 Aug 2014
1/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
Set Up
You should download this empty template that I made
for this tutorial. In it you will find:
phaser.min.js, the Phaser framework v2.0.5
index.html, where the game will be displayed
main.js, a file where we will write all our code
assets/, a directory with 2 images (bird.png and
pipe.png)
You should also download the code editor Brackets, that
will make our life easier to set up a webserver.
Empty Project
In the index.html file we add this simple code:
<!DOCTYPEhtml>
<html>
<head>
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
2/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
<metacharset="utf8"/>
<title>FlappyBirdClone</title>
<scripttype="text/javascript"
src="phaser.min.js"></script>
<scripttype="text/javascript"src="main.js">
</script>
</head>
<body>
<p>Pressthespacebartojump</p>
<divid="gameDiv"></div>
</body>
</html>
//InitializePhaser,andcreatea400x490pxgame
vargame=newPhaser.Game(400,490,Phaser.AUTO,
'gameDiv');
//Createour'main'statethatwillcontainthe
game
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
3/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
varmainState={
preload:function(){
//Thisfunctionwillbeexecutedatthe
beginning
//That'swhereweloadthegame'sassets
},
create:function(){
//Thisfunctioniscalledafterthepreload
function
//Herewesetupthegame,displaysprites,
etc.
},
update:function(){
//Thisfunctioniscalled60timesper
second
//Itcontainsthegame'slogic
},
};
//Addandstartthe'main'statetostartthegame
game.state.add('main',mainState);
game.state.start('main');
The Bird
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
4/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
The Bird
Let's first focus on adding a bird to the game that can be
controlled with the spacebar key.
Everything is quite simple and well commented, so you
should be able to understand easily the code below. For
better readability, I removed the Phaser initialization
and states management code that you can see above.
First we update the preload() , create() and update()
functions.
preload:function(){
//Changethebackgroundcolorofthegame
game.stage.backgroundColor='#71c5cf';
//Loadthebirdsprite
game.load.image('bird','assets/bird.png');
},
create:function(){
//Setthephysicssystem
game.physics.startSystem(Phaser.Physics.ARCADE);
//Displaythebirdonthescreen
this.bird=this.game.add.sprite(100,245,
'bird');
//Addgravitytothebirdtomakeitfall
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
5/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
game.physics.arcade.enable(this.bird);
this.bird.body.gravity.y=1000;
//Callthe'jump'functionwhenthespacekeyis
hit
varspaceKey=
this.game.input.keyboard.addKey(Phaser.Keyboard.SPAC
EBAR);
spaceKey.onDown.add(this.jump,this);
},
update:function(){
//Ifthebirdisoutoftheworld(toohighor
toolow),callthe'restartGame'function
if(this.bird.inWorld==false)
this.restartGame();
},
//Makethebirdjump
jump:function(){
//Addaverticalvelocitytothebird
this.bird.body.velocity.y=350;
},
//Restartthegame
restartGame:function(){
//Startthe'main'state,whichrestartsthe
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
6/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
game
game.state.start('main');
},
Testing
Now it's time to test our game. Open the index.html file
with the Brackets editor, then click on the small icon in
the top right corner:
The Pipes
A Flappy Bird game without pipes is not really
interesting, so let's change that.
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
7/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
game.load.image('pipe','assets/pipe.png');
this.pipes=game.add.group();//Createagroup
this.pipes.enableBody=true;//Addphysicstothe
group
this.pipes.createMultiple(20,'pipe');//Create20
pipes
addOnePipe:function(x,y){
//Getthefirstdeadpipeofourgroup
varpipe=this.pipes.getFirstDead();
//Setthenewpositionofthepipe
pipe.reset(x,y);
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
8/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
//Addvelocitytothepipetomakeitmoveleft
pipe.body.velocity.x=200;
//Killthepipewhenit'snolongervisible
pipe.checkWorldBounds=true;
pipe.outOfBoundsKill=true;
},
addRowOfPipes:function(){
//Pickwheretheholewillbe
varhole=Math.floor(Math.random()*5)+1;
//Addthe6pipes
for(vari=0;i<8;i++)
if(i!=hole&&i!=hole+1)
this.addOnePipe(400,i*60+10);
},
this.timer=game.time.events.loop(1500,
this.addRowOfPipes,this);
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
9/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
Now you can save your file and test the code. This is
slowly starting to look like a real game.
this.score=0;
this.labelScore=game.add.text(20,20,"0",{font:
"30pxArial",fill:"#ffffff"});
this.score+=1;
this.labelScore.text=this.score;
game.physics.arcade.overlap(this.bird,this.pipes,
this.restartGame,null,this);
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
10/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
What's Next?
The game is working, but it's a little bit boring. In the
next tutorial we'll see how we can make it better by
adding sounds and animations. Read part 2.
For your information, I also wrote a book on how to
make a full featured game with Phaser. More
information on: DiscoverPhaser.com.
11/12
12/5/2015
HowtomakeaFlappyBirdinHTML5Part1
register
Thomas
GhosteryblockedcommentspoweredbyDisqus.
http://blog.lessmilk.com/howtomakeflappybirdinhtml51/
12/12