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=())`

the Group class is a container for sprites This is the base sprite group class. It does everything needed to behave as a normal group. You can easily inherit a new group class from this if you want to add more features.
You can initialize a group by passing it a sprite or sequence of sprites to be contained.

Group.add
`pygame.sprite.Group.add(sprite)`

Add a sprite or sequence of sprites to a group.

Group.copy
`pygame.sprite.Group.copy() -> Group`

Returns a copy of the group that is the same class type, and has the same contained sprites.

Group.empty
`pygame.sprite.Group.empty()`

Removes all the sprites from the group.

Group.has
`pygame.sprite.Group.has(sprite) -> bool`

Returns true if the given sprite or sprites are contained in the group

Group.remove
`pygame.sprite.Group.remove(sprite)`

Remove a sprite or sequence of sprites from a group.

Group.sprites
`pygame.sprite.Group.sprites() -> iterator`

Returns an object that can be looped over with a 'for' loop. (For now it is always a list, but newer version of python could return different objects, like iterators.)

Group.update
`pygame.sprite.Group.update(...)`

calls the update method for all sprites in the group. Passes all arguments on to the Sprite update function.

GroupSingle
`pygame.sprite.GroupSingle()`

a group container that holds a single most recent item This class works just like a regular group, but it only keeps a single sprite in the group. Whatever sprite has been added to the group last, will be the only sprite in the group.

RenderClear
`pygame.sprite.RenderClear()`

a group container that can draw and clear its sprites The RenderClear group is just like a normal group, but it can draw and clear the sprites. Any sprites used in this group must contain member elements named "image" and "rect". These are a pygame Surface and Rect, which are passed to a blit call.

RenderClear.clear
`pygame.sprite.RenderClear.clear(surface, bgd)`

Clears the area of all drawn sprites. the bgd argument should be Surface which is the same dimensions as the surface. The bgd can also be a function which gets called with the passed surface and the area to be cleared.

RenderClear.draw
`pygame.sprite.RenderClear.draw(surface)`

Draws all the sprites onto the given surface.

RenderPlain
`pygame.sprite.RenderPlain(sprite=())`

a sprite group that can draw all its sprites The RenderPlain group is just like a normal group, it just adds a "draw" method. Any sprites used with this group to draw must contain two member elements named "image" and "rect". These are a pygame Surface and Rect object that are passed to blit.
You can initialize a group by passing it a sprite or sequence of sprites to be contained.

RenderPlain.draw
`pygame.sprite.RenderPlain.draw(surface)`

Draws all the sprites onto the given surface.

RenderUpdates
`pygame.sprite.RenderUpdates()`

a sprite group that can draw and clear with update rectangles The RenderUpdates is derived from the RenderClear group and keeps track of all the areas drawn and cleared. It also smartly handles overlapping areas between where a sprite was drawn and cleared when generating the update rectangles.

RenderUpdates.draw
`pygame.sprite.RenderUpdates.draw(surface)`

Draws all the sprites onto the given surface. It returns a list of rectangles, which should be passed to pygame.display.update()

Sprite
`pygame.sprite.Sprite(group=())`

the base class for your visible game objects. The sprite class is meant to be used as a base class for the objects in your game. It just provides functions to maintain itself in different groups. A sprite is considered 'alive' as long as it is a member of one or more groups. The kill() method simply removes this sprite from all groups.
You can initialize a sprite by passing it a group or sequence of groups to be contained in.

Sprite.add
`pygame.sprite.Sprite.add(group)`

Add the sprite to a group or sequence of groups.

Sprite.alive
`pygame.sprite.Sprite.alive() -> bool`

Returns true if this sprite is a member of any groups.

Sprite.groups
`pygame.sprite.Sprite.groups() -> list`

Returns a list of all the groups that contain this sprite.

Sprite.kill
`pygame.sprite.Sprite.kill()`

Removes the sprite from all the groups that contain it. The sprite is still fine after calling this kill() so you could use it to remove a sprite from all groups, and then add it to some other groups.

Sprite.remove
`pygame.sprite.Sprite.remove(group)`

Remove the sprite from a group or sequence of groups.

groupcollide
`pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb) -> dict`

given two groups, this will find the intersections between all sprites in each group. it returns a dictionary of all sprites in the first group that collide. the value for each item in the dictionary is a list of the sprites in the second group it collides with. the two dokill arguments control if the sprites from either group will be automatically removed from all groups.

spritecollide
`pygame.sprite.spritecollide(sprite, group, dokill) -> list`

given a sprite and a group of sprites, this will return a list of all the sprites that intersect the given sprite. all sprites must have a "rect" value, which is a rectangle of the sprite area. if the dokill argument is true, the sprites that do collide will be automatically removed from all groups.

spritecollideany
`pygame.sprite.spritecollideany(sprite, group) -> sprite`

given a sprite and a group of sprites, this will return return any single sprite that collides with with the given sprite. If there are no collisions this returns None.
if you don't need all the features of the spritecollide function, this function will be a bit quicker.
all sprites must have a "rect" value, which is a rectangle of the sprite area.