Adding Collision Detection to Your Bullets
00:10 You’ve seen basic collision detection already and used it to see if game objects passed off the screen. Now, just a little tidying up and some consequences when things get shot. You’re interested in two kinds of collisions: Bullets can collide with rocks, and in that case, the rock should go away. And rocks can collide with ships, wherein the ship should go away.
Here I am inside of
game.py. Let me scroll down a bit. This is inside of the
._game_logic() method just after the code that figures out if the bullets left the screen. Using very similar logic for removing the bullet, I’m going to loop through the bullets and the rocks and see if any of them are colliding.
I’m using the same trick as before, iterating through a copy of the list so the original can be modified. For each bullet, iterate through all of the rocks. For each rock-bullet pair, check if they collide using the method in the
GameObject base class that was written many lessons ago.
01:13 If there is a rock-bullet collision, you need to remove the rock, then remove the bullet. You can also short circuit the rocks iteration as the bullet has done its job and won’t collide with anything else.
For each rock, check if there’s a ship collision. If there is, set the
None. For our simple version of the game, there’s only one life. If you had multiple lives, this would be where you adjusted the life counter. Setting the
None here has some deep complications in the code.
02:36 If the user moves a ship that isn’t there, you’ll get a crash, so lines 40 and 41 exit the input handling method early. There’s a bug in here. You can decide for yourself whether I was too lazy to fix it or left it as an exercise for you. This check isn’t sufficient.
This highlights the
.ship being set to
None being used as an indicator for game over. It isn’t an elegant solution. A smarter choice might be to have the aliveness of the ship be a part of the ship’s state, and then with any of the ship’s methods, do nothing if a method is called but the ship is not alive.
03:47 I told you I wasn’t very good at these things. In the original Asteroids game, the rocks didn’t just disappear. They split into smaller chunks. In the next lesson, you’ll add that capability to your game.
Become a Member to join the conversation.