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

Wind

What we are doing

Every shot has a new wind: a small push sideways, added to the speed each frame, like gravity but across. The top middle shows its direction and strength.

```js
let wind

const newWind = () => (wind = Math.round((Math.random() - 0.5) * 10) / 100) // -0.05 to 0.05

  newWind()

    newWind()

// One frame of flight: wind pushes sideways, gravity pulls down. Returns what it hit, or null.
  s.vx += wind

  ctx.textAlign = 'center'
  const arrow = wind > 0 ? '→' : wind < 0 ? '←' : ''
  ctx.fillText('Wind ' + arrow + ' ' + Math.abs(Math.round(wind * 100)), W / 2, 20)
```

What it means

Your turn

  1. Above state, write wind; above fire, write newWind.
  2. Call newWind() in reset before state, and in endTurn after the turn changes.
  3. In fly, add the wind; update the comment above it.
  4. In draw, show the wind before the turn text.

Skills: Velocity and gravity, 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