We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Collisions

You may have noticed that you can fly through asteroids without any consequences.

We need collision detection. Now, this is a fairly deep topic, but we're going to keep things simple: we'll treat everything (including our triangular ship) as a circle when it comes to collisions.

Detecting a collision between two circles is simple:

  1. Calculate the distance between the center of the two circles: "distance"
  2. Calculate the radius of each circle: r1 and r2.
  3. If distance is less than or equal to r1 + r2, the circles are colliding. If not, they aren't!

Assignment

Everything that collides inherits from CircleShape, so that seems like a good place to write our collision logic. We also want to log when collisions happen, so we'll use one more function from that logger.py module.

  1. Each CircleShape has a position property that is a pygame.Vector2. You can use the position's distance_to method to get the distance between the two shapes, then use that distance to decide whether to return True or False.

It's okay for asteroids to simply pass through each other for now.

If that worked, run and submit the CLI tests.