Asteroid Shooter
An arcade asteroid shooter on HTML5 Canvas. It runs at the same speed on any screen and can be played with a keyboard or with touch buttons.
An arcade asteroid shooter drawn on an HTML5 Canvas. You fly a ship with momentum, clear waves of rocks, and pick up what they drop.
How to play
- Keyboard: A and D or the left and right arrows turn, W or up thrusts, Space fires, and P pauses.
- Touch: on a phone or tablet, four buttons appear under the game: Left, Right, Thrust and Fire. They can be held together.
- Lives: you start with three. After losing one, the ship blinks for two seconds and cannot be hit.
The rocks
- Normal rocks take one hit. Large ones split into two smaller ones.
- Armoured rocks, with a heavier outline, take three hits. They appear from wave 3.
- Fast rocks, with a thin outline, move at twice the speed. They appear from wave 2.
- Explosive rocks, marked with a dashed ring, damage every rock near them when destroyed. They appear from wave 5.
Scoring
Each rock is worth 10 to 40 points depending on its type, multiplied by your combo. The combo grows by one with each rock destroyed, up to ten times, and resets when you lose a life. Clearing a wave adds a bonus of 50 times the number of the wave you are starting.
About three rocks in ten drop a power-up: a stronger weapon (W), a shield that absorbs one hit (S), an extra life (+), or eight seconds of rapid fire (R).
How it is built
The rules live in one module with no canvas and no React in it. A single step function takes the game, the input and a slice of time, and advances everything:
export function step(state: GameState, input: Input, dt: number = STEP, random = Math.random) {
// ...
if (input.thrust) {
ship.vx += Math.cos(ship.angle) * THRUST * dt
ship.vy += Math.sin(ship.angle) * THRUST * dt
}
const drag = DRAG_PER_SECOND ** dt
ship.vx *= drag
ship.vy *= drag
}
Every speed is per second, and the page calls step in fixed slices of one sixtieth of a second, carrying any leftover time to the next frame:
owed = Math.min(owed + (now - last) / 1000, STEP * 5)
while (owed >= STEP) {
step(game, inputRef.current)
owed -= STEP
}
The result is that the game plays at the same speed on a 60 Hz laptop and a 120 Hz phone. Timers such as rapid fire count game time, so they stop when the game is paused.
Because the rules are a plain function with its randomness passed in, they are covered by unit tests: splitting, armour, the explosive chain, the shield, scoring and waves.
Details
- The game pauses itself when you switch tab or window, or scroll it off screen.
- Key releases are always honoured, so a key cannot stay held after a pause.
- The canvas is drawn at the screen's pixel density and scales with its container.
- The score, lives and wave are ordinary text above the canvas, not painted into it.
Limitations
- There is no sound.
- High scores are not saved.
- A screen reader can read the score and state, but the game itself needs sight.
Canvas · Game loop · Fixed timestep · Collision detection · Touch