Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Flappy Bird game, Game development, HTML5, Javascript and Phaser.

Ok, I am (quite) a bit in late to show you how to make an HTML5 Flappy Bird game because a lot of tutorial writer already published several “Create Flappy Bird with < language name >, anywway this is my take in the making of this game, basically for two reasons:

1) It’s the perfect game, in its simplicty, to merge some Phaser concepts such as states, extending classes and using Arcade physics.

2) It will be the starting point in the making of another endless runner tutorial.

So, let me clarify some concepts about this game:

* Like all endless runner games, the character you control does not actually move. It’s the whole environment which moves towards you.

* Obstacles, powerups and other interactive stuff appear from the right, move to the left and must be removed once they leave the stage.

* In this specific case, the game is built using Arcade physics, where the bird is the only actor to be affected by gravity, while pipes only move thanks to an horizontal force

* All pipes are children of a single group, and we are going to check for collisions between the bird and such group

* To restart the game, we simply call the game state, and Phaser will make the dirty job for us.

Before you dive into the really simple code, I suggest you to have a look to Phaser Tutorial: understanding Phaser states to see how Phaser manages states and Create an HTML5 game like “Drop Wizard” with Phaser – player fire, by extending sprite class to have the basics about extending classes.

This is the game you are about to create:

Use the mouse to flap.

And this is the source code, with space for customization as you can see from the comments on the first variables.

window.onload = function() {	
	var game = new Phaser.Game(320, 480, Phaser.CANVAS);
	var bird;
     // bird gravity, will make bird fall if you don't flap
	var birdGravity = 800;
     // horizontal bird speed
	var birdSpeed = 125;
     // flap thrust
	var birdFlapPower = 300;
     // milliseconds between the creation of two pipes
	var pipeInterval = 2000;
     // hole between pipes, in puxels
	var pipeHole = 120;
	var pipeGroup;
	var score=0;
	var scoreText;
     var topScore;
     
     var play = function(game){}
     
     play.prototype = {
		preload:function(){
			game.load.image("bird", "bird.png"); 
			game.load.image("pipe", "pipe.png");	
		},
		create:function(){
			pipeGroup = game.add.group();
			score = 0;
			topScore = localStorage.getItem("topFlappyScore")==null?0:localStorage.getItem("topFlappyScore");
			scoreText = game.add.text(10,10,"-",{
				font:"bold 16px Arial"
			});
			updateScore();
			game.stage.backgroundColor = "#87CEEB";
			game.stage.disableVisibilityChange = true;
			game.physics.startSystem(Phaser.Physics.ARCADE);
			bird = game.add.sprite(80,240,"bird");
			bird.anchor.set(0.5);
			game.physics.arcade.enable(bird);
			bird.body.gravity.y = birdGravity;
			game.input.onDown.add(flap, this);
			game.time.events.loop(pipeInterval, addPipe); 
			addPipe();
		},
		update:function(){
			game.physics.arcade.collide(bird, pipeGroup, die);
			if(bird.y>game.height){
				die();
			}	
		}
	}
     
     game.state.add("Play",play);
     game.state.start("Play");
     
     function updateScore(){
		scoreText.text = "Score: "+score+"\nBest: "+topScore;	
	}
     
	function flap(){
		bird.body.velocity.y = -birdFlapPower;	
	}
	
	function addPipe(){
		var pipeHolePosition = game.rnd.between(50,430-pipeHole);
		var upperPipe = new Pipe(game,320,pipeHolePosition-480,-birdSpeed);
		game.add.existing(upperPipe);
		pipeGroup.add(upperPipe);
		var lowerPipe = new Pipe(game,320,pipeHolePosition+pipeHole,-birdSpeed);
		game.add.existing(lowerPipe);
		pipeGroup.add(lowerPipe);
	}
	
	function die(){
		localStorage.setItem("topFlappyScore",Math.max(score,topScore));	
		game.state.start("Play");	
	}
	
	Pipe = function (game, x, y, speed) {
		Phaser.Sprite.call(this, game, x, y, "pipe");
		game.physics.enable(this, Phaser.Physics.ARCADE);
		this.body.velocity.x = speed;
		this.giveScore = true;	
	};
	
	Pipe.prototype = Object.create(Phaser.Sprite.prototype);
	Pipe.prototype.constructor = Pipe;
	
	Pipe.prototype.update = function() {
		if(this.x+this.width<bird.x && this.giveScore){
			score+=0.5;
			updateScore();
			this.giveScore = false;
		}
		if(this.x<-this.width){
			this.destroy();
		}
	};	
}

As said, this is only the first endless runner based on tapping, next week we’ll cover another one-button blockbuster, meanwhile download the source code.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.