Sprite Groups in PY Games

Updated: February 17, 2022
No Credit Card Required


Last lesson, we learned how to create Sprites. In this lesson, we're going to create groups of Sprites. These can be things like enemies in our game or magical weapons. Grouping things together makes them easier to handle when there are a bunch of Objects to deal with.

To start, let's create a new Repl and name it PGD_12_Spritegroups.

Before we begin to develop SPRITE GROUPS, let's take a minute to discuss PLAYER CONDITIONS. This means controlling where the Sprite is on the screen. Too far, left is not good, too far down is a problem. We can use Conditional Statements to check the current coordinates of our player. You can use the code IF self.rect_left is less than zero, for example, to see if the player has gone too far left. If this condition is true, then the player may no longer move in that direction.

What do you think this code block does in our game? Well, do you remember the values for screen width and screen height?

Okay, now we're ready to add some Sprite Groups. How about we start with the bad guys? We don't want all our enemies in the same place on our screen in our game flow. The PY Game Library helps solve that problem with the RANDOM FUNCTION. Random is used to get pseudo random numbers from the random module in Python, and spawn enemies in random positions.

Let's import the Random Function next. We need to create a Class so we can easily spawn more enemies of the same type. We covered Classes in past lessons if you need to review. Do you remember the init.self function? It's used to specify the specific Parameters of the individual Objects created with the Class. Here we are using it to specify the position of the enemy in a random location within the game screen, and giving the enemy random speed.

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 the screen. You should also see a Method called Update inside of your enemy class that moves the enemy Sprite by the speed times negative one.

Now let's group our Sprites and begin to have our enemies interact with our players. That'd be really cool, right? Last lesson, we created a Player Object from the Player Class. Now, we need to add enemies to our game. In order for our players to interact with our enemies, it'd be easier if we group them all together. We can also group all Sprites together to make it easier to render or load our Sprites. To group Sprites together, we create a CONTAINER as a Class. Containers are an Object that hold another Object, like a bottle holds water. We've seen Containers before. Well, for example, a Dictionary is a Container. A List is a Container or a Library is a kind of Container.

The code pygame.sprite.group is a Container Class that holds and manages multiple Sprites. So let's go ahead and create the group. Let's make enemies a container called “group” and the variable “all_sprites” into the Container. We do this because since we'll have many Sprites on the screen, it's easier to manage if they're all in the same group. The enemies have a specific job compared to other Sprites, so we need them in a group.

Then, we add our players to the container “all_sprites” with this line of code. Now we need to display our Objects on the screen. This is called RENDERING. Rendering means an image in 2d or 3d is being created. We did this in a previous lesson using BLIT. So let's use a For Loop to render our Sprites using BLIT.

Type this code into your editor. Here's what the entire code should look like.

Great. Well, we got a lot done today and I'll see you soon.