HTML5 prototype of iOS “Zhed” puzzle game with 10 playable levels made with Phaser

Read all posts about "" game
If you enjoyed the “Zhed” puzzle game prototype made with Construct 3, and you also are a Phaser enthusiast, you’ll probably like this version I made using the latest version pf Phaser CE, 2.7.8 I tried to write the code following the same line of concept I used when I made the Construct 3 version. Obviously coding from scratch gives you more opportunities to optimize your work, but I wanted to keep the code similar to the Constrcut 3 version for you to compare them. I will also write some kind of “Phaser Vs Construct” from a programmer point of view, once I made a couple of games. Now, this is the Phaser version of the game:
Tap a square and select a direction (upwards, downwards, to the left and to the right). See how the square reacts when they intersect with each other, and build a path to fill the goal square. When you finished, the next level will show up! If you get stuck, press “Restart” button. If you have a mobile device, play the game from this link. About the source code, here it is, uncommented at the moment but I will add comments as soon as I add some more levels.
var game;
var gameOptions = {
    tileSize: 100,
    gameWidth: 800,
    gameHeight: 1000
}
var levels = [[[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,0,1,10,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,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,0,0,0,0,0,0,0],[0,0,0,2,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,10,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,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],[0,0,1,1,0,10,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]],[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,10,0,0,0],[0,0,0,0,0,0,0,0],[0,0,2,0,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]],[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,2,0,0,10,0],[0,0,0,0,0,0,0,0],[0,2,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]],[[0,0,0,0,0,0,0,0],[0,0,10,0,0,0,3,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,2,0,0,0,0,0],[0,0,0,0,0,3,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,0],[0,0,0,0,0,0,0,0],[0,0,0,1,0,0,0,0],[0,10,0,0,0,0,1,0],[0,0,0,0,0,1,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]],[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,1,1,0,0],[0,1,0,0,0,0,10,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],[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,0,0,1,2,0,0],[0,0,0,1,0,0,0,0],[0,0,1,0,0,0,0,0],[0,0,0,0,0,0,3,0],[0,0,10,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,0,0,0],[0,0,0,0,1,2,0,0],[0,0,0,0,0,0,0,0],[0,0,2,0,0,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,10,0,0]]]

