Build a HTML5 game like “Magick” iPad game using Phaser and ARCADE physics – code updated and new features added

Read all posts about "" game
Magick is a fun iPad game I played some years ago, and I also wrote a first tutorial about it. I don’t think you can find the game anymore, or at least I can’t open the original link, but I wanted to update the source code adding more room for customization and the capability to change player direction by tapping on it.
The player walks and climbs on his own, you can only click anywhere on an empty spot to summon a crate, but you can summon only one crate at once. Moreover, in this version you can also change player direction if you tap within a certain distance from it. Will you be able to reach the upper platform on the left? I really like the gameplay, and since at the moment the game seems to have disappeared, it would be nice to create something which remembers it. The source code is less than 100 lines, still uncommented but you can find some comments in the first version of the game. I will add comments once the game has more features, like enemies and traps.
var game;
var gameOptions = {
    playerSpeed: 120,
    playerJumpSpeed: {
        x: 30,
        y: -100
    },
    tileSize: 32,
    changeDirectionRange: 32,
    playerGravity: 400
}
window.onload = function() {
    game = new Phaser.Game(800, 320, Phaser.CANVAS);
    game.state.add("PlayGame", playGame, true);
}
var playGame = function(){}
playGame.prototype = {
    preload: function(){
        game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
		game.load.tilemap("map", "map.json", null, Phaser.Tilemap.TILED_JSON);
		game.load.image("rock", "rock.png");
        game.load.image("block", "block.png");
        game.load.image("player", "player.png");
	},
	create: function(){
        this.tilePoint = null;
        game.physics.startSystem(Phaser.Physics.ARCADE);
		this.map = game.add.tilemap("map");
        this.map.addTilesetImage("rock");
		this.map.addTilesetImage("block");
        this.map.setCollisionBetween(1,2);
        this.levelLayer = this.map.createLayer("myLevel");
        this.player = game.add.sprite(48, 226, "player");
		this.player.anchor.set(0.5);
        this.player.isJumping = false;
        this.player.direction = 1;
        game.physics.enable(this.player, Phaser.Physics.ARCADE);
        this.player.body.gravity.y = gameOptions.playerGravity;
        game.input.onDown.add(this.addBlock, this);
	},
	update:function(){
        this.player.body.velocity.x = 0;
        game.physics.arcade.collide(this.player, this.levelLayer, this.movePlayer, null, this);
	},
    movePlayer: function(){
        if(this.player.body.blocked.down){
            this.player.body.velocity.x = gameOptions.playerSpeed * this.player.direction;
            this.player.isJumping = false;
        }
        if(this.player.body.blocked.right && this.player.direction == 1){
            if((!this.map.getTileWorldXY(this.player.x + gameOptions.tileSize, this.player.y - gameOptions.tileSize, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer) && !this.map.getTileWorldXY(this.player.x, this.player.y - gameOptions.tileSize, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer)) || this.player.isJumping){
				this.jump();
			}
            else{
                this.player.direction *= -1;
            }
        }
        if(this.player.body.blocked.left && this.player.direction == -1){
			if((!this.map.getTileWorldXY(this.player.x - gameOptions.tileSize, this.player.y - gameOptions.tileSize, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer) && !this.map.getTileWorldXY(this.player.x, this.player.y - gameOptions.tileSize, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer)) || this.player.isJumping){
				this.jump();
			}
			else{
				this.player.direction *= -1;
			}
		}
    },
    addBlock: function(e){
        var distanceX = e.x - this.player.x;
        var distanceY = e.y - this.player.y;
        if ((distanceX * distanceX + distanceY * distanceY) < gameOptions.changeDirectionRange * gameOptions.changeDirectionRange){
            this.player.direction *= -1;
        }
        else{
            if(!this.map.getTileWorldXY(e.x, e.y, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer)){
                if(this.tilePoint){
                    this.map.removeTileWorldXY(this.tilePoint.x, this.tilePoint.y, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer);
                }
                this.map.putTileWorldXY(2, e.x, e.y, gameOptions.tileSize, gameOptions.tileSize, this.levelLayer);
                this.tilePoint = new Phaser.Point(e.x, e.y);
            }
        }
    },
    jump: function(){
        this.player.body.velocity.y = gameOptions.playerJumpSpeed.y;
        this.player.body.velocity.x = gameOptions.playerJumpSpeed.x * this.player.direction;
        this.player.isJumping = true;
    }
}
One of the biggest reasons you should build HMTL5 games is probably JavaScript will never die, so download the source code and play with it.

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

215 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
// Stairs
// 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