Workshop › Space Invaders-style › Step 5 of 21

Fire

What we are doing

Space fires a bullet from the cannon. Bullets live in a list, each a small box starting just above the ship.

```js
const BULLET_SPEED = 8
let bullets = []

function shoot() {
  bullets.push({ x: ship.x + SHIP_W / 2 - 2, y: ship.y - 12, w: 4, h: 12 })
}

  if (event.key === ' ') shoot()
```

What it means

Your turn

  1. Under SHIP_SPEED, write BULLET_SPEED; under ship, write let bullets = [].
  2. Under keys, write shoot; in keydown, fire on Space.

Skills: Keyboard and mouse input, Arrays and objects

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