【问题标题】:Trying to play animation in pygame causes error尝试在 pygame 中播放动画会导致错误
【发布时间】:2020-06-18 00:12:11
【问题描述】:

我正在尝试制作一个游戏,在开始屏幕中我想在按下开始时播放动画。它给了我错误“TypeError:参数 1 必须是 pygame.Surface,而不是列表”。 这就是我为动画加载图像的方式:

for i in range(3, 53):
    menuanimation.append(pygame.image.load("menu/menu" + str(i) +".jpg"))

这里是从按钮调用它的地方

    if event.type == pygame.MOUSEBUTTONDOWN:
        mouse_pos = event.pos
        if button1.collidepoint(mouse_pos):
            menuanimations()
            player.run = True
            player.start = False

这里是函数的所在

def menuanimations():
    win.blit(menuanimation, (0, 0))
    print("testing")

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    问题是您的menuanimation 变量是 表面列表,而不是表面。 Pygame 函数Surface.blit() 需要一个表面(图像)作为参数,你给它一个列表。可能您的意思是只绘制列表中的单个图像,因此需要对列表变量进行索引。

    你可能想要这样的东西:

    def menuanimations():
        global animation_frame, menuanimation
        if ( animation_frame >= len( menuanimation ) ):         # Stay within limit
            animation_frame = 0
        win.blit( menuanimation[ animation_frame ], ( 0, 0 ) )  # Index into the list
        animation_frame += 1
    

    显然你需要初始化animation_frame=0

    【讨论】:

    • 非常感谢!我只需要添加一个循环并检查动画是否在主循环中结束,然后将循环设置为 false 并开始游戏。我只希望动画播放一次。有一段时间没有使用它们了,我知道有些东西不见了。再次感谢!
    猜你喜欢
    • 2020-05-14
    • 2013-08-25
    • 2022-12-09
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多