Presentation on theme: "Sprites A sprite is a 2D image or animation that is integrated into a larger scene. Originally, sprites were created by special hardware that would super-impose."— Presentation transcript:
Sprites A sprite is a 2D image or animation that is integrated into a larger scene. Originally, sprites were created by special hardware that would super-impose one video source on top of another. This was done because computers were too slow to redraw the entire screen every time an object moved.
The pygame.sprite Module This module contains several simple classes to be used within games. The use of these classes is entirely optional when using Pygame. The Sprite class is intended to be used as a base class for the different types of objects in the game. It can't really be used on its own. There are several Group classes that help store sprites as well as simply operations such as updating and drawing sprites. This module also contains several collision functions. These help find sprites inside multiple groups that have intersecting bounding rectangles.
Sprite = pygame.sprite.Sprite(*groups) pygame.sprite.Sprite.updatemethod to control sprite behavior pygame.sprite.Sprite.addadd the sprite to groups pygame.sprite.Sprite.removeremove the sprite from groups pygame.sprite.Sprite.killremove the Sprite from all Groups pygame.sprite.Sprite.alivedoes the sprite belong to any groups pygame.sprite.Sprite.groupslist of Groups that contain this Sprite Base class for visible game objects. The initializer can accept any number of Group instances to be added to.
pygame.sprite.Sprite() (cont.) A Sprite object is only useful if used with Group containers. Groups require a Sprite derived class to override the Sprite.update() method and have valid Sprite.image and Sprite.rect attributes. When subclassing the Sprite, be sure to call the base initializer before adding the Sprite to Groups.
pygame.sprite.Sprite() (cont.) class Block(pygame.sprite.Sprite): def __init__(self, width, height): # Call the parent class constructor pygame.sprite.Sprite.__init__(self) # Create or load an image. MUST be name self.image = pygame.Surface(width, height) # Get the rectangle. MUST be named self.rect = self.image.get_rect() def update(): # Used to update the object as needed. self.rect.move_ip(1,1) class Block(pygame.sprite.Sprite): def __init__(self, width, height): # Call the parent class constructor pygame.sprite.Sprite.__init__(self) # Create or load an image. MUST be name self.image = pygame.Surface(width, height) # Get the rectangle. MUST be named self.rect = self.image.get_rect() def update(): # Used to update the object as needed. self.rect.move_ip(1,1)
Group = pygame.sprite.Group(*sprites) Simple container class to hold and manage multiple Sprite objects. The constructor takes any number of Sprite arguments to add to the Group. The Sprites in the Group are not ordered, so drawing and iterating the Sprites is in no particular order. pygame.sprite.Group.spriteslist of the Sprites this Group contains pygame.sprite.Group.copyduplicate the Group pygame.sprite.Group.addadd Sprites to this Group pygame.sprite.Group.removeremove Sprites from the Group pygame.sprite.Group.hastest if a Group contains Sprites pygame.sprite.Group.updatecall the update method on contained Sprites pygame.sprite.Group.drawblit the Sprite images pygame.sprite.Group.cleardraw a background over the Sprites pygame.sprite.Group.emptyremove all Sprites
pygame.sprite.Group (cont.) Group.update() and Group.draw() are the standout methods of the Group class: – Group.update(*args) will call the Sprite.update() method of all Sprites in the Group, provided the derived class has one defined. It will pass the argument is any to each Sprite. – There is currently no way to retrieve a return value from Sprite.update(). – Group.draw(Surface) draws each Sprite in the Group to the Surface argument, provided each Sprite object has a Sprite.image and a Sprite.rect attribute. – The Group does not keep Sprites in any order, so the draw order is arbitrary, which may give undesired results.
Group Types pygame.sprite.GroupA container class to hold and manage multiple Sprite objects. pygame.sprite.GroupSingleGroup container that holds a single sprite. pygame.sprite.RenderPlainSame as pygame.sprite.Group pygame.sprite.RenderClearSame as pygame.sprite.Group pygame.sprite.RenderUpdatesGroup sub-class that tracks dirty updates. pygame.sprite.OrderedUpdatesRenderUpdates sub-class that draws Sprites in order of addition. pygame.sprite.LayeredUpdates LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates. pygame.sprite.LayeredDirtyLayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates. The Group class has evolved since its creation. Here is a list of currently available Group types.
pygame.sprite.spritecollide() Returns a list containing all Sprites in a Group that intersect with another Sprite. Intersection is determined by comparing the Sprite.rect attribute of each Sprite. The dokill argument is a bool. If set to True, all Sprites that collide will be removed from the Group. … spritecollide(sprite, group, dokill, collided=None)
The collided Argument (cont.) All three collision methods have an optional collided argument. It is a callback function used to calculate if two sprites are colliding. If collided is not passed, all sprites must have a sprite.rect value which is used to calculate the collision.
The collided Argument (cont.) The available callbacks are: pygame.sprite.collide_rect Collision detection between two sprites, using rects. pygame.sprite.collide_rect_ratio Collision detection between two sprites, using rects scaled to a ratio. pygame.sprite.collide_circle Collision detection between two sprites, using circles. pygame.sprite.collide_circle_ratio Collision detection between two sprites, using circles scaled to a ratio. pygame.sprite.collide_mask Collision detection between two sprites, using masks.
pygame.sprite.groupcollide() This will find collisions between all the Sprites in two groups. Every Sprite inside group1 is added to the return dictionary. The value for each item is the list of Sprites in group2 that intersect. If either dokill argument is True, the colliding Sprites will be removed from their respective Group. groupcollide(group1, group2, dokill1, dokill2, collided=None)