Sprites

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.

Sprite

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.

pygeme.sprite.Sprite

pygame.sprite.Sprite(*groups) returns new Sprite

pygeme.sprite.Sprite.update

Sprite.update(*args) do nothing. Is here only to be implemented in your classes. Is called when Group.upded() is called.

pygeme.sprite.Sprite.add

Sprite.add(*groups) adds this sprite to given groups.

pygeme.sprite.Sprite.remove

Sprite.remove(*groups) if sprite is a member of those groups it will be removed from them.

pygeme.sprite.Sprite.kill

Sprite.kill() sprite will be removed from all groups.

pygeme.sprite.Sprite.alive

Sprite.alive() return True if sprite is contained by any group.

pygeme.sprite.Sprite.groups

Sprite.groups() return list of all groups that contain this sprite.

DirtySprite

pygeme.sprite.DirtySprite

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)

Group

pygeme.sprite.Group

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

pygeme.sprite.Group.sprites

Group.sprites() return all contained sprites.

pygeme.sprite.Group.copy

Group.copy() make identical Group object with the same sprites, it also work well with subclasses if theirs constructors takes the same arguments.

pygeme.sprite.Group.add

Group.add(*any_number_of_sprites) adds sprites to the group(also iterators are allowed)

pygeme.sprite.Group.remove

Group.remove(*any_number_of_sprites) remove sprites that are in group(also iterators are allowed)

pygeme.sprite.Group.has

Group.has(*any_number_of_sprites) test if all given sprites are in group. To test only one sprite use:

if sprite in group:
pygeme.sprite.Group.update

Group.update(*args) call every sprite's update method and pass to them args.

pygeme.sprite.Group.draw

Group.draw(surface) draw all contained sprites into surface

pygeme.sprite.Group.clear

Group.clear(surface, color) draw all contained sprites with color into surface.

pygeme.sprite.Group.empty

Group.empty() remove all sprites from this group.