Workshop › Whack-a-Mole › Step 6 of 18

Moles pop up

What we are doing

Every 700 ms a mole pops up in a random empty hole and stays up for one second.

```js
let nextPop = 0 // when the next mole pops up

function update() {
  if (now >= nextPop) {
    const empty = holes.filter((hole) => !isUp(hole))
    if (empty.length > 0) {
      const hole = empty[Math.floor(Math.random() * empty.length)]
      hole.upUntil = now + 1000
    }
    nextPop = now + 700
  }
}

  update()
```

What it means

Your turn

  1. Under let now, write let nextPop = 0.
  2. Above function draw() {, write update.
  3. In loop, call update() before draw().

Skills: Game states, The game loop

All steps

  1. A green field
  2. Nine holes
  3. Draw the holes
  4. A clock and a loop
  5. A mole in a hole
  6. Moles pop up
  7. Which hole was clicked?
  8. Whack!
  9. Show the score
  10. Click to start
  11. Time's running
  12. Time's up
  13. Best score
  14. How far into the round?
  15. Faster and faster
  16. Remember each hit
  17. Floating +1
  18. Build it yourself: combos