1 / 13

Sprites

Sprites. A sprite is a 2D image or animation that is integrated into a larger scene.

yvon
Download Presentation

Sprites

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.

  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.

  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.

  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:

  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.

More Related