1 条评论

  • @ 2024-10-13 11:53:59
    import pygame
    from pygame.locals import *
    from sys import exit
    import random
    # 变量
    path = 'resources/images/' # 图片保存的路径
    index = 0 # 小鸟动画切换的列表下标
    y = 300 # 小鸟的上下位置信息
    x = 0
    g = [300,300,300,300,300]
    # 初始化
    pygame.init()
    screen = pygame.display.set_mode((800, 600)) # 用pygame生成800*600的窗口
    pygame.display.set_caption('flappy bird') # 设置标题
    bg = pygame.image.load(path + 'bg_day.png') # 根据文件路径和文件名加载图片
    bg = pygame.transform.smoothscale(bg, (800, 600)) # 缩放图片大小
    birds = [ # 加载三张小鸟图片存放到列表中
        pygame.image.load(
            path + '0.png'),
        pygame.image.load(
            path + '1.png'),
        pygame.image.load(
            path + '2.png')]
    gold = pygame.image.load(path + 'gold1.png')
    pygame.key.set_repeat(1, 3) # 设置按键一次后延迟1毫秒,必须间隔1毫秒才能摁键
    # 运行时
    while True:
        for event in pygame.event.get(): 
            if event.type == QUIT:  # 如果收到退出事件,则退出程序
                exit()
            elif event.type == KEYDOWN:  # 如果有按键按下
                key = event.key
                if key == K_SPACE:  # 如果按下空格键
                    y = y - 5  # 小鸟向上移动
    
        screen.blit(bg, (0, 0))  # 将背景图片绘制到窗口上
        screen.blit(birds[index], (x, y))  # 将小鸟图片绘制到窗口上
        for i in range(5):
             screen.blit(gold, (200+i*120, g[i]))
        y = y + 2  # 小鸟向下移动,模拟重力
        x = x+2
        if x>800:
            x = 0
            for i in range(5):
                g[i] = random.randint(200,400)
        if index == 2:  # 如果动画下标等于2
            index = 0  # 重置动画下标
        else:
            index = index + 1  # 否则,动画下标加1
    
        pygame.display.update()  # 更新窗口显示
    
    
    • 1