Sprite Groups
In this lesson, you’ll learn about the sprite.Group
class, and add your Player
and Enemy
objects to them. You’ll create two different Group
objects:
- The first
Group
will hold everySprite
in the game. - The second
Group
will hold just theEnemy
objects.
Here’s what that looks like in code:
82# Create the 'player'
83player = Player()
84
85# Create groups to hold enemy sprites and all sprites
86# - enemies is used for collision detection and position updates
87# - all_sprites is used for rendering
88enemies = pygame.sprite.Group()
89all_sprites = pygame.sprite.Group()
90all_sprites.add(player)
91
92# Variable to keep the main loop running
93running = True
When you call .kill()
, the Sprite
is removed from every Group
to which it belongs. This removes the references to the Sprite
as well, which allows Python’s garbage collector to reclaim the memory as necessary.
Now that you have an all_sprites
group, you can change how objects are drawn. Instead of calling .blit()
on just Player
, you can iterate over everything in all_sprites
:
114# Fill the screen with black
115screen.fill((0, 0, 0))
116
117# Draw all sprites
118for entity in all_sprites:
119 screen.blit(entity.surf, entity.rect)
120
121# Flip everything to the display
122pygame.display.flip()
Now, anything put into all_sprites
will be drawn with every frame, whether it’s an enemy or the player.
For more information about the sprite.Group
class, check out the pygame
documentation.
00:00
In this lesson, you’ll learn about using sprite groups. pygame
includes a class called the sprite.Group
. It’s a very useful class.
00:10
It allows you to hold and manage multiple Sprite
objects. It also includes methods for collision detection and makes updating positions and rendering of sprites much easier.
00:22
I’ll take you into the code where you’re going to add groups for the enemies and then an overall all_sprites
group. So, you’re going to create two groups.
00:31
The first group is going to hold every single sprite in the game, and then you’ll have a second group that’s going to hold just Enemy
objects.
00:38
So, starting at line 85, just below where you instantiate the player
, here you’re going to create those groups.
00:54
The enemies group is going to be used for collision detection and position updates. The other group is called all_sprites
and is primarily used for rendering.
01:02
So, you’re at line 88. The first group is going to be called enemies
. Again, pygame.sprite.
and here’s this one Group()
, so you’re creating an instance of the Group
class.
01:13
The other one is called all_sprites
.
01:21
Okay. So, what you do to add sprites to the group is use this method called .add()
, and then in this case, we’re going to add the object player
. Nice. Okay.
01:34
Now when you call .kill()
, which you might remember right here, you might remember setting up the .update()
so that when enemies went to the left and finally got its right edge past the edge of the screen, you would run this method called .kill()
. Well, that .kill()
is going to remove it from the group, and it’s part of PyGame’s garbage collection system, to work with Python’s garbage collection system—reclaim the memory that’s needed as necessary to go through. Now, the other thing you need to do is where you were calling .blit()
before—so again, here’s where you’ve created all the groups—you’re going to go down here where it says # Fill the screen
, and instead of drawing the player on the screen, you’re going to draw from all_sprites
. Again, player
is included in that. So to do that, you’re going to use a for
loop. for
each entity in all_sprites:
—then this will be indented—screen.blit()
the entity.surf
and the entity.rect
.
02:37
And then it’s still going to call the standard pygame.display
. Go ahead and save. This shouldn’t really have changed anything, but you can still test to make sure that everything’s still running properly.
02:49 Looks good! Okay. So, we still haven’t been generating any enemies, right? Well, that’s what’s up next.
02:57 The next lesson is going to take you into custom events.
gwhitelum on May 9, 2020
Never mind. all is well. Just had to do a spell check to see if object names are spelt the same. Sorry.
Become a Member to join the conversation.
gwhitelum on May 9, 2020
I’m getting an error at the end of lesson “Creating Sprite Groups” error as follows:
Python is asking for an iterable but Player is an object. and yes my player object is lowercase and the Player class is uppercase. (my IDE is PyCharm)
Since I’m stuck I’ll look at other tutorials.
Thanks for the help
Garth Whitelum