Sprite Images in PY Games

Updated: February 17, 2022
No Credit Card Required


All right, you have a game, but let's be honest, it's kind of ugly. The player and enemies are just white blocks on a black background. In this lesson, let's replace all those boring rectangles with some cooler images that will make the game feel like an actual game.

But before we start, create a new Repl and name it PGD_14_Spriteimages.

We need some additional images to use to upgrade our game. You can use images that we created or you can create your own. Here are the links to several images. Open the links listed, then find and click the Download button. This will open up a new tab. Right click on the image and save as to download. Here's a website URL if you want to create your own images.

Once you have your assets downloaded, they're ready to be uploaded to your Repl. To upload your assets, follow these steps.

1. Go to your Repl and open your PGD 13 project.

2. Find the file section in your project.

3. Three, look for the pancake menu, the three stack dots and click it.

4. Then four, click on the Upload File.

5. Five, find your assets in the pop up window and click on them, then click Open.

Before you use images to represent the player and enemy sprites, you need to make some changes to their CONSTRUCTORS. A Constructor is a special kind of Method that Python calls when it creates a new Object using the definitions found in the Class.

The Constructor assigns values to any Instance Variables that the Object will need when it starts. This code should replace the code that we use previously. Go ahead and add this after importing pygame and Random. Here's what it should look like.

The pygame.image.load, uploads your image, you pass it a path to the file by writing the name of the file in the parentheses as a String. It returns a Surface and the duck convert call at the end optimizes the surface, making future dot BLIT calls faster. The next line uses set.color key to indicate the color Pygame will render as transparent. In this case, you choose white because that's the background color of the jet image.

The AR le Excel constant is an optional Parameter that helps Py Game render more quickly on non accelerated displays. This is added to the Py game locals import statement towards the top of your game. Replace the two lines after Super in your Repl with this code. The INIT uses .super to call the INIT Method of Sprite, which allows us to change the surf of an image instead of a simple shape.

Set Color key is used to set a transparent color for our sprite image. Our le XL is used to help render things quickly since they're going to be moving. Nothing else needs to change. The image is still a surface, except now it has a picture painted on top of it. You still use it in the same way you finish adding Objects.

Now, for the changes to the Enemy Class. We're once again adding our image using Pygame.imag. load by entering the file name for the enemy. Remember that it uses a .super to call the INIT Method of spread, which we can then use to add our image.

Next, let's add the uses set color key to indicate the color Pygame will render as transparent. In this case, we choose white because that's the background color of the jet image.

To keep the game fun, we want the starting position and speed to be different each time it's played. Remember the RANDOM FUNCTION. Let's add this code after set color key inside the enemy class.

Here's what you should have in your editor. The program now should be the same game you had before. Except now you've added some nice graphic skins along with your images. Make sure to test your code.

Nice job. I'll see you in the next lesson.