The Game class

The Game class is the main entry point for creating and managing a game instance in Angry Pixel Engine.
It acts as the central coordinator that manages:

  • Scenes.
  • Entities.
  • Components.
  • Managers (rendering, physics, input, etc.).

It also provides methods to configure the game, add scenes and dependencies, and control the execution cycle (game loop).

Constructor

new Game(config: GameConfig)

The constructor receives a GameConfig object where you can define:

  • The HTML node where the game will be rendered (containerNode).
  • Dimensions (width and height).
  • Optional: physics settings, colors, collision methods, etc.

Basic example

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

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

Advanced example

const game = new Game({
    containerNode: document.getElementById("app"),
    width: 1920,
    height: 1080,
    debugEnabled: false,
    canvasColor: "#000000",
    physicsFramerate: 180,
    collisions: {
        collisionMatrix: [
            ["layer1", "layer2"],
            ["layer1", "layer3"],
        ],
        collisionMethod: CollisionMethods.SAT,
        collisionBroadPhaseMethod: BroadPhaseMethods.SpartialGrid,
    },
});

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

GameConfig details

The GameConfig object allows you to customize how the game initializes.
Here are all the available properties:

PropertyTypeDescription
containerNodeHTMLElementHTML node where the game will be rendered. Required.
widthnumberGame width in pixels. Required.
heightnumberGame height in pixels. Required.
debugobject (optional)Debug options. Allows showing colliders, mouse position, and other data during execution.
debug.collidersbooleanShow colliders.
debug.mousePositionbooleanShow mouse position.
debug.collidersColorstringColor of the colliders. Default: #00FF00 (green).
debug.textColorstringColor of debug texts. Default: #00FF00.
debug.textPositionstringPosition of debug text: "top-left", "top-right", "bottom-left" or "bottom-right".
canvasColorstringBackground color of the canvas. Default: #000000 (black).
physicsFrameratenumberPhysics execution framerate. Allowed values: 60, 120, 180 or 240. Default: 180.
headlessbooleanEnables headless mode. Disables input and rendering (ideal for game servers).
dependenciesArrayList of external dependencies to be injected through dependency injection.
collisionsobject (optional)Collision configuration options.
collisions.collisionMethodCollisionMethodsCollision detection method: SAT or ABB. Default: SAT.
collisions.collisionMatrixArrayMatrix that defines which layers can collide with each other.
collisions.collisionBroadPhaseMethodBroadPhaseMethodsBroad phase method: QuadTree or SpartialGrid. Default: SpartialGrid.

Note: If some options are not specified, the engine will use recommended default values.

Methods

addScene(sceneType, name, openingScene = false)

Adds a new scene to the game.

  • sceneType: the scene class.
  • name: name to identify the scene.
  • openingScene: if true, this will be the opening scene when the game starts.

Example:

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

addDependencyType(dependencyType, name?)

Adds a class to be used as a dependency.

  • dependencyType: the dependency class.
  • name: the name of the dependency (optional if the class uses the injectable decorator).

Example:

game.addDependencyType(MyCustomService);

addDependencyInstance(dependencyInstance, name)

Adds a specific instance to be used as a dependency.

  • dependencyInstance: the instance to use.
  • name: the name of the dependency.

Example:

game.addDependencyInstance(myDatabaseInstance, "Database");

start()

Starts the game and begins the update cycle (game loop).

game.start();

stop()

Stops the game’s update cycle.

game.stop();

Properties

running

Returns true if the game is currently running.

if (game.running) {
    console.log("The game is running");
}

Summary

The Game class is responsible for:

✅ Initializing configuration and dependencies.
✅ Managing scenes.
✅ Controlling the game’s lifecycle (start and stop).
✅ Providing a central point to customize the engine’s behavior.

Note: Internally, Game uses dependency injection to initialize and connect all game systems.