Workshop › Space Invaders-style › Step 10 of 21

Hit!

What we are doing

A bullet that overlaps a living invader destroys it and is used up. overlaps is the classic check for two boxes.

```js
function overlaps(a, b) {
  return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y
}

  for (const bullet of bullets) {
    const hit = invaders.find((invader) => invader.alive && overlaps(bullet, invader))
    if (hit) {
      hit.alive = false
      bullet.y = -100 // used up; removed below
    }
  }
```

What it means

Your turn

  1. Above alive, write overlaps.
  2. In update, between moving and filtering the bullets, write the hits loop.

Skills: Collision detection

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