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

The computer's shot

What we are doing

On its turn the computer "thinks" for one second (60 frames) and then fires with its barrel as it is. It doesn't aim yet; that comes later. A small triangle above a tank shows whose turn it is.

```js
let thinking // frames until the computer shoots

  if (turn === 1) thinking = 60

  if (state === 'aiming' && turn === 1 && --thinking === 0) {
    fire()
  }

  for (const [i, t] of tanks.entries()) {

    if (i === turn && state === 'aiming') {
      ctx.fillStyle = '#0f172a'
      ctx.beginPath()
      ctx.moveTo(t.x - 5, t.y - 34)
      ctx.lineTo(t.x + 5, t.y - 34)
      ctx.lineTo(t.x, t.y - 27)
      ctx.fill()
    }
```

What it means

Your turn

  1. Under state, write thinking; in endTurn, start thinking on the computer's turn.
  2. In update, after the explosion block, fire when thinking is over.
  3. In draw, number the tanks in the loop, and draw the triangle after the health bar.

Skills: Game states, The game loop

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