Adding Laser Bullets to Your Game
Things are coming along quite nicely. You’ve got a ship, you’ve got some rocks, now let’s add your ship’s gun. The bullets in the game will be treated like any other object, so once again, you’ll be extending
GameObject, this time to create a bullet specific class.
The bullets need to come out of your ship and then continue along their original trajectory in a straight line. So once you’ve got the
Bullet object, you’ll need to add some code to make the shooting happen and management of the bullet into the game logic code.
Notice the new optional parameter to
GameObject. Sometimes you want objects to wrap when they hit the edge of the screen, and sometimes you don’t. This parameter and a bit of extra code will take care of that choice. Line 15 stores the
.wraps variable for later use.
It is time to start figuring out how to shoot things with your brand spanking new
Bullet objects. Bullets come out of the spaceship, so let’s take a look at the
Spaceship object code. First off, I’ve added a constant to specify how fast bullets go.
02:08 Now, to add a function on the ship that shoots the bullets. The first thing you need to do is calculate the velocity of the bullet. Take the current direction of the ship, multiply that by a bullet speed, and add it to the velocity of the ship.
Every time you shoot, a message will show up in the terminal telling you how many bullets are in the container. With the model in place, it’s time to make changes to the game file. Switching over to
Inside of the
.__init__() method on line 16, I create a list to store the bullets. On line 18, I update the creation call to the
Spaceship object, passing in that newly created bullet container. Next up, I’m going to add shooting to the input handling code.
Let me just scroll down. Here, I’m looking for the
KEYDOWN event. Remember that when you press a key, the
KEYDOWN event triggers only once. This is different from the
.get_pressed() dictionary that contains
True for as long as the key is held down. For accelerating the ship, holding the button keeps accelerating. For shooting, it is different. It doesn’t matter how long you hold the key, it only fires once.
Scrolling down to the
.game_objects() property. You’ve got more objects in the game now, so this property needs to be updated so that it returns all the rocks, all the bullets, and the ship. Now ask yourself a question.
04:17 What should happen to a bullet when it goes off the screen? The rocks and ship wrap their positions. That might be a bit crazy with bullets. It might lead you to having to deal with the fact that you could shoot yourself.
Instead, as you might’ve guessed when I changed the wrap handling code in the
GameObject base class, let’s have bullets disappear when they go off the screen. To do that, the first thing you have to do is detect when they leave. Some changes to the game object method will handle that. Line 54 gets the bounding rectangle of the screen. Line 55 iterates through all of the bullets. Python gets wonky if you try to modify a list while you’re iterating over it.
04:58 As the desired result of the bullet being off the screen is to remove it, you’re going to need to iterate and remove at the same time. There are two ways to handle this in code: Either create a temporary list of objects to remove and remove all of them after you’re done iterating, or what I’m doing here, which is using a copy of the list.
[:] notation here is using list slicing, but saying, “Give me a slice that is the whole list.” This gives you a copy of the list. Line 55 is actually iterating through that copy of the bullet container.
05:34 There are pros and cons to doing this. Keeping a separate remove list requires extra code. Iterating over a copy means a bit of extra memory in use. Our list of bullets is so small, but that isn’t really a problem in this case, so I’ve gone with the second choice. For each bullet in the copy of the bullet container, I’m checking if it collides with the screen. If it doesn’t, that means it has gone off the edge of space.
05:59 Therefore, it can be removed from the bullet container. You’re all set. You’ve created bullets, you’ve added code to shoot them, you’ve added code to handle when to delete them. Time to see it all in action.
06:18 Watch the bullets go off the screen. I’m going to shoot a bit more. See how the total bullet counter in the terminal has changed? That’s great. It means our bullet removal code has worked. Our bullets aren’t perfect.
Become a Member to join the conversation.