Workshop › Scorched Earth-style Artillery › Step 14 of 23

Taking turns

What we are doing

After each shot the turn passes to the other tank. When it's the computer's turn, your keys don't aim and Space doesn't fire. The top right says whose turn it is. (The computer learns to shoot in the next step.)

```js
function endTurn() {
  turn = 1 - turn
  state = 'aiming'
}

    endTurn()

  if (state !== 'aiming' || turn !== 0) return

    if (turn === 0) fire()

  ctx.textAlign = 'right'
  ctx.fillText(turn === 0 ? 'Your turn' : 'Computer', W - 10, 20)
```

What it means

Your turn

  1. Above update, write endTurn; when the explosion ends, call it.
  2. At the top of aimBy, aim only on your turn; on Space, fire only on your turn.
  3. At the end of draw, show whose turn it is.

Skills: Game states

All steps

  1. The ground
  2. Hills
  3. Two tanks
  4. The barrel
  5. The game loop
  6. Aim
  7. Angle and power
  8. Fire!
  9. Flight
  10. Boom
  11. Craters
  12. Falling tanks
  13. Damage
  14. Taking turns
  15. The computer's shot
  16. Hitting a tank
  17. Win or lose
  18. Wind
  19. The computer aims
  20. Aim with the mouse
  21. Drag and let go
  22. The atan2 trap
  23. Build it yourself: a trail