Sprites II

In previous chapter I have presented Sprite, DirtySprite and Group classes here you will found a bit better classes than Group that uses from extra attributes of DirtySprite and render sprites with some order.

RenderUpdates

pygame.sprite.RenderUpdates

pygame.sprite.RenderUpdates(*sprites) This class is deriven from Group and have extended draw() method.

pygame.sprite.RenderUpdates.draw

RenderUpdates.draw() draws all sprites, and return list of rect's that have changed so you can pass that list to pygame.display.update. It will improve speed on software renders. If you have animated background you will gain no speed.

OrderedUpdates

pygame.sprite.OrderedUpdates

pygame.sprite.OrderedUpdates draws in order of adding sprites to this class.

LayeredClass

pygame.sprite.LayeredUpdates

pygame.sprite.LayeredUpdates(*spites, **kwargs) this is class that actually use DirtySprite, kwargs can be "default_laye" so all layers go to it or "layer" so only passed will go to it and any futhers go to layer number 0 (default one).

pygame.sprite.LayeredUpdates.add

LayeredUpdates.add(*sprites, **kwargs) work the same as constructor

pygame.sprite.LayeredUpdates.sprites

LayeredUpdates.sprites() return sprites

pygame.sprite.LayeredUpdates.draw

LayeredUpdates.draw(surface) draw all sprites in right order onto passed surface

pygame.sprite.LayeredUpdates.get_sprites_at

LayeredUpdates.get_sprites_at(pos) return all sprites in this point first will be bottom to top.

pygame.sprite.LayeredUpdates.get_sprite

LayeredUpdates.get_sprite(index): return sprite. Raises IndexOutOfBounds if the index is not within range

pygame.sprite.LayeredUpdates.remove_sprites_of_layers

LayeredUpdates.remove_sprites_of_layer(layer_nr): return sprites from given layer.

pygame.sprite.LayeredUpdates.layers

LayeredUpdates.layers() return list of layers sorted bottom up.

pygame.sprite.LayeredUpdates.get_layer_of_sprite

LayeredUpdates.get_layer_of_sprite(sprite) return layer of sprite.

pygame.sprite.LayeredUpdates.get_top_layer

LayeredUpdates.get_top_layer() return top layer

pygame.sprite.LayeredUpdates.get_bottom_layer

LayeredUpdates.get_bottom_layer() return layer

pygame.sprite.LayeredUpdates.move_to_front

LayeredUpdates.move_to_front(sprite) put sprite is top layer.(added at the end of that layer)

pygame.sprite.LayeredUpdates.move_to_back

LayeredUpdates.move_to_back(sprite) moves sprite behind others

pygame.sprite.LayeredUpdates.get_top_sprite

LayeredUpdates.get_top_sprite() return sprite

pygame.sprite.LayeredUpdates.get_sprites_from_layer()

LayeredUpdates.get_sprites_from_layer(layer_nr) returns sprites in order they were added.

pygame.sprite.LayeredUpdates.switch_layer

LayeredUpdates.switch_layer(layer1, layer2) layers must exist!! it is not checked.

LayeredDirty

pygame.sprite.LayeredDirty

LayeredDirty(*sprites, *kwagrs)
This group requires pygame.sprite.DirtySprite or any sprite that has the following attributes: image, rect, dirty, visible, blendmode
It uses the dirty flag technique and is therefore faster than the pygame.sprite.RenderUpdates if you have many static sprites. It also switches automatically between dirty rect update and full screen drawing, so you do no have to worry what would be faster.

pygame.sprite.LayeredDirty.draw

LayeredDirty.draw(surface, bgd=None) draws sprites, background can be also specified

pygame.sprite.LayeredDirty.clear

LayeredDirty.clear(surf, bgd) clear sprites with bgd color

pygame.sprite.LayeredDirty.repaint_rect

LayeredDirty.repaint_rect(rect) repaint rect area on the screen

pygame.sprite.LayeredDirty.get_clip

LayeredDirty.get_clip() return Rect

pygame.sprite.LayeredDirty.change_layer

LayeredDirty.change_layer(sprite, new_layer) layer must be in render it is not checked

pygame.sprite.LayeredDirty.set_timing_treshold

LayeredDirty.set_timing_treshold(times_in_ms) if fps is better than specified time fullscreen is entered

pygame.sprite.GroupSingle

pygame.sprite.GroupSingle(sprite=None): return GroupSingle
The GroupSingle container only holds a single Sprite. When a new Sprite is added, the old one is removed.