Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Serious Scramblers game, Game development, HTML5, Javascript and TypeScript.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.