The Game Class

The Game class is the entry point of the engine. It creates the game instance, holds its scenes and dependencies, and controls the game loop.

Creating a game instance

A Game is created from a configuration object. The container element, width, and height are required:

import { Game } from "angry-pixel";

const game = new Game({
    containerNode: document.getElementById("app")!,
    width: 1920,
    height: 1080,
});

The engine creates a <canvas> element inside containerNode with the given width and height.

Configuration

The configuration object (GameConfig) accepts the following options:

OptionTypeRequiredDefaultDescription
containerNodeHTMLElementYesHTML element where the game canvas is created.
widthnumberYesGame width in pixels.
heightnumberYesGame height in pixels.
canvasColorstringNo"#000000"Background color of the canvas.
physicsFrameratenumberNo180Framerate for physics execution. Allowed values: 60, 120, 180, 240. A higher framerate makes physics more accurate but consumes more resources.
headlessbooleanNofalseDisables rendering, audio, and input. Intended for game server development.
debugobjectNoDebug visualization options (see below).
collisionsobjectNoCollision detection options (see below).
dependencies[DependencyName, any][]NoExternal dependencies accessible through dependency injection.

Debug options

The debug object enables on-screen debugging aids:

OptionTypeDefaultDescription
collidersbooleanfalseDraw colliders.
mousePositionbooleanfalseShow the mouse position.
textRendererBoundingBoxesbooleanfalseDraw text renderer bounding boxes.
collidersColorstring"#00FF00"Color of the colliders.
textBoxColorstring"#0000FF"Color of the text bounding box.
textColorstring"#00FF00"Color of the debug text.
textPosition"top-left" | "top-right" | "bottom-left" | "bottom-right""bottom-left"Position of the debug text.

Collision options

The collisions object configures collision detection:

OptionTypeDefaultDescription
collisionMethodCollisionMethodsCollisionMethods.SATDetection method: CollisionMethods.SAT or CollisionMethods.AABB.
collisionBroadPhaseMethodBroadPhaseMethodsBroadPhaseMethods.SpatialGridBroad phase method: BroadPhaseMethods.SpatialGrid or BroadPhaseMethods.QuadTree.
collisionMatrixCollisionMatrixPairs of collision layers that are allowed to collide.
import { Game, CollisionMethods, BroadPhaseMethods } from "angry-pixel";

const game = new Game({
    containerNode: document.getElementById("app")!,
    width: 1920,
    height: 1080,
    debug: {
        colliders: true,
        mousePosition: false,
        textRendererBoundingBoxes: false,
    },
    physicsFramerate: 180,
    collisions: {
        collisionMethod: CollisionMethods.SAT,
        collisionBroadPhaseMethod: BroadPhaseMethods.SpatialGrid,
        collisionMatrix: [
            ["player", "enemy"],
            ["player", "wall"],
        ],
    },
});

Adding scenes

Scenes are registered with addScene. Pass the scene class and a name. The third argument marks it as the opening scene — the one loaded when the game starts:

import { MainScene } from "./scene/MainScene";

game.addScene(MainScene, "MainScene", true);

A game can register multiple scenes, but only one is marked as the opening scene.

Starting and stopping the game

start runs the game loop; stop halts it. The running property reports whether the loop is active.

game.start();

if (game.running) {
    game.stop();
}

Registering dependencies

Custom dependencies can be made available through dependency injection. Register a class with addDependencyType, or an existing instance with addDependencyInstance:

game.addDependencyType(MyService);
game.addDependencyInstance(myConfig, "MyConfig");

Dependencies can also be provided up front through the dependencies option in the configuration object.

Headless mode

Setting headless: true disables rendering, audio, and input systems. This mode is intended for running the engine on a server, where no canvas or user input is available.