#-*- 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+directionif self.angle<-2:
self.angle=-2
if self.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)*2]returnspeed#滑雪者左右移动
defmove(self,speed):
self.rect.centerx=self.rect.centerx+speed[0]if self.rect.centerx<20:
self.rect.centerx=20
if self.rect.centerx>620:
self.rect.centerx=620
#创建树和小旗
classObstacleClass(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#让场景向上滚动
defscroll(self,t_ptr):
self.rect.centery=self.location[1]-t_ptr#创建一个窗口,包含随机的树和小旗
defcreate_map(start,end):
obstacles=pygame.sprite.Group()
gates=pygame.sprite.Group()
locations=[]for i in range(10):
row=random.randint(start,end)
col=random.randint(0,9)
location=[col*64+20,row*64+20]if not (location inlocations):
locations.append(location)
type=random.choice(["tree","flag"])if type=="tree":
img="skier_tree.png"
elif type=="flag":
img="skier_flag.png"obstacle=ObstacleClass(img,location,type)
obstacles.add(obstacle)returnobstacles#有移动时重辉屏幕
defanimate():
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()#切换到场景的下一屏
defupdateObstacleGroup(map0,map1):
obstacles=pygame.sprite.Group()for ob inmap0:
obstacles.add(ob)for ob inmap1:
obstacles.add(ob)returnobstacles#做好准备
pygame.init()
screen=pygame.display.set_mode([640,640])
clock=pygame.time.Clock()
skier=SkierClass()
speed=[0,6]
map_position=0
points=0
map0=create_map(20,29)
map1=create_map(10,19)
activeMap=0
obstacles=updateObstacleGroup(map0,map1)
font=pygame.font.Font(None,50)#开始主循环,每秒更新30次图形
whileTrue:
clock.tick(30)for event inpygame.event.get():if event.type==pygame.QUIT:
pygame.quit()
sys.exit()if event.type==pygame.KEYDOWN:if event.key==pygame.K_LEFT:
speed=skier.turn(-1)elif event.key==pygame.K_RIGHT:
speed=skier.turn(1)
skier.move(speed)
map_position+=speed[1]if map_position>=640 and activeMap==0:
activeMap=1map0=create_map(20,29)
obstacles=updateObstacleGroup(map0,map1)if map_position>=1280 and activeMap==1:
activeMap=0for ob inmap0:
ob.location[1]=ob.location[1]-1280map_position=map_position-1280map1=create_map(10,19)
obstacles=updateObstacleGroup(map0,map1)for obstacle inobstacles:
obstacle.scroll(map_position)
hit=pygame.sprite.spritecollide(skier,obstacles,False)ifhit:if hit[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=Trueelif hit[0].type=="flag" and nothit[0].passed:
points+=10obstacles.remove(hit[0])
score_text=font.render("Score:"+str(points),1,(0,0,0))
animate()