Pygame has a Group class which makes working with a group of similar objects easier. A group is like a list, with some extra functionality that’s helpful when building games
Making and filling a group
An object that will be placed in a group must inherit from Sprite.
from pygame.sprite import Sprite, Group def Bullet(Sprite): ... def draw_bullet(self): ... def update(self): ... bullets = Group() new_bullet = Bullet() bullets.add(new_bullet)
Looping through the items in a group
The sprites() method returns all the members of a group
for bullet in bullets.sprites(): bullet.draw_bullet()
Calling update() on a group
Calling update() on a group automatically calls update() on each member of the group
Removing an item from a group
It’s important to delete elements that will never appear again in the game, so you don’t waste memory and resources.