Pygame Study Notes (5): Game Wizard

Source: Internet
Author: User
It is said that in the Nintendo FC era, the role of the elves is quite huge, but at that time only know how to play Super Mary, the soul, but the elves do not know. Pygame.sprite.Sprite is a class used in Pygame to implement the wizard, when used, do not need to instantiate it, just inherit him, and then write their own class on demand, so it is very simple and practical.

First, what is a genie

Sprites can be thought of as small pictures, a graphical object that can be moved on the screen, and can interact with other graphical objects. The sprite image can be an image drawn using the Pygame drawing function, or it can be the original image file.

second, the main and commonly used variables in the sprite are the following : more detailed See Http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
Self.image this is responsible for showing what. such as Self.image=pygame. Surface ([x, Y]) indicates that the sprite is an x, y size, self.image=pygame.image.load (filename) indicating that the sprite call displays the filename of this image file.
Copy the Code code as follows:


Self.image.fill ([color]), which is responsible for coloring the self.image, such as Self.image=pygame. Surface ([x, Y])
Self.image.fill ([255,0,0])


Fills the x, y pitch red.
Self.rect is responsible for where to display. In general, the image size is obtained first with Self.rect=self.image.get_rect (), and then the Self.rect is set to the display position, generally with self.rect.topleft (TopRight, Bottomleft , BottomRight) to set the display position of a corner. In addition, Self.rect.top, Self.rect.bottom, Self.rect.right, Self.rect.left, respectively, indicate the upper and lower sides.
Self.update is responsible for making the Elves Act effective.
Sprite.add add sprites to group.
Sprite.remove Remove from Group
Sprite.kill Remove all sprites from the groups
Sprite.alive judge whether the elves belong to groups

Third, establish a simple wizard

All sprites are inherited from Pygame.sprite.Sprite when they are created.

(1) Make an elf, draw a width 30, height 30 of the distance shape, the specific code is as follows:

Copy the Code code as follows:


Class Temp (Pygame.sprite.Sprite):
def __init__ (self,color,initial_position):
Pygame.sprite.sprite.__init__ (self)
Self.image = Pygame. Surface ([30,30])
Self.image.fill (color)
Self.rect=self.image.get_rect ()
Self.rect.topleft=initial_position

Here, stepping through the analysis, pygame.sprite.sprite.__init__ (self) completes the initialization. Self.image = Pygame. Surface ([30,30]) defines a 30*30 surface that shows the image. Self.image.fill (color) fills colors with color. Self.rect=self.image.get_rect () gets the self.image size. Self.rect.topleft=initial_position determine the upper-left corner of the display, of course, you can also use TopRight, Bottomrigh, bottomleft to determine the location of a few other corners respectively. The sprite's display, in a 640*480 size white form [50,100], draws a 30*30 size red pitch, complete with the following code:
Copy the Code code as follows:


Import Pygame,sys
Pygame.init ()
Class Temp (Pygame.sprite.Sprite):
def __init__ (self,color,initial_position):
Pygame.sprite.sprite.__init__ (self)
Self.image = Pygame. Surface ([30,30])
Self.image.fill (color)
Self.rect=self.image.get_rect ()
Self.rect.topleft=initial_position
Screen=pygame.display.set_mode ([640,480])
Screen.fill ([255,255,255])
B=temp ([255,0,0],[50,100])
Screen.blit (B.image,b.rect)
Pygame.display.update ()
While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()


(2) Do an elf, display content for a certain picture, here used to use the car picture as an example, the code is as follows:
Copy CodeThe code is as follows:


Import Pygame,sys
Pygame.init ()
Class Car (Pygame.sprite.Sprite):
def __init__ (self,filename,initial_position):
Pygame.sprite.sprite.__init__ (self)
Self.image=pygame.image.load (filename)
Self.rect=self.image.get_rect ()
#self. rect.topleft=initial_position
Self.rect.bottomright=initial_position
Print Self.rect.right

Screen=pygame.display.set_mode ([640,480])
Screen.fill ([255,255,255])
Fi= ' ok1.jpg '
B=car (fi,[150,100])
Screen.blit (B.image,b.rect)
Pygame.display.update ()
While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()


This code differs from (1) in that self.image is defined as pygame.image.load (filename), which is used to display the filename file, this code uses the Ok1.jpg file, and defines the display position of the right corner of the trolley as [150,100 ]。

Third, the Learning Wizard Group

(1) Using the wizard to put multiple images on the screen, this method does not use the concept of the sprite group, but the use of the list to generate each wizard. Cargroup is used to store car,screen.blit (Carlist.image,carlist.rect) in different locations to display each sprite individually. See the code for details:

Copy the Code code as follows:


Import Pygame,sys
Pygame.init ()
Class Car (Pygame.sprite.Sprite):
def __init__ (self,filename,initial_position):
Pygame.sprite.sprite.__init__ (self)
Self.image=pygame.image.load (filename)
Self.rect=self.image.get_rect ()
Self.rect.bottomright=initial_position


Screen=pygame.display.set_mode ([640,480])
Screen.fill ([255,255,255])
Fi= ' ok1.jpg '
Locationgroup= ([150,200],[350,360],[250,280])
Cargroup=[]
For lo in Locationgroup:
Cargroup.append (Car (Fi,lo))
For Carlist in Cargroup:
Screen.blit (Carlist.image,carlist.rect)
Pygame.display.update ()
While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()


