父与子的编程游戏——滑雪者游戏

听从作者建议亲手键入代码,100行代码左右。运行时有几个小错误,成功修改,看到运行界面成功出现时,还蛮有成就感的。

import pygame,sys,random
skier_images = ["skier_down.png","skier_right1.png","skier_right2.png","skier_left2.png","skier_left1.png"]
class SkierClass(pygame.sprite.Sprite):
#创建滑雪者
    def __init__(self):
        #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 = 0
    def turn(self,direction):#滑雪者转向
        self.angle = self.angle + direction
        if self.angle < -2: self.angle = -2
        if self.angle >  2: self.angle =  2
        center = 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]
        return speed
    def move(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
class ObstacleClass(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.rect = self.image.get_rect()
        self.rect.center = location
        self.type = type
        self.passed = False
    def update(self):
        global speed
        self.rect.centery -= speed[1]#让场景向上滚
        if self.rect.centery < -32:
            self.kill()#删除从屏幕上方滚下的障碍物
def create_map():#创建一个窗口,包含随机的树和小旗
    global obstacles
    locations = []
    for i in range(10):
        row = random.randint(0,9)
        col = random.randint(0,9)
        location = [col * 64 + 20, row * 64 + 20 + 640]
        if not (location in locations):
            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)
#重绘屏幕
def animate():
    screen.fill([255,255,255])
    obstacles.draw(screen)
    screen.blit(skier.image,skier.rect)
    screen.blit(score_text,[10,10])
    pygame.display.flip()
#做好准备
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0,6]
obstacles = pygame.sprite.Group()
map_position = 0
points = 0
create_map()
font =pygame.font.Font(None, 50)
#开始主循环
running = True
while running:
    clock.tick(30)#每秒更新30次图片
    for event in pygame.event.get():#检查按键或窗口是否关闭
        if event.type == pygame.QUIT:
            running =False
        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:
        create_map()
        map_position = 0
    #检查是否碰到树或小旗子
    hit = pygame.sprite.spritecollide(skier,obstacles,False)
    if hit:
        if hit[0].type == "tree" and not hit[0].passed:
            points = points - 100
            skier.image = pygame.image.load("skier_crash.png")
            animate()
            pygame.time.delay(1000)
            skier.image = pygame.image.load("skier_down.png")
            skier.angle = 0
            speed = [0,6]
            hit[0].passed = True
        elif hit[0].type == "flag" and not hit[0].passed:
            points += 10
            hit[0].kill()
    obstacles.update()
    score_text = font.render("Score:" +str(points),1,(0,0,0))#显示得分
    animate()
pygame.quit()

运行界面:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值