Workshop › Wolfenstein-style 3D Maze › Step 4 of 18

Turn around

What we are doing

Left and right (or A and D) turn the player: the angle changes a little every frame while the key is held.

```js
const TURN = 0.04 // radians per frame
const keys = {}

document.addEventListener('keydown', (event) => {
  keys[event.key] = true
  if (event.key.startsWith('Arrow')) event.preventDefault()
})
document.addEventListener('keyup', (event) => {
  keys[event.key] = false
})

function update() {
  if (keys.ArrowLeft || keys.a) player.angle -= TURN
  if (keys.ArrowRight || keys.d) player.angle += TURN
}

  update()
```

What it means

Your turn

  1. Above MINI, write TURN; under player, write keys.
  2. Above draw, write the key listeners and update.
  3. In loop, call update() before draw().

Skills: Keyboard and mouse input

All steps

  1. The map
  2. The player
  3. Ceiling, floor and a loop
  4. Turn around
  5. Walk
  6. Is it a wall?
  7. Stop at walls, slide along them
  8. A ray: where it starts
  9. A ray: the grid lines
  10. A ray: walk to the wall
  11. The 3D view
  12. No fish-eye
  13. Light and shade
  14. A timer
  15. Escape!
  16. Best time
  17. Touch controls
  18. Build it yourself: run