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
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.
.__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.
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.
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
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—
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.