Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Creating Enemies

00:00 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 random, randint().

00:19 Let me show you in the REPL how that works. I’m going to enter into the Python REPL by typing python or python3. And my second step is to import random, a module in the built-in library.

00:33 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 1 and 100.

00:50 So, a number or an integer between 1 and 100. Okay. First one was 51. Do it again. There’s 50, 12, and so forth.

01:01 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 random.

01:22 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,

01:37 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.

02:21 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.

02:33 You’re going to do __init__(self)

02:40 .__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 20 by 10a little smaller than the rectangles you were working with before for the player.

02:58 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 Surface for .fill().

03:09 Then you’re going to create the parameter of .rect.

03:14 Again, you use the surf.get_rect() (get rectangle). This time,

03:20 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.

03:29 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.

03:40 The values you’re going to set are between SCREEN_WIDTH—and you always start with the width—+ 20 and SCREEN_HEIGHT + 100.

03:50 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 20 and 100 so that they’ll be at various places off to the right of the screen.

04:05 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 SCREEN_HEIGHT.

04:19 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 center.

04:33 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 .speed.

04:43 And it’s going to be a random int, also, between 5 and 20. And then we’ll learn what the .speed does here. Okay. So that’s the initializing of each of the enemies.

04:54 It’s going to initialize a Surface. That 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.

05:14 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. selfso, 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 self.speed.

05:48 So, somewhere between 5 to 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.

06:04 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 Sprite methods.

06:21 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—

06:36 and you might remember before, down here, updating the player.

06:42 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.

07:10 The next lesson is all about grouping sprites.

Become a Member to join the conversation.