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
andheight
). - 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:
Property | Type | Description |
---|---|---|
containerNode |
HTMLElement | HTML node where the game will be rendered. Required. |
width |
number | Game width in pixels. Required. |
height |
number | Game height in pixels. Required. |
debug |
object (optional) | Debug options. Allows showing colliders, mouse position, and other data during execution. |
debug.colliders |
boolean | Show colliders. |
debug.mousePosition |
boolean | Show mouse position. |
debug.collidersColor |
string | Color of the colliders. Default: #00FF00 (green). |
debug.textColor |
string | Color of debug texts. Default: #00FF00 . |
debug.textPosition |
string | Position of debug text: "top-left" , "top-right" , "bottom-left" or "bottom-right" . |
canvasColor |
string | Background color of the canvas. Default: #000000 (black). |
physicsFramerate |
number | Physics execution framerate. Allowed values: 60 , 120 , 180 or 240 . Default: 180 . |
headless |
boolean | Enables headless mode. Disables input and rendering (ideal for game servers). |
dependencies |
Array | List of external dependencies to be injected through dependency injection. |
collisions |
object (optional) | Collision configuration options. |
collisions.collisionMethod |
CollisionMethods | Collision detection method: SAT or ABB . Default: SAT . |
collisions.collisionMatrix |
Array | Matrix that defines which layers can collide with each other. |
collisions.collisionBroadPhaseMethod |
BroadPhaseMethods | Broad 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
: iftrue
, 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 theinjectable
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.