Skip to content

Build Your First Game ​

Start with the template setup. This walkthrough takes you from the spinning gear to a character you can move, then to a production build.

Move a square ​

Replace src/game.ts with the class below. It uses the built-in PIXEL texture, so no downloaded art or settings file is needed.

ts
import { StartBare, Scene, GameObject, Color } from 'bare-engine';

export class FirstGame extends Scene
{
    player: GameObject;

    OnStart()
    {
        this.player = this.AddGameObject('PIXEL')
            .SetScale(32)
            .SetColor(Color.Orange)
            .SetPosition(bare.width / 2, bare.height / 2)
            .CenterPivot();
    }

    OnUpdate(deltaTime: number)
    {
        const direction = Number(bare.keyboard.IsRightDown()) - Number(bare.keyboard.IsLeftDown());
        this.player.x += direction * 200 * deltaTime;
    }
}

StartBare(FirstGame);

You should see an orange square in the center. Click the game, then hold the left or right arrow key (A/D also work). It moves at 200 game units per second; multiplying by deltaTime makes the speed independent of frame rate.

Give it an image ​

Put a PNG named player.png in texturePackImages/. The dev server rebuilds the atlas. Change AddGameObject('PIXEL') to AddGameObject('player'), remove .SetScale(32) to use the image's own dimensions, and remove .SetColor(Color.Orange) to keep its original colors.

Save the file and you should see your image where the square was. Asset names omit the file extension. See Assets for streaming images and generated names.

Build and run the result ​

In the template project:

sh
npm run build
npx vite preview --config vite.config.cjs

Open the preview URL printed in the terminal. Check that the image appears and movement works there too. The result is in dist/; do not open its HTML directly from disk. See Building & Publishing.

Continue building ​

GoalRead next
Reuse movement on several objectsComponents
Define a player and enemies as dataPrefabs
Place them in a mapLevels & Layers, then Level Editor
Add walls, gravity, or collision triggersPhysics
Add a score and restart cleanlyText & UI, then Lifecycle & Cleanup

The square's source above is also compiled and run by the engine's documentation example tests.

Bare Engine is open source under the MIT license.