Workshop › Frogger-style Crossing › Step 10 of 22

Three lives

What we are doing

The frog has three lives. Each hit costs one; after the last, the game is over, nothing moves and a message covers the board.

```js
let lives = 3
let state = 'playing' // 'playing' or 'over'

function die() {
  lives -= 1
  if (lives > 0) {
    newFrog()
    return
  }
  state = 'over'
}

  if (state !== 'playing') return

  ctx.fillStyle = 'white'
  ctx.font = 'bold 18px sans-serif'
  ctx.textAlign = 'right'
  ctx.fillText('Lives: ' + lives, canvas.width - 10, 27)

  if (state === 'over') {
    ctx.fillStyle = 'rgba(15, 23, 42, 0.75)'
    ctx.fillRect(0, 0, canvas.width, canvas.height)
    ctx.fillStyle = 'white'
    ctx.textAlign = 'center'
    ctx.font = 'bold 32px sans-serif'
    ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2)
    ctx.font = '18px sans-serif'
    ctx.fillText('Press Space to play again', canvas.width / 2, canvas.height / 2 + 32)
  }
```

What it means

Your turn

  1. Under frog, write lives and state.
  2. Replace the body of die.
  3. Make the state check the first line of hop and of update.
  4. At the end of draw, write the lives text and the game over block.

Skills: Game states

All steps

  1. Road and river
  2. The frog
  3. Keep drawing
  4. Hop
  5. Swipe to hop
  6. Lanes of traffic
  7. Cars
  8. Traffic moves
  9. Hit by a car
  10. Three lives
  11. Play again
  12. Tap to play again
  13. Logs on the river
  14. Stay on the logs
  15. Ride the log
  16. Back in line
  17. Home slots
  18. Get home
  19. Points for progress
  20. Next level
  21. Best score
  22. Build it yourself: a time limit