Workshop › Frogger-style Crossing › Step 9 of 22

Hit by a car

What we are doing

If a car covers the frog's square, the frog is hit and a new one starts from the bottom. The frog's box is shrunk a little, so only a real overlap counts.

```js
function newFrog() {
  frog = { x: 5, y: START_ROW }
}

function laneAt(row) {
  return LANES.find((lane) => lane.row === row)
}

function die() {
  newFrog()
}

function hitByCar(lane) {
  return items(lane).some((x) => frog.x + 0.15 < x + lane.len && frog.x + 0.85 > x)
}

  const lane = laneAt(frog.y)
  if (!lane) return
  if (hitByCar(lane)) die()
```

What it means

Your turn

  1. Under the offset line, write newFrog and laneAt.
  2. Above DIRECTIONS, write die; above update, write hitByCar.
  3. At the end of update, write the three check lines.

Skills: Collision detection

All steps

  1. Road and river
  2. The frog
  3. Keep drawing
  4. Hop
  5. Swipe to hop
  6. Lanes of traffic
  7. Cars
  8. Traffic moves
  9. Hit by a car
  10. Three lives
  11. Play again
  12. Tap to play again
  13. Logs on the river
  14. Stay on the logs
  15. Ride the log
  16. Back in line
  17. Home slots
  18. Get home
  19. Points for progress
  20. Next level
  21. Best score
  22. Build it yourself: a time limit