Workshop › Frogger-style Crossing › Step 4 of 22

Hop

What we are doing

Each arrow press makes the frog hop one square, never off the board. Holding the key does not keep hopping: one press, one hop, like the arcade game.

```js
function hop(dx, dy) {
  frog.x = Math.min(COLS - 1, Math.max(0, frog.x + dx))
  frog.y = Math.min(START_ROW, Math.max(0, frog.y + dy))
}

const DIRECTIONS = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
document.addEventListener('keydown', (event) => {
  const direction = DIRECTIONS[event.key]
  if (direction) {
    event.preventDefault()
    // One hop per press: holding the key down does not hop again.
    if (!event.repeat) hop(...direction)
  }
})
```

What it means

Your turn

Under frog, write hop, DIRECTIONS and the keydown listener.

Skills: Keyboard and mouse input

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