Adding Obstacles to Your Game
00:00 In the previous lesson, I showed you how to get your ship moving. In this lesson, you’ll add some asteroids to your asteroid game. Games usually aren’t much fun unless there is a chance you can die.
In the last lesson, I removed the rocks to make it easier to play with the ship. Now it’s time to add them back in. Like you did with the ship, let’s extend
GameObject to write some rock-specific code.
That doesn’t really give the player a fighting chance. To compensate for that, the rock generation code will leave a buffer of space around the ship. Anything less wouldn’t be sporting. Here, I’m inside of
models.py. Before I make the changes to the
GameObject, I need to import
random so that I can use it. Let me just scroll down.
01:10 This class constant specifies the minimum distance between the ship and the randomly generated rocks. There are several different techniques you can use when you need to generate something randomly, but with constraints.
01:42 Then I check if the rock is within the minimum gap. If it isn’t, then you can break out of the loop. The position is done. If it is within the gap, the loop iterates again and a new position is randomly generated until a valid one is found.
As there will be more than one, let’s use a list. The
for loop here inside of the list comprehension will call the
Rock() constructor six times. A better programmer would have a constant here instead of a magic number.
As soon as one shows up, I’ll make sure they get on that. I’m going to scroll down. Inside of the game logic and the drawing sequences, the code is going to have to do something to every object in the game. To help this out, I’ve created a property here called
.game_objects() that returns a list of all the objects in the game. By using the star operator (
*) inside of this list, it deconstructs the rock list, so the result returned here is a single list with all the rocks plus the ship.
._game_logic() method handles the motion of anything that has a velocity. With the new
.game_objects() property, this method can be simplified to iterate over every object in the game and calling each object’s
.move() method. Let me scroll down.
I’m going to make one quick little change to the game offscreen here, increasing the number of rocks. How does
600 sound? I’m doing that to demonstrate that the minimum gap property is working properly.
03:59 I apologize to those of you of a certain age. Well, two certain ages, actually: those who lived through the song the first time and those who lived through it when it was adopted by animated singing lemurs.
04:10 If you have no idea what I’m talking about, be grateful that nineties dance tunes aren’t now stuck in your head. Anyhow, our rocks aren’t much fun. Sure, they appear randomly, but they’re rather still at the moment.
04:40 These constants specify the range. Here, inside of initialize, the speed is randomly chosen, and random angle for direction as well. These two values are combined in a vector, and you have a velocity.
Become a Member to join the conversation.