dino-ge

1. Build a first scene

Core guide

Dino GE runs a 2D game in a browser. The engine creates a canvas (the HTML element it draws into) and runs the game loop. The loop reads input, updates the game and draws the result. A scene holds the objects for one part of the game, such as a level or a menu. Start with one scene and a circle to represent the explorer.

Run the demo · View the source

The explorer moves across the canvas. Select Pause to freeze it, then Resume to continue. Restart replaces the engine and starts the explorer at the left again.

Create the page

Create a folder and save this as index.html. The import map tells the browser where to find the module named dino-ge. Load it before the module script that imports the engine.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My first Dino GE game</title>
  <script type="importmap">
    {
      "imports": {
        "dino-ge": "https://cdn.jsdelivr.net/npm/dino-ge@1.16.0/dist/index.js"
      }
    }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <div id="game" style="width: 360px; max-width: 100%; height: 220px"></div>
  <button id="pause">Pause</button>
  <button id="restart">Restart</button>
  <p id="status" role="status"></p>
</body>
</html>

Create and activate the scene

Save the following as main.js beside index.html. The start() function creates a fresh Clearing, starts the engine, then activates that same scene in the engine’s onLoad() callback. Activation calls Clearing.onLoad(), which creates and adds the circle. The fixed update can now move it through clearing.explorer.

import { Circle, Engine, Scene, Vector2 } from 'dino-ge';

class Clearing extends Scene {
  onLoad() {
    this.explorer = new Circle({ position: new Vector2(24, 80), radius: 12, colour: '#ffc46f' });
    this.add(this.explorer);
  }
}

const status = document.querySelector('#status');
const pause = document.querySelector('#pause');
let game;

function start() {
  game?.terminate();
  const clearing = new Clearing();
  game = new Engine({
    onLoad() {
      Engine.currentScene = clearing;
    },
    fixedUpdate() {
      const position = clearing.explorer.transform.position;
      position.x = position.x + 2 >= game.width - 20 ? 24 : position.x + 2;
    },
  }, { containerId: 'game', width: '100%', height: '100%', backgroundColour: '#183035' });
  Engine.paused = false;
  pause.textContent = 'Pause';
  status.textContent = 'Running';
}

pause.addEventListener('click', () => {
  Engine.paused = !Engine.paused;
  pause.textContent = Engine.paused ? 'Resume' : 'Pause';
  status.textContent = Engine.paused ? 'Paused' : 'Running';
});
document.querySelector('#restart').addEventListener('click', start);
start();

The containerId: 'game' option matches the HTML element’s id="game". The engine inserts the canvas and sizes it to that container, so give the container a height. Vector2(24, 80) stores an x and y position in pixels. With the default camera, x increases to the right and y increases downwards from the top left.

From the folder containing both files, run npx serve . (requires Node.js and npm), then open the local URL it prints. The canvas should show a circle moving right and wrapping back to the left. Pause, resume and restart it to check each button. Use the local server rather than double-clicking the HTML file: browser modules load over HTTP.

If you already have a project with a bundler, run npm install dino-ge@1.16.0 instead of using the import map. Keep the container and buttons, and load main.js through your project’s usual entry point.

Choose when work runs

Try doubling the movement per step. The explorer travels twice as fast even though you haven’t changed the draw rate.

Engine · Scene · Canvas