Workshop › Frogger-style Crossing › Step 7 of 22

Cars

What we are doing

items works out where each car of a lane is right now: one every spacing tiles, slid by the lane's offset, wrapped around the period. Then every car is drawn as a colored box.

```js
// Left edges (in tiles) of the cars or logs in a lane right now.
function items(lane) {
  const p = period(lane)
  const xs = []
  for (let start = 0; start < p; start += lane.spacing) {
    xs.push((((start + lane.offset) % p) + p) % p - lane.len)
  }
  return xs
}

  for (const lane of LANES) {
    ctx.fillStyle = lane.color
    for (const x of items(lane)) {
      ctx.fillRect(x * TILE + 2, TOP + lane.row * TILE + 6, lane.len * TILE - 4, TILE - 12)
    }
  }
```

What it means

Your turn

  1. Under period, write the comment and items.
  2. In draw, between the rows and the frog, draw the lanes.

Skills: Arrays and objects, Drawing on a canvas

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