In short, it is an animated image.
Two member variables
1) self. Image = the surface of the image to be displayed
2) self. rect = displays the area of the surface,
Common Methods for setting rect: Self. rect = self. image. get_rect ()
Then set self. rect. topleft = () to set the position in the upper left corner to set the display position of the genie on the screen.
Member Functions
The following are sprite member functions. Update is used to update sprite images and is generally overwritten.
Sprite. Update-Method to Control sprite behaviormethod to control sprite Behavior
Sprite. Add-Add the sprite to groups add the sprite to groups
Sprite. Remove-remove the sprite from groups
Remove the sprite from groups
Sprite. Kill-remove the sprite from all groups
Remove the sprite from all groups
Sprite. Alive-does the sprite belong to any groupsdoes the sprite belong to any groups
Sprite. Groups-list of groups that contain this spritelist of groups that contain this Sprite
Sprite code example
#-*-Coding: gb2312-*-import pygame, sys, randomfrom pygame. locals import * # defines the sprite class, inherits from Sprite, and overrides the update () function # The parent class constructor sprite must be executed in the initialization function. _ init _ (Self) class player (pygame. sprite. sprite): def _ init _ (self, color, topleft): pygame. sprite. sprite. _ init _ (Self) self. image = pygame. surface (20, 20) # This is the surfaceself image of each genie. image. fill (color) self. rect = self. image. get_rect () # rectangleself displayed on each sprite surface. rect. topleft = topleft # Set the position in the upper left corner of the matrix def Update (Self): # Move to the right. If it reaches the far right, speed = random starts from the left. randint (0, 10) self. rect. left + = speedif self. rect. left> 630: Self. rect. left =-10pygame. init () fspclock = pygame. time. clock () screen = pygame. display. set_mode (640,480) playergroup = pygame. sprite. group () # defines four players and adds them to the group of the genie for POs in (100,100), (200,200), (300,300), (): playergroup. add (player (0, 0), POS) while true: screen. fill (255,255,255) for event in pygame. event. get (): If event. type = quit: pygame. quit () sys. exit () # update each Sprite and display it on screen for player in playergroup: Player. update () screen. bplayer (player. image, player. rect) pygame. display. update () fspclock. tick (10)
Conclusion