The specific effect is shown in figure:

(2) Use the sprite group to implement multiple images. There is a list of the above elves, very convenient, is a bit less useful. In addition to the Genie, Pygame also provides a sprite group, which is well suited for dealing with wizard lists, including adding, removing, drawing, and updating methods. Specific as follows: Http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
Group.sprites Elf Group
Group.copy replication
Group.add add
Group.remove Removal
Group.has Judging Sprite Group members
Group.update Update
Group.draw bit block display
Group.clear-Draw Background
Group.empty Empty
This is also the example above, which is implemented with a sprite group.

Copy the Code code as follows:


Import Pygame,sys
Pygame.init ()
Class Car (Pygame.sprite.Sprite):
def __init__ (self,filename,initial_position):
Pygame.sprite.sprite.__init__ (self)
Self.image=pygame.image.load (filename)
Self.rect=self.image.get_rect ()
Self.rect.bottomright=initial_position
Screen=pygame.display.set_mode ([640,480])
Screen.fill ([255,255,255])
Fi= ' ok1.jpg '
Locationgroup= ([150,200],[350,360],[250,280])
Cargroup=pygame.sprite.group ()
For lo in Locationgroup:
Cargroup.add (Car (Fi,lo))

For Carlist in Cargroup.sprites ():
Screen.blit (Carlist.image,carlist.rect)
Pygame.display.update ()
While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()


Two examples are displayed in [150,200],[350,360],[250,280] three locations three cars, the difference is the first use of the list, the second one is the wizard group. The difference is in a few words, one is cargroup=pygame.sprite.group () definition Cargroup for the sprite group, two is Cargroup.add (Car (Fi,lo)) with add instead of append, three is for Carlist in Cargroup.sprites () This sentence one by one shows the genie, here to try, directly with for Carlist in Cargroup is also possible. The sprite group code is highly optimized, often faster than the list. Insertions and deletions are common operations, and the code can also avoid repeated consumption in the presence of loops.

Four, animation

Using the sprite group to do animation will be more convenient, here we first let the above three cars moving up.
(1) Three cars move at different speeds, using the random.choice randomly generated values between [ -10,-1] as a speed to move the trolley from the bottom up, and when it reaches the top, it appears from the base. The code is as follows:

Copy the Code code as follows:


Import Pygame,sys
From random Import *
Pygame.init ()
Class Car (Pygame.sprite.Sprite):
def __init__ (self,filename,initial_position,speed):
Pygame.sprite.sprite.__init__ (self)
Self.image=pygame.image.load (filename)
Self.rect=self.image.get_rect ()
Self.rect.topleft=initial_position
Self.speed=speed
def move (self):
Self.rect=self.rect.move (Self.speed)
If Self.rect.bottom < 0: #当小车底部到达窗口顶部时, let the car come out from underneath
self.rect.top=480
Screen=pygame.display.set_mode ([640,480])
Screen.fill ([255,255,255])
Fi= ' ok1.jpg '
Locationgroup= ([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.group ()
For lo in Locationgroup:
Speed=[0,choice ([ -10,-1])
Cargroup.add (Car (fi,lo,speed))

While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()
Pygame.time.delay (20)
Screen.fill ([255,255,255])
For Carlist in Cargroup.sprites ():
Carlist.move ()
Screen.blit (Carlist.image,carlist.rect)
Pygame.display.update ()

(2) You can control the left and right movement of the three cars by the key, and then move to the right, when you reach the far left, no longer move, you will be moved to the far right, when you reach the rightmost, no longer move. The specific code is as follows:
Copy the Code code as follows:


Import Pygame,sys
From random Import *
Pygame.init ()
Class Car (Pygame.sprite.Sprite):
def __init__ (self,filename,initial_position,speed):
Pygame.sprite.sprite.__init__ (self)
Self.image=pygame.image.load (filename)
Self.rect=self.image.get_rect ()
Self.rect.topleft=initial_position
Self.speed=speed
def move (self):
Self.rect=self.rect.move (Self.speed)
If Self.rect.bottom < 0:
self.rect.top=480
def moveleft (self):
Self.rect.left=self.rect.left-10
If self.rect.left<0:
Self.rect.left=0
def moveright (self):
Self.rect.right=self.rect.right+10
If self.rect.right>640:
self.rect.right=640
Screen=pygame.display.set_mode ([640,480])
Screen.fill ([255,255,255])
Fi= ' ok1.jpg '
Locationgroup= ([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.group ()
For lo in Locationgroup:
Speed=[0,choice ([ -10,-1])
Cargroup.add (Car (fi,lo,speed))

While True:
For event in Pygame.event.get ():
If Event.type==pygame. QUIT:
Sys.exit ()
elif Event.type = = Pygame. KEYDOWN:
If Event.key==pygame. K_left:
For Carlist in Cargroup.sprites ():
Carlist.moveleft ()
Screen.blit (Carlist.image,carlist.rect)
If Event.key==pygame. K_right:
For Carlist in Cargroup.sprites ():
Carlist.moveright ()
Screen.blit (Carlist.image,carlist.rect)
Pygame.time.delay (20)
Screen.fill ([255,255,255])
For Carlist in Cargroup.sprites ():
Carlist.move ()
Screen.blit (Carlist.image,carlist.rect)
Pygame.display.update ()

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.