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

Hills

What we are doing

A sine wave goes smoothly up and down. Adding three of them, a big slow one and two smaller quicker ones, each with a random size and position, gives natural-looking hills that are different every game.

```js
// Hills from three sine waves of random size and position, added together.
  const waves = [1, 2, 3].map((n) => ({ size: (30 / n) * (0.5 + Math.random()), length: W / (n + Math.random()), shift: Math.random() * W }))

    for (const w of waves) y += w.size * Math.sin(((x + w.shift) / w.length) * Math.PI * 2)
    ground.push(Math.max(120, Math.min(H - 20, y)))
```

What it means

Your turn

  1. Above makeGround, write the comment; at its top, make the three waves.
  2. In the loop, add the waves to y, and keep it within the limits when you push it.

Skills: Drawing on a canvas, Loops

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