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 SpritesA 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 behaviorpygame.sprite.Sprite.addadd the sprite to groupspygame.sprite.Sprite.removeremove the sprite from groupspygame.sprite.Sprite.killremove the Sprite from all Groupspygame.sprite.Sprite.alivedoes the sprite belong to any groupspygame.sprite.Sprite.groupslist 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 constructorpygame.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 containspygame.sprite.Group.copyduplicate the Grouppygame.sprite.Group.addadd Sprites to this Grouppygame.sprite.Group.removeremove Sprites from the Grouppygame.sprite.Group.hastest if a Group contains Spritespygame.sprite.Group.updatecall the update method on contained Spritespygame.sprite.Group.drawblit the Sprite imagespygame.sprite.Group.cleardraw a background over the Spritespygame.sprite.Group.emptyremove 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 TypesThe 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.GroupSingleGroup container that holds a single sprite.pygame.sprite.RenderPlainSame as pygame.sprite.Grouppygame.sprite.RenderClearpygame.sprite.RenderUpdatesGroup sub-class that tracks dirty updates.pygame.sprite.OrderedUpdatesRenderUpdates sub-class that draws Sprites in order of addition.pygame.sprite.LayeredUpdatesLayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.pygame.sprite.LayeredDirtyLayeredDirty group is for DirtySprite objects. Subclasses LayeredUpdates.
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_ratioCollision detection between two sprites, using rects scaled to a ratio.pygame.sprite.collide_circleCollision detection between two sprites, using circles.pygame.sprite.collide_circle_ratioCollision detection between two sprites, using circles scaled to a ratio.pygame.sprite.collide_maskCollision 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.