Workshop › Endless Runner › Step 12 of 19

Try again

What we are doing

After a crash, Space (or a tap) should bring back a fresh run. All the starting values move into a reset function, which runs at the start and again after every game over.

```js
let runner
let state // 'ready', 'running' or 'over'
let obstacles
let speed
let nextIn // frames until the next obstacle

function reset() {
  runner = { x: 50, y: GROUND - STAND_H, w: 40, h: STAND_H, vy: 0 }
  state = 'ready'
  obstacles = []
  speed = 6
  nextIn = 60
}

  if (state === 'over') {
    reset()
    return
  }

reset()
```

What it means

Your turn

  1. Replace the five variable lines with plain declarations and the reset function.
  2. At the top of jump, write the game-over block.
  3. Call reset() right above requestAnimationFrame(loop) at the bottom.

Skills: Game states, Functions

All steps

  1. The ground
  2. The runner
  3. Draw again and again
  4. Gravity
  5. Jump
  6. Short hops
  7. Tap to jump
  8. Things that come at you
  9. Cacti keep coming
  10. Wait for the start
  11. Crash
  12. Try again
  13. Faster and faster
  14. Remember the best run
  15. The score counter
  16. Birds
  17. Which keys are down?
  18. Duck
  19. Build it yourself: jump buffering