HTML5 “Serious Scrambers” prototype built with Kaboom, using object pooling. Written in TypeScript

Read all posts about "" game

Today I am publishing an experiment made with Kaboom. The website says Kaboom is a Javascript game programming library that helps you make games fast and fun, so let’s try building some stuff.

It’s a Serious Scramblers prototype I already built with Phaser finding some issues on low end mobile devices when using Arcade physics.

Anyway, let’s jump straight to the point and see the prototype, then we’ll see the pros and cons of the library:

Focus on the canvas, then use LEFT and RIGHT arrows to move. Try not to fall down and not to disappear from the top of the canvas.

As you will see below, the commented code is pretty straightforward and it was quite easy to build this prototype, but there are a couple of issues:

1 – Scaling needs to be improved when using letterbox, because it displays scroll bars. This may be due to a canvas bigger than needed.

2 – There isn’t a proper physics engine: forget Box2D or Matter, we only have a simplified version of Phaser’s Arcade physics with no immovable bodies, so if two moving bodies collide, they will stop moving. This was a problem when dealing with moving platforms.

3 – Children management does not seem to work at the moment

4 – TypeScript definition throw some errors if you don’t comment some lines with // @ts-expect-error

Despite all, here is the commented source code in only three files: one HTML file, one CSS file and one TypeScript file:

index.html

The web page which hosts the game. main.js contains the game itself and should be placed right before body closes.

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>   
        <script src="main.js"></script> 
    </body>
</html>

style.css

The cascading style sheets of the main web page.

* {
    padding : 0;
    margin : 0;
}

body {
    background-color : black;    
}

canvas {
    touch-action : none;
    -ms-touch-action : none;
}

main.ts

This is where the game is created, with all Kaboom stuff.

// game options to set player and platform speed
const HEROSPEED : number = 320;
const PLATFORMSPEED : number = 200;

// import kaboom
import kaboom from 'kaboom';

// create a kaboom game
kaboom({
    width : 750,
    height : 1334,
    stretch : true,
    letterbox : true,
    background : [0, 0, 255]
});

// load sprites
loadSprite('hero', 'assets/sprites/hero.png');
loadSprite('platform', 'assets/sprites/platform.png');

// this is how we create a scene
scene('game', () => {

    // at the moment we are not playing
    let isPlaying : boolean = false;

    // array containing all platforms
    let platforms : any[] = [];

    // add the starting platform
    platforms.push(add([

        // sprite used
        sprite('platform'),

        // position
        pos(width() / 2, 180),

        // platform has a collider
        area(),

        // objects can't pass though it
        solid(),

        // set origin (and avoid TypeScript errors)
        // @ts-expect-error
        origin('center'),

        // tag
        'platform'
    ]));
    
    // add the hero
    const hero : any = add([

        // sprite used
        sprite('hero'),

        // position
        pos(width() / 2, 40),

        // hero has a collider
        area(),

        // hero has a body, affected by gravity
        body(),

        // set origin (and avoid TypeScript errors)
        // @ts-expect-error
        origin('center'),

        // tag
        'hero'
    ])
    
    // add 10 more platforms
    for (let i : number = 0; i < 10; i ++) {
        platforms.push(add([
            sprite('platform'),
            // @ts-expect-error
            pos(randi(100, width() - 100), getLowestPlatform(platforms) + randi(100, 200)),
            area(),
            solid(),
            // @ts-expect-error
            origin('center'),
            'platform'
        ]))
    }
    
    // when LEFT key is pressed...
    onKeyDown('left', () => {

        // move hero to the left by HEROSPEED (pixels per second?)
        hero.move(-HEROSPEED, 0);

        // now we are playing
        isPlaying = true;
    })
    
    // when RIGHT key is pressed...
    onKeyDown('right', () => {

        // move hero to the right by HEROSPEED (pixels per second?)
        hero.move(HEROSPEED, 0);

        // now we are playing
        isPlaying = true
    })

    // method to be executed at each frame for the hero
    onUpdate('hero', (hero : any) => {

        // if the hero is outside the canvas...
        if (hero.pos.y > height() + 200 || hero.pos.y < 0) {

            // restart the game
            go('game');
        }    
    })

    // method to be executed at each frame for the platform
    onUpdate('platform', (platform : any) => {

        // are we playing?
        if (isPlaying) {

            // move the platform according to velocity and delta time
            platform.moveTo(platform.pos.x, platform.pos.y - PLATFORMSPEED * dt())
            
            // if platform leaves the canvas from the top...
            if (platform.pos.y < -10) {

                // reposition the platform (and avoid an error)
                // @ts-expect-error
                platform.moveTo(randi(100, width() - 100), getLowestPlatform(platforms) + randi(100, 200))
            }   
        }
    })

    // function to get the lowest platform
    function getLowestPlatform(platforms : any[]) : number {
        let y : number = 0;
        platforms.forEach((platform : any) => {
            y = Math.max(y, platform.pos.y)
        })
        return y;
    }
})

// start game scene
go('game');

It’s early to say if Kaboom can be used to build any kind of commercial HTML5 game, but at the moment I managed to build this simple prototype, net time will try to improve it. Meanwhile 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
// 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