Workshop › Space Invaders-style › Step 7 of 21

A short reload

What we are doing

Holding Space should not fire a stream of bullets. After a shot, the next one can only come 350 milliseconds later. We keep the time of each frame in now and the time of the last shot in lastShot.

```js
const COOLDOWN = 350 // milliseconds between shots
let lastShot = -COOLDOWN
let now = 0

  if (now - lastShot < COOLDOWN) return
  lastShot = now

function loop(time) {
  now = time
```

What it means

Your turn

  1. Under BULLET_SPEED, write COOLDOWN; under bullets, write lastShot and now.
  2. At the top of shoot, write the two cooldown lines.
  3. Give loop the time parameter and keep it in now.

Skills: Game states, The game loop

All steps

  1. The ship
  2. The game loop
  3. Move the ship
  4. Stay on screen
  5. Fire
  6. Bullets fly
  7. A short reload
  8. The invaders
  9. The march
  10. Hit!
  11. Score
  12. They shoot back
  13. Bombs fall
  14. Lives
  15. Game over
  16. Best score
  17. A wave in a function
  18. Play again
  19. Waves
  20. Faster and faster
  21. Build it yourself: an extra life