This is a skier's game.
Skier from the top down, the way will encounter trees and flags, pick up a flag to 10 points, hit a tree buckle 100 points, you can use the left and right arrows to control the direction of skier.
Prepare material
A ready Python environment: I downloaded the python2.6 IDLE
Two pygame installation download: http://www.pygame.org/download.shtml, double-click Install can
Note 1 You need to select the corresponding Python path when installing
Note 2 The number of installed bits is consistent with Python, 64 to 32 bits cannot be used successfully
Verify success: Import Pygame in Python no exception, can be used correctly
Three pictures of the material, need a skier_down.png, a skier_crash.png,skier_left1.png and Skier_left2.png,skier_right1.png and Skier_right2.png, There is also a tree skier_tree.png and a flag skier_flag.png.
Four good, start to write code and then run it OK: comments add it.
#-*-coding:utf-8-*-ImportPygame,sys,random
#定义图片元素列表skier_images=["Skier_down1.png","Skier_right1.png","Skier_right2.png","Skier_left2.png","Skier_left1.png"]classSkierclass (pygame.sprite.Sprite):def __init__(self): Pygame.sprite.Sprite.__init__(self) self.image=pygame.image.load ("Skier_down.png") Self.rect=self.image.get_rect () self.rect.center=[320,100] Self.angle=0defTurn (self,direction): Self.angle=self.angle+directionifSelf.angle<-2: Self.angle=-2ifSelf.angle>2: Self.angle=2Center=Self.rect.center self.image=pygame.image.load (Skier_images[self.angle]) Self.rect=self.image.get_rect () self.rect.center=Center Speed=[self.angle,6-abs (self.angle) * *] return Speed#skiers move around defMove (self,speed): Self.rect.centerx=self.rect.centerx+Speed[0]ifSelf.rect.centerx<20: Self.rect.centerx=20ifself.rect.centerx>620: Self.rect.centerx=620#Create a tree and a small flagclassObstacleclass (pygame.sprite.Sprite):def __init__(Self,image_file,location,type): Pygame.sprite.Sprite.__init__(self) self.image_file=image_file self.image=pygame.image.load (image_file) self.location=Location Self.rect=self.image.get_rect () self.rect.center=Location Self.type=type self.passed=False#let the scene scroll up defScroll (self,t_ptr): Self.rect.centery=self.location[1]-t_ptr#Create a window that contains random trees and small flagsdefCreate_map (start,end): Obstacles=Pygame.sprite.Group () Gates=Pygame.sprite.Group () Locations=[] forIinchRange (10): Row=Random.randint (start,end) Col=random.randint (0,9) Location=[col*64+20,row*64+20] if not(Locationinchlocations): locations.append (location) type=random.choice (["Tree","Flag"]) iftype=="Tree": IMG="Skier_tree.png" eliftype=="Flag": IMG="Skier_flag.png"Obstacle=Obstacleclass (Img,location,type) Obstacles.add (obstacle)returnobstacles#There is a re-shine screen when movingdefanimate (): Screen.fill ([255,255,255]) pygame.display.update (Obstacles.draw (screen)) Screen.blit (Skier.image,skier.rect) screen.blit (score_text,[ 10,10]) Pygame.display.flip ()#switch to the next screen of the scenedefUpdateobstaclegroup (MAP0,MAP1): Obstacles=Pygame.sprite.Group () forObinchmap0:obstacles.add (OB) forObinchmap1:obstacles.add (OB)returnobstacles#Prepare forpygame.init () screen=pygame.display.set_mode ([640,640]) Clock=Pygame.time.Clock () skier=Skierclass () speed=[0,6]map_position=0points=0map0=create_map (20,29) Map1=create_map (10,19) Activemap=0obstacles=updateobstaclegroup (map0,map1) Font=pygame.font.font (none,50)#start main loop, update 30 graphs per second whileTrue:clock.tick (30) forEventinchpygame.event.get ():ifevent.type==Pygame. QUIT:
Pygame.quit () sys.exit ( )ifevent.type==Pygame. KEYDOWN:ifevent.key==Pygame. K_left:speed=skier.turn (-1) elifevent.key==Pygame. K_right:speed=skier.turn (1) Skier.move (speed) map_position+=speed[1] ifmap_position>=640 andactivemap==0:activemap=1map0=create_map (20,29) Obstacles=Updateobstaclegroup (MAP0,MAP1)ifmap_position>=1280 andActivemap==1: Activemap=0 forObinchmap0:ob.location[1]=ob.location[1]-1280map_position=map_position-1280Map1=create_map (10,19) Obstacles=Updateobstaclegroup (MAP0,MAP1) forObstacleinchObstacles:obstacle.scroll (map_position) hit=pygame.sprite.spritecollide (Skier,obstacles,false)ifHit :ifhit[0].type=="Tree" and nothit[0].passed:points=points-100Skier.image=pygame.image.load ("Skier_crash.png") animate () Pygame.time.delay (1000) Skier.image=pygame.image.load ("Skier_down1.png") Skier.angle=0 Speed=[0,6] hit[0].passed=Trueelifhit[0].type=="Flag" and nothit[0].passed:points+=10Obstacles.remove (hit[0]) Score_text=font.render ("Score:"+STR (points), 1, (0,0,0)) animate ()
5 Problems I have encountered
Issue 1 Self.image=pygame.image.load ("Skier_down.png") error:couldn ' t open skier_down.png
WORKAROUND: You need to place the PNG file in the same path as the PY code
Issue 2 The comment keyword string in python code cannot be highlighted
Workaround: Because Python code is not saved as a *.py suffix file
Issue 3 TypeError: ' Nonetype ' object is not iterable
Resolution: The corresponding call class is write return.
Issue 4 Close the game box to exit the game, but not the game box can be closed
Resolution: Add a Pygame.quit () before Sys.exit ()
Python-skier game [excerpt from. Learn to program with your child]