HTML5 prototype of “Don’t Touch The Spikes” built with Phaser 3 and Matter physics

Read all posts about "" game
Ketchapp Games is really famous for publishing simple but fun games you can play in portrait mode with just your thumb while waiting for the train, the so-called hyper casual games. Recently I played Don’t Touch The Spikes and it’s the classic Ketchapp game: easy, fun and interesting to deconstruct. So I am going to show you this first prototype built with Phaser 3 and Matter physics. I am sure it could have been easily done with no physics engines at all like my circular endless runner prototype, but come on, let’s see something new. I have to warn you: defining custom collision shapes from a path is a bit of a pain, I had to do a lot of trial and error in order to create triangle collision work well. It would have been simpler if I used equilateral triangles, however. Ok, let me show you something:
Click or tap to make the ball float in the air. Hit a spike or fly off the stage, and the game will restart. The tiny purple lines are represent the debug draw, showing you the actual matter body shapes. The source code is still uncommented as I am fighting with triangles and friction but at least will be useful to show you how to set up a Matter physics world, create circles boxes and custom shapes, apply forces and check for collisions:
var game;
var gameOptions = {
}
window.onload = function() {
    var gameConfig = {
       type: Phaser.AUTO,
       width: 480,
       height: 640,
       backgroundColor: 0x000000,
       scene: playGame,
       physics: {
           default: "matter",
           matter: {
               debug: true
           }
        }
    }
    game = new Phaser.Game(gameConfig);
    window.focus();
    resize();
    window.addEventListener("resize", resize, false);
}
class playGame extends Phaser.Scene{
    constructor(){
        super("PlayGame");
    }
    preload(){
        this.load.image("wall", "wall.png");
        this.load.image("ball", "ball.png");
        this.load.image("leftspike", "leftspike.png");
        this.load.image("rightspike", "rightspike.png");
    }
    create(){
        this.matter.add.image(10, game.config.height / 2, "wall", null, {
            isStatic: true,
            label: "leftwall"
        });
        this.matter.add.image(game.config.width - 10, game.config.height / 2, "wall", null, {
            isStatic: true,
            label: "rightwall"
        });
        var leftSpikePath = this.matter.world.fromPath("-45 -35 45 0 -45 35");
        var rightSpikePath = this.matter.world.fromPath("-45 0 45 35 45 -35");
        this.leftSpike = this.matter.add.image(50, Phaser.Math.Between(50, game.config.height - 50), "leftspike", null, {
            isStatic: true,
            shape: {
                type: "fromVerts",
                verts: leftSpikePath
            },
            label: "spike"
        });
        this.rightSpike = this.matter.add.image(game.config.width - 50, Phaser.Math.Between(50, game.config.height - 50), "rightspike", null, {
            isStatic: true,
            shape: {
                type: "fromVerts",
                verts: rightSpikePath
            },
            label: "spike"
        });
        this.ball = this.matter.add.image(game.config.width / 2, game.config.height / 2, "ball");
        this.ball.setCircle();
        this.ball.setVelocity(7, 0);
        this.ball.setBounce(1);
        this.ball.setFriction(0);
        this.input.on("pointerdown", this.jump, this);
        this.matter.world.on("collisionstart", function (e, b1, b2) {
            if(b1.label == "spike" || b2.label == "spike"){
                this.scene.start("PlayGame");
            }
            if(b1.label == "leftwall" || b2.label == "leftwall"){
                this.rightSpike.y = Phaser.Math.Between(50, game.config.height - 50);
                this.ball.setVelocity(7, 0);
            }
            if(b1.label == "rightwall" || b2.label == "rightwall"){
                this.leftSpike.y = Phaser.Math.Between(50, game.config.height - 50);
                this.ball.setVelocity(-7, 0);
            }
        }, this);
    }
    jump(){
        this.ball.setVelocityY(-10);
    }
    update(){
        if(this.ball.y &gt; game.config.height || this.ball.y < 0){
            this.scene.start("PlayGame");
        }
    }
};
function resize() {
    var canvas = document.querySelector("canvas");
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;
    var windowRatio = windowWidth / windowHeight;
    var gameRatio = game.config.width / game.config.height;
    if(windowRatio < gameRatio){
        canvas.style.width = windowWidth + "px";
        canvas.style.height = (windowWidth / gameRatio) + "px";
    }
    else{
        canvas.style.width = (windowHeight * gameRatio) + "px";
        canvas.style.height = windowHeight + "px";
    }
}
Next time, some more optimization and feature, 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