Workshop › Pac-Man-style Maze Chase › Step 32 of 42
Caught!
What we are doing
After everyone has moved, compare the player's drawn position with each ghost that is out of the house. Closer than
0.6 tiles is a catch: one life less and everyone back to the start, or, with no lives left, the game is over.
```js
function caught() {
lives -= 1
if (lives > 0) {
placeActors()
return
}
state = 'over'
}
const p = position(player)
for (const g of ghosts) {
if (g.waiting > 0) continue
const q = position(g)
if (Math.abs(p.x - q.x) + Math.abs(p.y - q.y) > 0.6) continue
caught()
return
}
```
What it means
The distance is |dx| + |dy| in tiles (walking distance on the grid), cheap and good enough here.
Ghosts still in the house are skipped.
After caught, return stops the frame: everyone has just been placed again.
Your turn
Above the key listener, write caught.
At the end of update, after an empty line, write the catch loop.