Workshop › Flappy-style Bird › Step 23 of 30

A function for a fresh start

What we are doing

To play again, every piece of state must go back to its start. We collect the starting values in one function, reset, and the first game starts through it too.

```js
let bird
let pipes
let frame
let score
let state // 'ready', 'playing' or 'over'

function reset() {
  bird = { x: 100, y: 300, vy: 0, r: 14 }
  pipes = []
  frame = 0
  score = 0
  state = 'ready'
}

reset()
requestAnimationFrame(loop)
```

What it means

Your turn

  1. Replace the five let lines with the five lines without values and the reset function.
  2. At the very bottom, write reset() above requestAnimationFrame(loop).

Skills: Game states, Functions

All steps

  1. Get the canvas and the pen
  2. Paint the sky
  3. Draw the bird
  4. A function that draws
  5. A function that moves
  6. The game loop
  7. Gravity
  8. A flap
  9. Space, Up arrow or a tap
  10. Wait for the first flap
  11. The ground and the sky
  12. Tell the player
  13. A pipe as data
  14. Draw the pipes
  15. The world moves, not the bird
  16. A new pipe at a random height
  17. A pipe every 90 frames
  18. Remove the pipes you passed
  19. Is the bird level with a pipe?
  20. Through the opening
  21. Score each pipe once
  22. Show the score
  23. A function for a fresh start
  24. Try again
  25. One place where the game ends
  26. Remember the best score
  27. Show the best score
  28. The same speed on every screen
  29. No catching up after a pause
  30. Build it yourself: a medal