Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Horizontal Endless Runner game, Game development, HTML5, Javascript and Phaser.

When we deal with endless runner games, where the player runs along a terrain, we can see two main concepts applied in the generation of the terrain:

The first concept wants you to create various pre-built prefabs and just randomly merge them together to create an endless environment. This is the case of Spelunky, which although is not a horizontal endless runner it’s the perfect example of what I mean.

The second concept, the one I prefer, consist in generating an endless environment from scratch, with no prefabs.

Let’s jump straight to the point and see what I built:

Do you like it? We have a series of randomly generated slopes painted over graphic objects scrolling from right to left, and reusing them once they leave the stage to the left side.

Slopes vary in width and height and are generated by a series of points interpolated by the cosine function, which is the simplest interpolation you can use to create slopes without using straight lines.

You can achieve better results by using cubic interpolation or Bezier curves but it’s not in the scope of this tutorial.

Before e talk about better ways to generate terrains, we need to make the terrain walkable by a ball/a cart/whatever, and we will require physics in order to do it.

At the moment, enjoy the simplest randomly generated terrain ever, with room for customization:

var game;

var gameOptions = {
    startTerrainHeight: 0.5,
    amplitude: 200,
    slopeLength: [100, 350],
    slicesAmount: 2,
    slopesPerSlice: 5,
    terrainSpeed: 200
}
window.onload = function() {
    let gameConfig = {
        type: Phaser.AUTO,
        backgroundColor: 0x75d5e3,
        scale: {
            mode: Phaser.Scale.FIT,
            autoCenter: Phaser.Scale.CENTER_BOTH,
            parent: "thegame",
            width: 750,
            height: 1334
        },
        scene: playGame
    }
    game = new Phaser.Game(gameConfig);
    window.focus();
}
class playGame extends Phaser.Scene{
    constructor(){
        super("PlayGame");
    }
    create(){
        this.slopeGraphics = [];
        this.sliceStart = new Phaser.Math.Vector2(0, Math.random());
        for(let i = 0; i < gameOptions.slicesAmount; i++){
            this.slopeGraphics[i] = this.add.graphics();
            this.sliceStart = this.drawTerrain(this.slopeGraphics[i], this.sliceStart);
        }
    }
    drawTerrain(graphics, sliceStart){
        let slopePoints = [];
        let slopes = 0;
        let slopeStart = 0;
        let slopeStartHeight = sliceStart.y;
        let currentSlopeLength = Phaser.Math.Between(gameOptions.slopeLength[0], gameOptions.slopeLength[1]);
        let slopeEnd = slopeStart + currentSlopeLength;
        let slopeEndHeight = Math.random();
        let currentPoint = 0;
        while(slopes < gameOptions.slopesPerSlice){
            if(currentPoint == slopeEnd){
                slopes ++;
                slopeStartHeight = slopeEndHeight;
                slopeEndHeight = Math.random();
                var y = game.config.height * gameOptions.startTerrainHeight + slopeStartHeight * gameOptions.amplitude;
                slopeStart = currentPoint;
                currentSlopeLength = Phaser.Math.Between(gameOptions.slopeLength[0], gameOptions.slopeLength[1]);
                slopeEnd += currentSlopeLength;
            }
            else{
                var y = (game.config.height * gameOptions.startTerrainHeight) + this.interpolate(slopeStartHeight, slopeEndHeight, (currentPoint - slopeStart) / (slopeEnd - slopeStart)) * gameOptions.amplitude;
            }
            slopePoints.push(new Phaser.Math.Vector2(currentPoint, y))
            currentPoint ++;
        }
        graphics.x = sliceStart.x;
        graphics.clear();
        graphics.moveTo(0, game.config.height);
        graphics.fillStyle(0x654b35);
        graphics.beginPath();
        slopePoints.forEach(function(point){
            graphics.lineTo(point.x, point.y);
        })
        graphics.lineTo(currentPoint, game.config.height);
        graphics.lineTo(0, game.config.height);
        graphics.closePath();
        graphics.fillPath();
        graphics.lineStyle(16, 0x6b9b1e);
        graphics.beginPath();
        slopePoints.forEach(function(point){
            graphics.lineTo(point.x, point.y);
        })
        graphics.strokePath();
        graphics.width = (currentPoint - 1) * -1;
        return new Phaser.Math.Vector2(graphics.x + currentPoint - 1, slopeStartHeight);
    }
    interpolate(vFrom, vTo, delta){
        let interpolation = (1 - Math.cos(delta * Math.PI)) * 0.5;
        return vFrom * (1 - interpolation) + vTo * interpolation;
    }
    update(t, dt){
        let offset = dt / 1000 * gameOptions.terrainSpeed;
        this.sliceStart.x -= offset;
        this.slopeGraphics.forEach(function(item){
            item.x -= offset;
            if(item.x < item.width){
                this.sliceStart = this.drawTerrain(item, this.sliceStart)
            }
        }.bind(this))
    }
}

The code is not commented yet, and not optimized. You may experience some lag if you turn this stuff into a landscape game, due to the large amount of points you need to generate.

Also, no physics makes this script quite useless as a game example, but this is the starting point to create a fully featured physics game with an endless scrolling environment, randomly generated.

Download the source code and wait for next step. The keyword will be: discretize.

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