CompositeSprite

Several times now I've seen people asking on #pygame about being able to manipulate multiple sprites as one entity, and have considered the use of such functionality myself. To this end, I have created this CompositeSprite class.

As with AnimSprite, there is a coloured-rectangle-themed demonstration applet contained within the module. Importing and running the test() function, or running the script directly (python animsprite.py) will show three coloured rectangles bouncing around inside each other like agitated matryoshka dolls.

Features

Member sprites can have negative coordinates and still be rendered on the composite; the internal coordinate system of a CompositeSprite encapsulates the rects of all it's member sprites. This means that, internally, the point (0, 0) is not necessarily the top-left of the sprite.

The CompositeSprite's image will reflect any changes in it's member sprites only after an update() call (which will automatically be propagated to it's member sprites). In an attempt to save processing, the CompositeSprite's image won't be changed unless at least one of the member sprites has changed since the last update() call - both their rect and image attributes are checked for equality.

Download

Latest version: 0.9.1

This class is released under the MIT license (because it's short).

Usage example

from compositesprite import CompositeSprite
 
foo = mySprite()
bar = myOtherSprite()
bob = CompositeSprite(foo, bar)
 
# CompositeSprites can be treated just like any other sprite.
grp = pygame.sprite.Group(bob)
grp.update()
grp.draw(screen)
pygame.display.flip()