window.onload = function() {
    game = new Phaser.Game(gameOptions.gameWidth, gameOptions.gameHeight);
    game.state.add("PreloadGame", preloadGame);
    game.state.add("PlayGame", playGame);
    game.state.start("PreloadGame");
}
var preloadGame = function(game){}
preloadGame.prototype = {
    preload: function(){
        game.stage.backgroundColor = 0x2c3e50;
        game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
        game.stage.disableVisibilityChange = true;
        game.load.spritesheet("tiles", "tiles.png", gameOptions.tileSize, gameOptions.tileSize);
    },
    create: function(){
        game.state.start("PlayGame", true, false, 0);
    }
}
var playGame = function(game){}
playGame.prototype = {
    init: function(currentLevel){
            this.currentLevel = currentLevel;
    },
    create: function(){
        this.undoArray = [];
        this.drawLevel();
        game.input.onTap.add(this.handleTap, this);
    },
    drawLevel: function(){
        this.localLevel = [];
        this.tileGroup = game.add.group();
        this.stepsGroup = game.add.group();
        for(var i = 0; i < levels[this.currentLevel].length; i++){
            this.localLevel[i] = [];
            for(var j = 0; j < levels[this.currentLevel][i].length; j++){
                var tile = game.add.sprite(j * gameOptions.tileSize, i * gameOptions.tileSize, "tiles", levels[this.currentLevel][i][j]);
                if(levels[this.currentLevel][i][j] == 0){
                    tile.alpha = 0.6 + game.rnd.realInRange(0, 0.3);
                }
                this.tileGroup.add(tile);
                this.localLevel[i][j] = {
                    sprite: tile,
                    value: levels[this.currentLevel][i][j]
                }
                var step = game.add.sprite(j * gameOptions.tileSize, i * gameOptions.tileSize, "tiles", 11);
                step.row = i;
                step.col = j;
                step.visible = false;
                this.stepsGroup.add(step);
            }
        }
        var restartButton = game.add.button(game.width / 2 - gameOptions.tileSize / 2, game.height - gameOptions.tileSize * 1.5, "tiles", function(){
            game.state.start("PlayGame", true, false, this.currentLevel);
        }, this, 13, 13);
    },
    handleTap: function(pointer, doubleTap){
        var turnTiles = false;
        var tileTapRow = Math.floor(pointer.y / gameOptions.tileSize);
        var tileTapCol = Math.floor(pointer.x / gameOptions.tileSize);
        if(tileTapRow < 0 || tileTapCol < 0 || tileTapRow &gt;= this.localLevel.length || tileTapCol &gt;= this.localLevel[tileTapRow].length){
            return;
        }
        var tileValue = this.localLevel[tileTapRow][tileTapCol].value;
        if(tileValue == 11 || tileValue == 21){
            turnTiles = true;
            this.localLevel[this.selectedRow][this.selectedCol].sprite.frame = 12;
            this.localLevel[this.selectedRow][this.selectedCol].value = 11;
            var deltaX = game.math.clamp(tileTapCol - this.selectedCol, -1, 1);
            var deltaY = game.math.clamp(tileTapRow - this.selectedRow, -1, 1);
        }
        this.stepsGroup.forEach(function(item) {
            if(item.visible){
                if(!turnTiles){
                    this.localLevel[item.row][item.col].value -= 11;
                }
                else{
                    if((deltaX &gt; 0 &amp;&amp; item.col &gt; this.selectedCol) || (deltaX < 0 &amp;&amp; item.col < this.selectedCol) || (deltaY &gt; 0 &amp;&amp; item.row &gt; this.selectedRow) || (deltaY < 0 &amp;&amp; item.row < this.selectedRow)){
                        this.localLevel[item.row][item.col].sprite.frame = 12;
                        if(this.localLevel[item.row][item.col].value == 21){
                            game.time.events.add(Phaser.Timer.SECOND, function(){
                                    game.state.start("PlayGame", true, false, (this.currentLevel + 1) % 10);
                            }, this);
                        }
                    }
                    else{
                        this.localLevel[item.row][item.col].value -= 11;
                    }
                }
                item.visible = false;
            }
        }, this);
        if(tileValue &gt; 0 &amp;&amp; tileValue < 10){
            this.showSteps(tileTapRow, tileTapCol, tileValue, 0, 1);
            this.showSteps(tileTapRow, tileTapCol, tileValue, 0, -1);
            this.showSteps(tileTapRow, tileTapCol, tileValue, 1, 0);
            this.showSteps(tileTapRow, tileTapCol, tileValue, -1, 0);
            this.selectedRow = tileTapRow;
            this.selectedCol = tileTapCol;
        }
    },
    showSteps: function(row, col, steps, deltaRow, deltaCol){
        var stepsMade = 0;
        while(stepsMade < steps){
            while(this.localLevel[row][col].value != 0 &amp;&amp; this.localLevel[row][col].value != 10){
                row += deltaRow;
                col += deltaCol;
                if(row < 0 || col < 0 || row &gt;= this.localLevel.length || col &gt;= this.localLevel[row].length){
                    return;
                }
            }
            this.stepsGroup.getChildAt(row * this.localLevel[col].length + col).visible = true;
            this.localLevel[row][col].value += 11;
            stepsMade ++;
            row += deltaRow;
            col += deltaCol;
            if(row < 0 || col < 0 || row &gt;= this.localLevel.length || col &gt;= this.localLevel[row].length){
                break;
            }
        }
    }
}
That’s around 100 lines of code, and the engine is done and ready to host your levels. 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