Create a HTML5 game like iOS hit “CLOCKS – The Game” using Phaser and Arcade physics

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers