Pygame Learning Notes (5): Game elf _python

Source: Internet
Author: User

It is said that in the Nintendo FC era, the role of elves is very large, but at that time only know how to play Super Mary, the Soul Dou Luo, but the elves do not know. Pygame.sprite.Sprite is a class that is used to implement the sprite in Pygame, and when used, it is not necessary to instantiate it, just inherit him, and write his own class on demand, so it is very simple and practical.

First, what is the Spirit

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

The main and commonly used variables in Sprite are as follows : more detailed See http://www.pygame.org/docs/ref/sprite.html
Self.image this is responsible for showing what. such as Self.image=pygame. Surface ([X,y]) indicates that the wizard is a x,y-size pitch, and self.image=pygame.image.load (filename) indicates that the wizard invokes this picture file that displays filename.

Copy Code code as follows:

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

Fills red with the x,y spacing.
Self.rect is responsible for showing where. In general, first use Self.rect=self.image.get_rect () to obtain the image size, and then give Self.rect set 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, said the top and bottom.
Self.update is responsible for bringing the wizard into effect.
Sprite.add Add the wizard to the group.
Sprite.remove removed from group
Sprite.kill Remove all sprites from groups
Sprite.alive judge whether the elves belong to groups

Third, the establishment of a simple wizard

All sprites are inherited from the pygame.sprite.Sprite when they are established.

(1) Do a wizard, draw a width of 30, 30 of the distance shape, the specific code is as follows:

Copy 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's a step by step analysis, pygame.sprite.sprite.__init__ (self) completes the initialization. Self.image = Pygame. Surface ([30,30]) defines a pitch Surface that displays 30*30. Self.image.fill (color) fills colors with colour. Self.rect=self.image.get_rect () gets the self.image size. Self.rect.topleft=initial_position to determine the position of the upper left corner, of course, you can also use TopRight, Bottomrigh, bottomleft to determine the location of the other corners. Wizard display, in a 640*480 size of the white form [50,100] to draw a 30*30 size of the red shape, complete code as follows:

Copy 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 a wizard, display content for a picture, here used to use the car picture for example, the code is as follows:
Copy 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.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 the self.image is defined as pygame.image.load (filename), used to display the filename file, the code uses the Ok1.jpg file, and defines the position of the right corner of the trolley is [150,100 ]。

Third, the Study Wizard group

(1) Use the wizard to put multiple images on the screen, this method does not use the concept of the wizard group, but use the list to generate each wizard. Car,screen.blit (Carlist.image,carlist.rect), which is used to store different locations, displays each sprite Cargroup. See the code specifically:

Copy 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 concrete effect See figure:

(2) Use the wizard group to implement multiple images. The above genie is in a list, very convenient, is a bit less useful. In addition to the Elves, Pygame also provides the wizard group, which is ideal for handling wizard lists, adding, removing, drawing, and updating methods. Specifically as follows: http://www.pygame.org/docs/ref/sprite.html
Group.sprites Wizard Group
Group.copy replication
Group.add add
Group.remove Removal
Group.has Judge Wizard Group members
Group.update Update
Group.draw bit block display
Group.clear-Drawing background
Group.empty empty.
This is also the example above, implemented using the wizard group.

Copy 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 in [150,200],[350,360],[250,280] three locations show three cars, the difference is the first one with a list, the second is the wizard group. The difference is in a few words, one is cargroup=pygame.sprite.group () Definition Cargroup for the wizard group, and the second is Cargroup.add (car (Fi,lo)) with add instead of append, three is for Carlist in Cargroup.sprites () This sentence shows the wizard one by one, here is a try, directly with the For Carlist in Cargroup is also possible. The Code for the wizard group is highly optimized, often faster than the list. Insertions and deletions are common operations, and code can avoid recurring consumption within loops.

Four, animation

The use of the wizard group animation will be more convenient, here we first let the above three cars to move up.
(1) Three cars move at different speeds, using the value of random.choice randomly generated [ -10,-1] as speed to move the car from the bottom up, and when it reaches the top, it appears from the base. The code is as follows:

Copy 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) through the key to control about three cars around the car, press left to move left, when the most left, no longer move, press the right button to move to the right, when the most right, no longer move. The specific code is as follows:

Copy 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 ()

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.