It’s time to start adding some enemies. To create them, you’re going to use a lot of the same steps you did with the player, but there’ll be a couple additional wrinkles. Let’s get started. For this next chunk of code you’re going to need some random numbers, so to do that you’re going to use something from the built-in module
Let me show you in the REPL how that works. I’m going to enter into the Python REPL by typing
python3. And my second step is to import
random, a module in the built-in library.
Okay. So, how do you create a random int? Well,
random.randint() takes two arguments. Basically, they are the values you want to use between—so let’s say, between
So, a number or an integer between
100. Okay. First one was
51. Do it again. There’s
12, and so forth.
So you can see the values popping out very randomly. Nice! Let’s shut this REPL down and start the code. You’re going to need some of that randomness that you just experienced with those enemies. So up here, at line 4, you’re going to import
You just need to import it. Okay. So to define this enemy, it’s going to look kind of similar to how you created a class for the player, and how you were able to create—not only based upon the
Sprite—and created the
Surface, and created the rectangle from it,
and then you created this
.update() method for allowing the user to control it. In this case, we’re not going to have the user controlling this—we’re going to have to have the actions be updated via the program itself.
01:48 But then what happens when the enemies, as they move from left to right across the screen—how do they disappear? So, a few things to think about. Here at line 53, you’re going to create this new class.
Enemy is a
pygame.sprite.—and then again, the capital
Sprite for the
Sprite class. That’s what you’re basing it upon. A lot of it, again, looks similar.
You’re going to do
.__init__(). So in that process, when one of these is instantiated and initialized, you’ll create a
Surface parameter. In this case, they’re going to be
10—a little smaller than the rectangles you were working with before for the player.
You’re going to fill that
Surface. You’re going to use white again. RGB, so
(255, 255, 255)—again, that’s a tuple, just like this was a tuple coming into the
Then you’re going to create the parameter of
Again, you use the
surf.get_rect() (get rectangle). This time,
instead of having it start at the default of
(0, 0) on that upper-left corner, this is where you’re going to have it kind of randomly appear.
Create its origin point, if you will. So, using
random, you’re going to use
randint(), which you just practiced with. And it’s nice here—it shows it’s going to return a random integer in the range.
The values you’re going to set are between
SCREEN_WIDTH—and you always start with the width—
+ 20 and
SCREEN_HEIGHT + 100.
These will be off to the right—hopefully that makes sense. It’s going to be all the way to the right—that’s what
SCREEN_WIDTH will do—and then these will be between
100 so that they’ll be at various places off to the right of the screen.
Then you’re going to do this again, but this is for the height. We’ll create not only enemies at different distances, but at a couple different heights. So from
0, the top of the screen, to the bottom of the screen, which is
So between those two values, provide a random int for its height. So, the width would be here and the height there—that all looks good. And this closes this value for the
And this closes the
.get_rect(), where you’re setting its center point. So then down here, you’re creating a new parameter that’s called
And it’s going to be a random int, also, between
20. And then we’ll learn what the
.speed does here. Okay. So that’s the initializing of each of the enemies.
It’s going to initialize a
Surface will have a
.fill(). You’re going to get a rectangle from it with this particular
center based upon these random values, and then the other parameter is its
.speed. So down here, like before, to move this object—the sprite, based on the speed.
But also you’re going to need to remove the sprite when it passes off the left edge of the screen. So, we’re creating an
.update() method, also.
self—so, remember how to move a rectangle? It was called move in place, so
.move_ip(). In this case, you’re moving leftward, so we’re going to minus using that new parameter of
.speed that you created. The negative
.speed value. So, a negative version of
So, somewhere between
20 will be the number of pixels it’s moving each time. And then it’s not moving up or down—it’s just moving this way, left, minus-ing its position from these values that started here.
Nice. And then part of the
.update() will be to check if the rectangle hits the right side. If its
.right value is less than
0, you’re going to do something called
.kill(), which is one of the built-in
And we’ll learn a little more about what that is. You get that for free by basing your class upon
Sprite, so you can use that method called
.kill(). Nice! If you created these enemies—well, you created the class—
and you might remember before, down here, updating the
Okay. And then where did you create the
player? Well, you instantiated the
player here. But for enemies, instead of just there being a single one, somewhere within this loop we’re going to have to have it generate new enemies.
06:55 And then also look to see if the enemies have gone off the edge of the screen. So, we’ve got a few things to put together, but there’s a large number of sprites now that you’re going to have that you’re going to generate, and this is where you’ll learn about the next concept.
Become a Member to join the conversation.