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

Boom

What we are doing

Where the shell meets the ground there is an explosion: an orange fireball that grows for 20 frames. During that time the state is 'boom', and nobody can shoot. A shell that flies off the side just ends the shot, with no explosion.

```js
const BLAST = 30 // explosion radius
let blast // { x, y } while an explosion shows
let timer // frames left to watch the explosion

  blast = null

function explode(x, y) {
  blast = { x, y }
}

  if (state === 'boom' && --timer === 0) {
    blast = null
    state = 'aiming'
  }

  if (hit !== 'away') explode(shell.x, shell.y)
  shell = null
  state = 'boom'
  timer = 20

  if (blast) {
    // The fireball grows as the timer runs down.
    ctx.fillStyle = 'rgba(249, 115, 22, 0.8)'
    ctx.beginPath()
    ctx.arc(blast.x, blast.y, BLAST * (1 - timer / 40), 0, Math.PI * 2)
    ctx.fill()
  }
```

What it means

Your turn

  1. Under GRAVITY, write BLAST; under shell, write blast and timer; in reset, clear blast.
  2. Above update, write explode.
  3. At the top of update, count the explosion down; at the end, explode and wait.
  4. In draw, draw the fireball after the shell.

Skills: Game states, Drawing on a canvas

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