Workshop › Zelda-style Dungeon › Step 19 of 22
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)
}
```update does nothing unless playing, so the time and the enemies freeze.frames, write state; in reset, set it after frames.update, stop when not playing; after hurt = 60, end the game on the last heart.draw, draw the end screen.Skills: Game states