Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Game development, HTML5 and Javascript.

Panda.js is an HTML5 game engine you can use to make great games for mobile and desktop.

panda

It provides a great ease of use, as well as some nice features such as:

Canvas/WebGL renderer: Pixi.js powered rendering with super fast speed.

Physics engine: hit detection and response with different shapes.

Particle engine: create special effects with particle engine.

Tweening: tween objects with easing, looping and grouping.

Timers: add timers with callback functions and repeating.

Mobile support: make games for mobile and tablet devices.

Sound manager: manage sound effects and music with automatic looping.

Modules: split your game into modules and keep your code organized.

To make a test drive, let’s create a fully working Circle Chain game prototype.

It’s a game you should find very familiar if you are an old time reader, as it has been ported in a lot of languages: AS3, Game Maker, Construct2 HTML5, Corona SDK, Gideros Studio, Stencyl, Starling, CreateJS, Cocos2D HTML5 and Unity.

Here it is:

Click on the stage to create red explosion and blow how much green circles as you can with the chain reaction.

And this is the source code, did not comment it because the game has already been explained a million times.

game.module(
	"game.main"
)
.require(
	"engine.core"
)
.body(function(){

	game.addAsset("media/green.png","green");
	game.addAsset("media/smallgreen.png","smallgreen");
	game.addAsset("media/red.png","red");
	game.System.orientation = game.System.LANDSCAPE;
	
	GreenCircle = game.Class.extend({
		xSpeed:4,
		ySpeed:4,
		init: function(x,y,dir){
			this.xSpeed*=Math.cos(dir);
			this.ySpeed*=Math.sin(dir);
			this.sprite=new game.Sprite("green");
			this.sprite.anchor.x=0.5;
			this.sprite.anchor.y=0.5;
			this.sprite.position.x=x;
			this.sprite.position.y=y;
			game.scene.stage.addChild(this.sprite);
			game.scene.addObject(this);
		},
		update: function(){
			this.sprite.position.x+=this.xSpeed;
			this.sprite.position.y+=this.ySpeed;
			if(this.sprite.position.x<=this.sprite.width/2){
				this.sprite.position.x=this.sprite.width/2;
				this.xSpeed*=-1;
			}
			if(this.sprite.position.x>=game.system.width-this.sprite.width/2){
				this.sprite.position.x=game.system.width-this.sprite.width/2;
				this.xSpeed*=-1;
			}
			if(this.sprite.position.y<=this.sprite.height/2){
				this.sprite.position.y=this.sprite.height/2;
				this.ySpeed*=-1;
			}
			if(this.sprite.position.y>=game.system.height-this.sprite.height/2){
				this.sprite.position.y=game.system.height-this.sprite.height/2;
				this.ySpeed*=-1;
			}		
		}
	});
	
	Bullet = game.Class.extend({
		xSpeed:8,
		ySpeed:8,
		init: function(x,y,dir,spriteStr){
			this.xSpeed*=Math.cos(dir);
			this.ySpeed*=Math.sin(dir);
			this.sprite=new game.Sprite(spriteStr);
			this.sprite.anchor.x=0.5;
			this.sprite.anchor.y=0.5;
			this.sprite.position.x=x;
			this.sprite.position.y=y;
			game.scene.stage.addChild(this.sprite);
			game.scene.addObject(this);
		},
		update: function(){
			this.sprite.position.x+=this.xSpeed;
			this.sprite.position.y+=this.ySpeed;
			if(this.sprite.position.x<=0||this.sprite.position.y<=0||this.sprite.position.x>=game.system.width||this.sprite.position.y>=game.system.height){
				game.scene.stage.removeChild(this.sprite);
				game.scene.removeObject(this);
			}
			for(i=game.scene.greenArray.length-1;i>=0;i--){
				var distX=this.sprite.position.x - game.scene.greenArray[i].sprite.position.x;
				var distY=this.sprite.position.y - game.scene.greenArray[i].sprite.position.y;
				var distance = distX*distX+distY*distY;
				if(distance<(this.sprite.width/2)*(this.sprite.width/2)+(game.scene.greenArray[i].sprite.width/2)*(game.scene.greenArray[i].sprite.width/2)){
					for(j=0;j<4;j++){ 
						var bullet = new Bullet(this.sprite.position.x,this.sprite.position.y,Math.PI/2*j,"smallgreen");
					} 
					game.scene.stage.removeChild(this.sprite);
					game.scene.removeObject(this);
					game.scene.stage.removeChild(game.scene.greenArray[i].sprite);
					game.scene.removeObject(game.scene.greenArray[i]);
					game.scene.greenArray.splice(i,1);	
				}  	
			}
		}	
	})
	
	SceneGame = game.Scene.extend({
		backgroundColor: 0x000000,
		greenArray:new Array(),
    		init: function(){
    			for(i=0;i<25;i++){
    				var greenCircle=new GreenCircle(25+Math.random()*(game.system.width-50),25+Math.random()*(game.system.height-50),Math.random()*(2*Math.PI));
    				this.greenArray.push(greenCircle);
    			}
    		},
    		click: function(event){
			var x = event.global.x;
			var y = event.global.y; 
			for(i=0;i<4;i++){ 
				var bullet = new Bullet(x,y,Math.PI/2*i,"red");
			}  
		}
    		
	});

	game.start();

});

If you are interested in Panda.js I can show you how to create more complex examples.

Meanwhile download the source code with all required libraries.

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