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

Two tanks

What we are doing

Two tanks: yours (blue) on the left, the computer's (red) on the right. Each remembers where it is, its health, and how it aims. They sit on the ground: groundAt(x) tells the ground's height at any x, even between or outside columns.

```js
let tanks // [you, the computer]: { x, y, hp, angle, power, color }

const groundAt = (x) => ground[Math.max(0, Math.min(W - 1, Math.round(x)))]

function reset() {
  makeGround()
  tanks = [
    { x: 70, y: 0, hp: 100, angle: -Math.PI / 4, power: 8, color: '#2563eb' },
    { x: W - 70, y: 0, hp: 100, angle: (-3 * Math.PI) / 4, power: 8, color: '#dc2626' },
  ]
  for (const t of tanks) t.y = groundAt(t.x)
}

  for (const t of tanks) {
    ctx.fillStyle = t.color
    ctx.fillRect(t.x - 10, t.y - 8, 20, 8)
  }

reset()
```

What it means

Your turn

  1. Under ground, write tanks.
  2. Under makeGround, write groundAt and reset.
  3. At the end of draw, draw the tanks; at the bottom, call reset() instead of makeGround().

Skills: Drawing on a canvas, Arrays and objects

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