Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about CLOCKS - The Game game, Game development, HTML5, Javascript and Phaser.

During these days I made a couple of plays at CLOCKS – The Game by Noodlecake Studios.

It’s an one button game where you have to destroy all clocks on the stage by hitting them with a ball which is fired from clocks’ hand.

clocks

Simple but addictive, it’s the perfect game to be deconstructed to make you see how easy is to make these kind of games with Phaser and Arcade physics.

I played the first 20 levels and as far as I know, it’s a tile based game played on an 8×8 grid with two different clock sizes:

clocks

In this first part of the tutorial, we’ll see how to create the levels, both manually and using Tiled Map Editor to generate an array of data which will be converted into a level.

Here you can see the 8×8 size of the game grid:

Since most of the level is made by empty tiles, an idea would be to define arrays to store big and small clock positions, this way:

{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(5, 7),
          new Phaser.Point(5, 5),
          new Phaser.Point(7, 5),
          new Phaser.Point(11, 9),
          new Phaser.Point(11, 11),
          new Phaser.Point(9, 11)             
     ],
     bigClocks: [
          new Phaser.Point(10, 6),
          new Phaser.Point(6, 10)
     ]
}

Here you can see an object representing level 3, with small and big clocks stored into arrays.

If you prefer – I do – you can use Tiled to manually draw the levels with big and small clocks, then keep only array data, like this:

{
     clockSpeed: [200, 450],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
}

And you just have to pay attention big clocks occupy four tiles while small clocks only occupy one tile.

In this example, each three seconds a level from 1 to 10 is generated, in one of the two methods mentioned above, randomly chosen:

Click on the canvas and see. Still no interaction with the level, but it will be easily added during next tutorials.

Here is the source code, don’t worry about the lack of comments as I only wanted to explain level design. You can try to understand the code on your own, or wait for next step with the explanation about the making of the clocks.

var game;
var gridSize = 40;
var levelWidth = 8;
var levelHeight = 8;
var level = 0;


window.onload = function() {	
	game = new Phaser.Game(640, 960, Phaser.AUTO, "");
     game.state.add("Boot", boot);
     game.state.add("Preload", preload); 
     game.state.add("PlayGame", playGame);
     game.state.start("Boot");
}

var boot = function(game){};
boot.prototype = {
  	preload: function(){
          this.game.load.image("loading","assets/sprites/loading.png"); 
	},
  	create: function(){
		game.scale.pageAlignHorizontally = true;
		game.scale.pageAlignVertically = true;
          game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.state.start("Preload");
	}      
}

var preload = function(game){};
preload.prototype = {
     preload: function(){
          game.load.image("smallclock", "assets/sprites/smallclock.png");
          game.load.image("smallhand", "assets/sprites/smallhand.png");
          game.load.image("bigclock", "assets/sprites/bigclock.png");
          game.load.image("bighand", "assets/sprites/bighand.png");
     },   
     create: function(){
          game.state.start("PlayGame");
     } 
}

var playGame = function(game){};
playGame.prototype = {
     create: function(){
          this.clockGroup = game.add.group();
          this.clockGroup.y = (game.height - gridSize * 16) / 2;
          game.stage.backgroundColor = 0x2babca;
          if(game.rnd.between(0, 1) == 0){
               for(var i = 0; i < levels[level].smallClocks.length; i++){
                    this.placeClock(levels[level].smallClocks[i], "small");
               }
               for(var i = 0; i < levels[level].bigClocks.length; i++){
                    this.placeClock(levels[level].bigClocks[i], "big");
               }
          }
          else{
               for(var i = 0; i < levels[level].tiledOutput.length; i++){
                    switch(levels[level].tiledOutput[i]){
                         case 1:
                              this.placeClock(new Phaser.Point(i % 8 * 2 + 1, Math.floor(i / 8) * 2 + 1), "small");
                              break;
                         case 2:
                              this.placeClock(new Phaser.Point(i % 8 * 2 + 2, Math.floor(i / 8) * 2), "big");
                              break;
                    }
               }
          }
          game.time.events.add(Phaser.Timer.SECOND * 3, function(){
               level = (level + 1) % 10;
               game.state.start("PlayGame");
          });
     },
     placeClock: function(clockObj, prefix){
          var clockSprite = game.add.sprite(clockObj.x * gridSize, clockObj.y * gridSize, prefix + "clock");
          clockSprite.anchor.set(0.5);
          this.clockGroup.add(clockSprite);
          var handSprite = game.add.sprite(clockObj.x * gridSize, clockObj.y * gridSize, prefix + "hand");
          handSprite.anchor.set(0.5);
          handSprite.tint = 0x2babca;
          handSprite.angle = game.rnd.between(0, 359);
          game.physics.enable(handSprite, Phaser.Physics.ARCADE);
          handSprite.body.angularVelocity = game.rnd.between(levels[level].clockSpeed[0], levels[level].clockSpeed[1]) * (1 - 2 * game.rnd.between(0, 1));
          clockSprite.addChild(handSprite);
          this.clockGroup.add(handSprite);
     }
}

