pygame documentation | || Home || Help Contents || || pygame || cdrom || constants || cursor || display || draw || || event || font || image || joystick || key || mixer || || mixer_music || mouse || movie || sndarray || surfarray || time || || transform || || CD || Channel || Clock || Font || Joystick || Movie || || Overlay || 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.
While it is possible to design sprite and group classes that don't derive from the Sprite and AbstractGroup classes below, it is strongly recommended that you extend those when you add a Sprite or Group class.

AbstractGroup | - (class) - A base for containers for sprites. It does everything
AbstractGroup.add | - add sprite to group
AbstractGroup.clear | - erase the previous position of all sprites
AbstractGroup.copy | - copy a group with all the same sprites
AbstractGroup.draw | - draw all sprites onto the surface
AbstractGroup.empty | - remove all sprites
AbstractGroup.has | - ask if group has a sprite or sprites
AbstractGroup.remove | - remove sprite from group
AbstractGroup.sprites | - get a list of sprites in the group
AbstractGroup.update | - call update for all member sprites
Group | - (class) - The basic Group class you will want to use.
GroupSingle | - (class) - A group container that holds a single most recent item.
OrderedUpdates | - (class) - RenderUpdates, but the sprites are drawn in the order they were added.
RenderClear | - (class) - The basic Group class you will want to use.
RenderPlain | - (class) - The basic Group class you will want to use.
RenderUpdates | - (class) - A sprite group that's more efficient at updating.
Sprite | - (class) - The base class for your visible game objects.
Sprite.add | - add a sprite to container
Sprite.alive | - check to see if the sprite is in any groups
Sprite.groups | - list used sprite containers
Sprite.kill | - remove this sprite 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

AbstractGroup
`pygame.sprite.AbstractGroup()`

A base for containers for sprites. It does everything needed to behave as a normal group. You can easily inherit a new group class from this, or the other groups below, if you want to add more features.
Any AbstractGroup-derived sprite groups act like sequences, and support iteration, len, and so on.

AbstractGroup.add
`pygame.sprite.AbstractGroup.add(sprite, list, or group, ...)`

Add a sprite or sequence of sprites to a group.

AbstractGroup.clear
`pygame.sprite.AbstractGroup.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.

AbstractGroup.copy
`pygame.sprite.AbstractGroup.copy()`

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

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

Draws all the sprites onto the given surface.

AbstractGroup.empty
`pygame.sprite.AbstractGroup.empty()`

Removes all the sprites from the group.

AbstractGroup.has
`pygame.sprite.AbstractGroup.has(sprite or group, ...)`

Returns true if the given sprite or sprites are contained in the group. You can also use 'sprite in group' or 'subgroup in group'.

AbstractGroup.remove
`pygame.sprite.AbstractGroup.remove(sprite, list, or group, ...)`

Remove a sprite or sequence of sprites from a group.

AbstractGroup.sprites
`pygame.sprite.AbstractGroup.sprites()`

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 iterators.) You can also iterate directly over the sprite group.

AbstractGroup.update
`pygame.sprite.AbstractGroup.update(*args)`

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

Group
`pygame.sprite.Group()`

The basic Group class you will want to use. It supports all of the above operations and methods.
The RenderPlain and RenderClear groups are aliases to Group for compatibility.

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.
You can access its one sprite as the .sprite attribute. Assigning to this attribute will properly remove the old sprite and then add the new one.

OrderedUpdates
`pygame.sprite.OrderedUpdates()`

RenderUpdates, but the sprites are drawn in the order they were added. More recently added sprites are drawn last (and so, above other sprites).

RenderClear
`pygame.sprite.RenderClear()`

The basic Group class you will want to use. It supports all of the above operations and methods.
The RenderPlain and RenderClear groups are aliases to Group for compatibility.

RenderPlain
`pygame.sprite.RenderPlain()`

The basic Group class you will want to use. It supports all of the above operations and methods.
The RenderPlain and RenderClear groups are aliases to Group for compatibility.

RenderUpdates
`pygame.sprite.RenderUpdates()`

A sprite group that's more efficient at updating. This group supports drawing to the screen, but its draw method also returns a list of the Rects updated by the draw (and any clears in between the last draw and the current one). You can use pygame.display.update(renderupdates_group.draw(screen)) to minimize the updated part of the screen. This can usually make things much faster.

Sprite
`pygame.sprite.Sprite()`

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.
You can initialize a sprite by passing it a group or sequence of groups to be contained in.
When you subclass Sprite, you must call this pygame.sprite.Sprite.__init__(self) before you add the sprite to any groups, or you will get an error.

Sprite.add
`pygame.sprite.Sprite.add(group or list of of groups, ...)`

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 of groups`

Returns a list of all the groups that contain this sprite. These are not returned in any meaningful order.

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

Removes the sprite from all the groups that contain it. The sprite still exists after calling this, 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 or list of groups, ...)`

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.