Workshop › Pac-Man-style Maze Chase › Step 24 of 42

Scatter or chase?

What we are doing

Ghosts that always chase would bunch up in a line. So they switch modes on a clock: in every 27-second cycle, the first 7 seconds are scatter (each ghost goes to its own corner, a breather for you), the rest is chase. One counter and % make a repeating schedule.

```js
const SCATTER = 420 // frames of scatter at the start of every 27-second cycle
const CYCLE = 1620

let clock // frames played on this life, for the scatter / chase cycle

  clock = 0

function mode() {
  return clock % CYCLE < SCATTER ? 'scatter' : 'chase'
}

function update() {
  clock += 1
```

What it means

Your turn

  1. Under GHOSTS write SCATTER and CYCLE; under let level write let clock.
  2. In placeActors, at the end, write clock = 0.
  3. Above target, write mode.
  4. At the top of update, write clock += 1 and an empty line.

Skills: Game states

All steps

  1. The maze as text
  2. The game loop
  3. Draw the walls
  4. A set of pellets
  5. Draw the pellets
  6. The player
  7. Where can you walk?
  8. What a mover remembers
  9. One frame of movement
  10. Move every frame
  11. Turns you can press early
  12. Glide between tiles
  13. Turn back at once
  14. Swipes on a phone
  15. Eat the pellets
  16. Show the score
  17. Clear the maze
  18. A red ghost
  19. A ghost never turns back
  20. The ghost walks
  21. The ghost hunts you
  22. Four ghosts in the house
  23. One by one out of the house
  24. Scatter or chase?
  25. Every ghost to its corner
  26. Pink cuts you off
  27. Orange is shy
  28. Cyan works with red
  29. Wait for the first key
  30. Tell the player to start
  31. Three lives
  32. Caught!
  33. Game over
  34. Play again
  35. Frighten the ghosts
  36. Power pellets work
  37. Scared ghosts wander slowly
  38. Eat a scared ghost
  39. Blue, then flashing
  40. Harder levels
  41. A best score that lasts
  42. Build it yourself: the slow tunnel