var levels = [
// level 1
{
     clockSpeed: [200, 450],
     smallClocks:[],
     bigClocks: [
          new Phaser.Point(6, 6),
          new Phaser.Point(10, 6),
          new Phaser.Point(6, 10),
          new Phaser.Point(10, 10)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
// level 2
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(9, 7),
          new Phaser.Point(7, 9)
     ],
     bigClocks: [
          new Phaser.Point(6, 6),
          new Phaser.Point(10, 10)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
},
// level 3
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(5, 7),
          new Phaser.Point(5, 5),
          new Phaser.Point(7, 5),
          new Phaser.Point(11, 9),
          new Phaser.Point(11, 11),
          new Phaser.Point(9, 11)             
     ],
     bigClocks: [
          new Phaser.Point(10, 6),
          new Phaser.Point(6, 10)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
},
// level 4
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(11, 5),
          new Phaser.Point(5, 11)
     ],
     bigClocks: [
          new Phaser.Point(6, 6),
          new Phaser.Point(10, 10)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
// level 5
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(11, 3),
          new Phaser.Point(13, 3),
          new Phaser.Point(13, 5),
          new Phaser.Point(3, 13),
          new Phaser.Point(3, 11),
          new Phaser.Point(5, 13),          
     ],
     bigClocks: [
          new Phaser.Point(6, 6),
          new Phaser.Point(10, 10)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
// level 6
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(7, 7),
          new Phaser.Point(9, 7),
          new Phaser.Point(11, 7),
          new Phaser.Point(5, 7),
          new Phaser.Point(7, 9),
          new Phaser.Point(9, 9),
          new Phaser.Point(11, 9),
          new Phaser.Point(5, 9),
          new Phaser.Point(7, 5),
          new Phaser.Point(9, 5),
          new Phaser.Point(7, 11),
          new Phaser.Point(9, 11)          
     ],
     bigClocks: [],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
// level 7
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(7, 7),
          new Phaser.Point(9, 7),
          new Phaser.Point(7, 9),
          new Phaser.Point(9, 9),
          new Phaser.Point(9, 5),
          new Phaser.Point(11, 5),
          new Phaser.Point(11, 3),
          new Phaser.Point(7, 11),
          new Phaser.Point(5, 11),
          new Phaser.Point(5, 13)                
     ],
     bigClocks: [],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
// level 8
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(5, 5),
          new Phaser.Point(5, 7),
          new Phaser.Point(5, 9),
          new Phaser.Point(7, 5),
          new Phaser.Point(9, 5),
          new Phaser.Point(11, 5),
          new Phaser.Point(7, 11),
          new Phaser.Point(5, 11),
          new Phaser.Point(9, 11),
          new Phaser.Point(11, 11),
          new Phaser.Point(11, 7),
          new Phaser.Point(11, 9),
          new Phaser.Point(9, 13),
          new Phaser.Point(3, 9),
          new Phaser.Point(7, 3),
          new Phaser.Point(13, 7)                 
     ],
     bigClocks: [],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
// level 9
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(11, 5),
          new Phaser.Point(5, 11),
          new Phaser.Point(7, 7),
          new Phaser.Point(9, 7),
          new Phaser.Point(7, 9),
          new Phaser.Point(9, 9),
     ],
     bigClocks: [
          new Phaser.Point(14, 2),
          new Phaser.Point(2, 14)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0] 
},
// level 10
{
     clockSpeed: [200, 450],
     smallClocks:[
          new Phaser.Point(7, 7),
          new Phaser.Point(9, 7),
          new Phaser.Point(7, 9),
          new Phaser.Point(9, 9),
     ],
     bigClocks: [
          new Phaser.Point(10, 2),
          new Phaser.Point(14, 2),
          new Phaser.Point(14, 6),
          new Phaser.Point(2, 14),
          new Phaser.Point(2, 10),
          new Phaser.Point(6, 14)
     ],
     tiledOutput: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0] 
}
]

If you look closer at the code, you will see there’s more than just level design, clocks have been already created as Arcade physics bodies, with their hands running at a random direction. During next step we’ll see how to do it, meanwhile you can 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.