pygame   documentation
||  Home  ||  Help Contents  ||
 
|| pygame || cdrom || constants || display || draw || event ||
|| font || image || joystick || key || mixer || mixer_music ||
|| mouse || movie || sndarray || surfarray || time || transform ||
 
|| CD || Channel || Clock || Font || Joystick || Movie ||
|| Rect || Sound || Surface ||
 
|| color || cursors || sprite ||

pygame.sprite


 
This module contains a base class for sprite objects. Also several different group classes you can use to store and identify the sprites. Some of the groups can be used to draw the sprites they contain. Lastly there are a handful of collision detection functions to help you quickly find intersecting sprites in a group.
 
The way the groups are designed, it is very efficient at adding and removing sprites from groups. This makes the groups a perfect use for cataloging or tagging different sprites. instead of keeping an identifier or type as a member of a sprite class, just store the sprite in a different set of groups. this ends up being a much better way to loop through, find, and effect different sprites. It is also a very quick to test if a sprite is contained in a given group.
 
You can manage the relationship between groups and sprites from both the groups and the actual sprite classes. Both have add() and remove() functions that let you add sprites to groups and groups to sprites. Both have initializing functions that can accept a list of containers or sprites.
 
The methods to add and remove sprites from groups are smart enough to not delete sprites that aren't already part of a group, and not add sprites to a group if it already exists. You may also pass a sequence of sprites or groups to these functions and each one will be used.
 
The design of the sprites and groups is very flexible. There's no need to inherit from the provided classes, you can use any object you want for the sprites, as long as it contains "add_internal" and "remove_internal" methods, which are called by the groups when they remove and add sprites. The same is true for containers. A container can be any python object that has "add_internal" and "remove_internal" methods that the sprites call when they want add and remove themselves from containers. The containers must also have a member named "_spritegroup", which can be set to any dummy value.
 

Group - (class) - the Group class is a container for sprites
Group.add - add sprite to group
Group.copy - copy a group with all the same sprites
Group.empty - remove all sprites
Group.has - ask if group has sprite
Group.remove - remove sprite from group
Group.sprites - return an object to loop over each sprite
Group.update - call update for all member sprites
GroupSingle - (class) - a group container that holds a single most recent item
RenderClear - (class) - a group container that can draw and clear its sprites
RenderClear.clear - erase the previous position of all sprites
RenderClear.draw - draw all sprites onto a surface
RenderPlain - (class) - a sprite group that can draw all its sprites
RenderPlain.draw - draw all sprites onto a surface
RenderUpdates - (class) - a sprite group that can draw and clear with update rectangles
RenderUpdates.draw - draw all sprites onto the surface
Sprite - (class) - the base class for your visible game objects.
Sprite.add - add a sprite to container
Sprite.alive - ask the life of a sprite
Sprite.groups - list used sprite containers
Sprite.kill - end life of sprite, remove from all groups
Sprite.remove - remove a sprite from container
groupcollide - collision detection between group and group
spritecollide - collision detection between sprite and group
spritecollideany - finds any sprites that collide

Group
pygame.sprite.Group(sprite=())
 
Group.add
pygame.sprite.Group.add(sprite)
 
Group.copy
pygame.sprite.Group.copy() -> Group
 
Group.empty
pygame.sprite.Group.empty()
 
Group.has
pygame.sprite.Group.has(sprite) -> bool
 
Group.remove
pygame.sprite.Group.remove(sprite)
 
Group.sprites
pygame.sprite.Group.sprites() -> iterator
 
Group.update
pygame.sprite.Group.update(...)
 
GroupSingle
pygame.sprite.GroupSingle()
 
RenderClear
pygame.sprite.RenderClear()
 
RenderClear.clear
pygame.sprite.RenderClear.clear(surface, bgd)
 
RenderClear.draw
pygame.sprite.RenderClear.draw(surface)
 
RenderPlain
pygame.sprite.RenderPlain(sprite=())
 
RenderPlain.draw
pygame.sprite.RenderPlain.draw(surface)
 
RenderUpdates
pygame.sprite.RenderUpdates()
 
RenderUpdates.draw
pygame.sprite.RenderUpdates.draw(surface)

 
Sprite
pygame.sprite.Sprite(group=())
 
Sprite.add
pygame.sprite.Sprite.add(group)
 
Sprite.alive
pygame.sprite.Sprite.alive() -> bool
 
Sprite.groups
pygame.sprite.Sprite.groups() -> list
 
Sprite.kill
pygame.sprite.Sprite.kill()
 
Sprite.remove
pygame.sprite.Sprite.remove(group)
 
groupcollide
pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb) -> dict
 
spritecollide
pygame.sprite.spritecollide(sprite, group, dokill) -> list
 
spritecollideany
pygame.sprite.spritecollideany(sprite, group) -> sprite