Workshop › Lunar Lander › Step 22 of 31

Readouts

What we are doing

Four readouts at the top left show the fuel, the falling speed, the sideways speed and the tilt. Each is green while it is safe for landing and red when it is not.

```js
// Readouts: green while the value is safe for landing, red when it is not.
const readouts = [
  ['Fuel ' + lander.fuel, lander.fuel > 50],
  ['Down ' + lander.vy.toFixed(1), lander.vy <= SAFE.vy],
  ['Side ' + lander.vx.toFixed(1), Math.abs(lander.vx) <= SAFE.vx],
  ['Tilt ' + Math.round((lander.angle * 180) / Math.PI) + '°', Math.abs(lander.angle) <= SAFE.angle],
]
ctx.font = 'bold 14px monospace'
ctx.textAlign = 'left'
readouts.forEach(([text, ok], i) => {
  ctx.fillStyle = ok ? '#4ade80' : '#f87171'
  ctx.fillText(text, 10, 20 + i * 18)
})
```

What it means

Your turn

In draw, above ctx.textAlign = 'center', write the readout lines, followed by an empty line.

Skills: Drawing on a canvas, Arrays and objects

All steps

  1. Random hills
  2. Draw the hills
  3. A landing pad
  4. The height at any x
  5. The lander
  6. Draw it around its own middle
  7. Two feet
  8. The game loop
  9. Moon gravity
  10. Around the sides
  11. Flying or not
  12. Touching the ground
  13. Remember held keys
  14. Tilt
  15. The engine
  16. A flickering flame
  17. Fuel
  18. Touch controls
  19. Landing or crashing
  20. Fly again
  21. Landed or crashed?
  22. Readouts
  23. Levels
  24. Score
  25. Next level or new game
  26. A narrower pad
  27. A high score
  28. Burst into pieces
  29. Pieces fall and fade
  30. Draw the wreck
  31. Build it yourself: wind