Workshop › Whack-a-Mole › Step 10 of 18
A game is a 30-second round. Before it starts, the field waits with "Click to start"; the first click calls start,
which sets everything up for a new round.
```js
const ROUND = 30000 // a round lasts 30 seconds
let state = 'ready' // 'ready', 'playing' or 'over'
let endsAt = 0
function start() {
state = 'playing'
score = 0
endsAt = now + ROUND
nextPop = now
for (const hole of holes) hole.upUntil = 0
}
if (state !== 'playing') {
start()
return
}
if (state !== 'playing') return
ctx.textAlign = 'center'
if (state === 'ready') {
ctx.fillText('Click to start', canvas.width / 2, canvas.height / 2)
}
```endsAt is the time the round ends: 30 000 ms after the start.start resets the score, sets the end time, makes the first mole come at once and empties the holes.update does nothing unless playing.HOLE_R, write ROUND; under let score, write state and endsAt.pointerdown listener, write start; at the top of the listener, write the start check.if (state !== 'playing') return the first line of update.draw, write the centered "Click to start" block.Skills: Game states
← Show the score Time's running →