Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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:

1 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.

2 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.

3 Sprite = pygame.sprite.Sprite(*groups)
Base class for visible game objects. The initializer can accept any number of Group instances to be added to. pygame.sprite.Sprite.update method to control sprite behavior pygame.sprite.Sprite.add add the sprite to groups pygame.sprite.Sprite.remove remove the sprite from groups pygame.sprite.Sprite.kill remove the Sprite from all Groups pygame.sprite.Sprite.alive does the sprite belong to any groups pygame.sprite.Sprite.groups list of Groups that contain this Sprite

4 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.

5 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 <image> self.image = pygame.Surface(width, height) # Get the rectangle. MUST be named <rect> self.rect = self.image.get_rect() def update(): # Used to update the object as needed. self.rect.move_ip(1,1)

6 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.sprites list of the Sprites this Group contains pygame.sprite.Group.copy duplicate the Group pygame.sprite.Group.add add Sprites to this Group pygame.sprite.Group.remove remove Sprites from the Group pygame.sprite.Group.has test if a Group contains Sprites pygame.sprite.Group.update call the update method on contained Sprites pygame.sprite.Group.draw blit the Sprite images pygame.sprite.Group.clear draw a background over the Sprites pygame.sprite.Group.empty remove all Sprites

7 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.

8 Group Types The Group class has evolved since its creation. Here is a list of currently available Group types. pygame.sprite.Group A container class to hold and manage multiple Sprite objects. pygame.sprite.GroupSingle Group container that holds a single sprite. pygame.sprite.RenderPlain Same as pygame.sprite.Group pygame.sprite.RenderClear pygame.sprite.RenderUpdates Group sub-class that tracks dirty updates. pygame.sprite.OrderedUpdates RenderUpdates 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.LayeredDirty LayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates.

9 Sprite Collisions The Sprite module provides 3 helper methods to detect collisions between sprites: spritecollide(sprite, group, dokill, collided = None) spritecollideany(sprite, group, collided = None) groupcollide(group1, group2, dokill1, dokill2, collided = None)

10 pygame.sprite.spritecollide()
spritecollide(sprite, group, dokill, collided=None) 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.

11 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.

12 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.

13 pygame.sprite.groupcollide()
groupcollide(group1, group2, dokill1, dokill2, collided=None) 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.


Download ppt "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."

Similar presentations


Ads by Google