Asteroids

Asteroids drops you straight into a shared instance of up to 50 players on an arena far larger than your screen, which scrolls with your ship and wraps seamlessly at every edge. Your ship sits in the centre of the view and points at your mouse; hold the button to thrust and coast when you let go. Every ship fires continuously at a steady rate, so the arena is always thick with bullets. The biggest rocks split into two smaller ones when hit, those split again, and the smallest are destroyed, while the field is kept stocked to a fixed weight of rock.

It is built to exercise the parts of the engine the smaller examples don't: a world bigger than the canvas, hundreds of moving things, and per-viewer relevance, the idea that the server should send each player only what is on (or near) their screen.

Move the mouse to aim · hold the mouse button (or touch) to thrust · you fire automatically. Open this page in a second window to join the same arena.
Live demo against game.gamehoster.org/asteroids. If it says “connecting…”, the game server isn't reachable. You can also run it locally with the test server (docker compose up --build in src/testing) and open http://localhost:8080/examples/asteroids/.

Code

The whole game is a folder of files. Here is Asteroids' definition, grouped the way this example's tabs are. Each file links to the tab that shows it in full:

asteroids/
  gamehoster-game.json
  gamehoster-player/
    gamehoster-player-join-schema.json
    gamehoster-player-schema.json
    gamehoster-player-join.js
    gamehoster-player-leave.js
    gamehoster-player-update-frontend.js
    gamehoster-player-view.js
  gamehoster-instance/
    gamehoster-instance-schema.json
    gamehoster-instance-update-backend.js
  gamehoster-commands/
    aim/
      gamehoster-command-schema.json
      gamehoster-command.json
      gamehoster-command-handler.js
    thrust/
      gamehoster-command-schema.json
      gamehoster-command.json
      gamehoster-command-handler.js
  gamehoster-entities/
    bullet/
      gamehoster-entity-schema.json
      gamehoster-entity-update-frontend.js
    rock/
      gamehoster-entity-schema.json
      gamehoster-entity-update-frontend.js
Asteroids' definition files. The Game, Player, Instance, Commands and Entities tabs show each in full, and the Frontend tab shows the browser code that runs it.