Workshop › Zelda-style Dungeon › Step 19 of 22

Game over

What we are doing

Losing the last heart ends the game: everything stops, a dark screen says so, and Space (or a tap) starts again. The screen already knows what to say after an escape; the stairs come next.

```js
let state // 'playing', 'won' or 'over'

  state = 'playing'

  if (state !== 'playing') return

      if (hearts === 0) state = 'over'

    if (state !== 'playing') reset()
    else if (swing === 0) swing = 12

  if (state !== 'playing') {
    reset()
    return
  }

  if (state !== 'playing') {
    ctx.fillStyle = 'rgba(12, 10, 9, 0.75)'
    ctx.fillRect(0, 0, canvas.width, canvas.height)
    ctx.fillStyle = 'white'
    ctx.textAlign = 'center'
    ctx.font = 'bold 28px sans-serif'
    ctx.fillText(state === 'won' ? 'You escaped in ' + seconds(frames) + ' s!' : 'Game over', canvas.width / 2, 190)
    ctx.font = '16px sans-serif'
    ctx.fillText('Press Space to play again', canvas.width / 2, 222)
  }
```

What it means

Your turn

  1. Above frames, write state; in reset, set it after frames.
  2. At the top of update, stop when not playing; after hurt = 60, end the game on the last heart.
  3. On Space and on a tap, start again when the game is over.
  4. At the end of draw, draw the end screen.

Skills: Game states

All steps

  1. A room
  2. The hero
  3. The game loop
  4. Walk
  5. Walls
  6. A touch pad
  7. Next room
  8. The sword
  9. Enemies
  10. Strike!
  11. They chase you
  12. They wander
  13. Ouch
  14. Blinking and hearts
  15. Keys and hearts
  16. A world that remembers
  17. The locked door
  18. A timer
  19. Game over
  20. The stairs out
  21. Best time
  22. Build it yourself: cleared rooms