Adding Background Images
00:00 In this lesson, you’ll start adding some background images. Up to this point, the images you’ve used have only been for the player and the enemies, so now it’s time to make some background images. Let’s make some clouds.
The steps are going to look really familiar. You’re going to create a
Cloud class. You’re going to add an image of a cloud to it. You’ll create a method
.update() that moves the
cloud toward the left side of the screen, and then you’ll create a custom event and handler to create new
cloud objects at a set time interval.
And this should sound familiar, because it’s going to look really similar to the
Enemy class that you created. Let me have you start to code it. Again, you might remember what
Enemy looks like as a class. Again, loading the image, setting the color key, getting the rectangle, finding its center, setting the speed, and then its
.update(), which was moving it to the left. Well, it’s going to look really similar.
and set up
super(). Okay. So, you’re going to create the
Surface based upon
pygame.image.load(). The directory is
images/ and it’s
cloud.png. Then run the method
.convert(). Move this up a little bit.
02:12 Then you need to do the color key. One unique thing here is the clouds are white and the color key in this case is going to be black. Basically, that’s what should be transparent, is the black areas.
So between there and
SCREEN_WIDTH + 100, and then one more random for the height from
0—that’s the top of the screen—to the bottom of the screen, which is the total
SCREEN_HEIGHT. So again, this is going to place it off the screen in a variety of positions between
100 off to the right of the screen, and then this is anywhere from the top to the bottom of the screen. Okay.
center. And then this is going to close the rectangle here. I need to move the cloud based on a constant speed, unlike the missiles, and remove it when it passes the left edge again.
So, this is the
.update() method you’re creating for the
self.rect, move in place,
.move_ip(), and it’s going to move at
-5, 0, not moving up and down, and then check
if self.rect—the rightmost edge of the rectangle, if that’s less than
0, then kill the
04:52 Great. Looks good. So what’s next? Well, while your event loop is running, you’re going to look for the new event, which will be “Hey, should we add a new cloud?” This should look pretty similar.
that’ll not only create the new cloud, but add it to the sprite groups. So here’s
new_cloud, it is an instance of
clouds, use that
.add() method to add a
new_cloud. Ope, not
We could do that in the same block here.
# Update enemy position and clouds.
clouds.update() will update all the clouds. One thing you’re going to change, though, is we’re going to change the color. Fill the screen with a blue, so it’s actually like a sky, and those white clouds will now stand out.
Just like this happens every quarter of a second. And then here, you created that new
Group, and then here you created the custom event
ADDCLOUD—looks good. Okay.
And then again, your
Cloud class. Going to save. Okay. So
python sky_dodge.py. That’s blue! Looks good. I see a cloud. I don’t see it moving. Something must be wrong. It looks like we may need to update something.
Become a Member to join the conversation.