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

Damage

What we are doing

A tank inside the blast loses health: 60 for a hit right on it, less the farther away, nothing at the edge. Each tank shows a health bar that turns red below 30.

```js
for (const t of tanks) {
  const d = Math.hypot(t.x - x, t.y - 6 - y)
  if (d < BLAST) t.hp = Math.max(0, Math.round(t.hp - (1 - d / BLAST) * 60))
}

  // Health bar
  ctx.fillStyle = '#0f172a'
  ctx.fillRect(t.x - 16, t.y - 22, 32, 4)
  ctx.fillStyle = t.hp > 30 ? '#22c55e' : '#ef4444'
  ctx.fillRect(t.x - 16, t.y - 22, (32 * t.hp) / 100, 4)
```

What it means

Your turn

  1. In explode, after digging, hurt the tanks nearby.
  2. In the tank loop of draw, after the barrel, draw the health bar.

Skills: Collision detection, 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