Sprites in PY Games

Updated: February 17, 2022
No Credit Card Required


We've created a window using the PYGame Library. Next is to start to fill our window with graphical images. You ready?

But before we start, let's review what we covered in the last lesson:

1. We introduced EVENTS. These allow you to use key presses, mouse movements, and even joystick movements in your game.

2. We also covered using Loops and Elif to start and end our gameplay,

3. We created the window on our screen and specified its dimensions.

Today, we're going to begin working with SPRITES. A Sprite is a standalone computer graphic element, and can be a two dimensional image, or an animated image that plays a specific role within a larger graphic environment.

For today's event, please go to Replit.com and create a new Repl and name it PGD_11_Sprites.

Let's start by initializing PY Game and setting up the environment. Please add the code below to your main.pi. Awesome now we have something to draw on.

We created a screen to be the overall Canvas for our game. We made the screen to use by calling PYgame.display.set_mode and passing the desired screen width and height.

Inside of a While Loop, we can use the SCREEN.FILL METHOD to add a background color. We then use the PYGAME.SURFACE METHOD to create a width and height for our first Sprite. We can add color to our Sprite with the SURF.FILL METHOD, similar to how we filled the screen. Recall that a surface is a rectangular object on which you can draw kind of like a blank sheet of paper.

The screen Object is a surface and you can create your own surface Objects separate from the display screen. We need to hold our Sprite in a variable. So we create “rect” and assign it with SURF.GET_RECT METHOD.

Try changing it to your own color. Oh, you can use the Google color picker to pick a color. To see it on the screen, we need to BLIT the surface onto the screen. The term BLIT stands for block transfer. Go ahead and add this code after the previous example.

FLIP updates the entire screen with everything that's been drawn since the last flip. Without the call to FLIP, nothing gets shown.

Now let's add some players to our game. PyGame provides a Sprite Class which is designed to hold one or several graphical representations of any game Object that you want to display on your screen. To use it, you create a new Class that extends Sprite. This allows you to use built in Methods.

Let's add this code just before the While Loop. Here's what your code should look like.

Use this new Class to create a new Object and change the drawing code as well. Let's create the Object “player”. This code is added after we created the Player Class.

All right, what do you think we should do next? What would you do now? If you said BLIT to the screen, you're correct. inside the While Loop, we can add the SCREEN.BLIT METHOD.

Right now our player IS just a rectangle. But we'll change that. We've set up PY Game and learn how to draw objects on the screen. Now let's learn how to make the player controllable using the keyboard. PyGame provides PYGAME.EVENT DOT.GET_PRESS METHOD which returns a DICTIONARY containing all the current keydown Events in the queue.

What's a Dictionary in Python? It's not that different from a regular dictionary, actually. A Python Dictionary is an unordered collection of ITEMS. Each Item in a Dictionary has a KEY VALUE PAIR. The KEY is an Item of data that has a name and then the VALUE is its content. For example, the the Key might be “city” and the Value might be “London”. The Python dictionary is a KEY VALUE DATABASE.

Type this code in your game loop right after the Event handling loop. This returns a Dictionary containing the keys pressed at the beginning of every frame. Next, let's write a Method in “player” to accept that Dictionary. This will define the behavior of the Sprite based on the keys that are pressed. Here's what a Method might look like. Take a moment and add this to your code.

Now we can call “update” every frame to move the player Sprite in response to key presses. Add this call right after the call to get pressed. Run the game and you should now have control over your player. Wow, that was a lot of ground to cover.

Hey, nice work. I'll see you in the next lesson.