Image + Rect = Sprite
PyGame offer a pygame.Sprite module that contain multiple classes. The simplest are: Sprite and Group. Sprite is basicly objesct that have its image and rect, and also can draw itself on surface. Groupe is an object that groups multiple sprites and can print them all.
When inheriting this class you will mostly wont to change Sprite.update() metchod, initialize Sprite.image and Sprite.rect and also don't forget to initialize super class before adding sprite to a Group object.
pygame.sprite.Sprite(*groups) returns new Sprite
Sprite.update(*args) do nothing. Is here only to be implemented in your classes. Is called when Group.upded() is called.
Sprite.add(*groups) adds this sprite to given groups.
Sprite.remove(*groups) if sprite is a member of those groups it will be removed from them.
Sprite.kill() sprite will be removed from all groups.
Sprite.alive() return True if sprite is contained by any group.
Sprite.groups() return list of all groups that contain this sprite.
DirtySprite is a Sprite with some extra attributes:
- dirty = 1 - 0 means no repainting, 1 repaint and set to 0, 2 repaint every frame
- blendmode = 0 - used for blit, blendmodes
- source_rect = None - source rect to use that it is relative to topleft (0,0) of self.image
- visible = 1 - normally 1, if set to 0 it will not be repainted (you must set it dirty too to be erased from screen)
- layer = 0 (READONLY value, it is read when adding it to the LayeredRenderGroup, for details see doc of LayeredRenderGroup)
pygame.sprite.Group(*sprites) is a simple container available for inheriting to create more specific behavior. Constructor will take any number of Sprites. The group supports the following standard Python operations:
in test if a Sprite is contained
len the number of Sprites contained
bool test if any Sprites are contained
iter iterate through all the Sprites
Group.sprites() return all contained sprites.
Group.copy() make identical Group object with the same sprites, it also work well with subclasses if theirs constructors takes the same arguments.
Group.add(*any_number_of_sprites) adds sprites to the group(also iterators are allowed)
Group.remove(*any_number_of_sprites) remove sprites that are in group(also iterators are allowed)
Group.has(*any_number_of_sprites) test if all given sprites are in group. To test only one sprite use:
if sprite in group:
Group.update(*args) call every sprite's update method and pass to them args.
Group.draw(surface) draw all contained sprites into surface
Group.clear(surface, color) draw all contained sprites with color into surface.
Group.empty() remove all sprites from this group.