Workshop › Snake › Step 24 of 28

One function that starts a game

What we are doing

To play again we must put every value back to its start. We gather all the starting values in reset() and call it once at the start.

```js
let snake
let dir
let nextDir
let food
let score
let gameOver
let last = 0

function reset() {
  snake = [
    { x: 5, y: 5 },
    { x: 4, y: 5 },
    { x: 3, y: 5 },
  ]
  dir = { x: 1, y: 0 }
  nextDir = dir
  score = 0
  gameOver = false
  placeFood()
}

reset()
requestAnimationFrame(loop)
```

What it means

Your turn

  1. Replace the variable lines (from let snake = [ to let gameOver = false) with the empty declarations and reset.
  2. Delete the lone placeFood() call under placeFood.
  3. Call reset() right before requestAnimationFrame(loop) at the bottom.

Skills: Game states, Functions

All steps

  1. Find the canvas
  2. Get the pen
  3. Paint the board
  4. Think in cells
  5. Keep the position in a variable
  6. A function that draws
  7. A function that moves
  8. The game loop
  9. Slow it down
  10. A direction to move in
  11. Steer with the arrow keys
  12. A body made of a list
  13. Move the whole body
  14. Pick a spot for the food
  15. Draw the food
  16. Eat and grow
  17. Never put food on the snake
  18. A table of turns
  19. No U-turns
  20. Crash into the walls
  21. Crash into yourself
  22. The game over message
  23. Show the score
  24. One function that starts a game
  25. Play again with Space
  26. Remember the best score
  27. Speed up as you grow
  28. Build it yourself: through the walls