Pygame不会blit图像列表

2024-09-28 03:22:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始用pygame用python构建一个简单的窗口,以遍历图像列表并对其进行blit

import pygame

pygame.init()

Window = pygame.display.set_mode((800, 600))
bg = pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Examples/pygame lesson spaceinvaders/space.jpg")
clock = pygame.time.Clock()

animateNeutral = [pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/Plasma T Neutral %s.png"% frame)for frame in range (1,3)] 
animateFiring = [pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/plasma T fire %s.png"% frame)for frame in range (1,7)]

running = True
while running:
    events = pygame.event.get()
    keys = pygame.key.get_pressed()
    for event in events:             
        if event.type==pygame.QUIT or keys[pygame.K_ESCAPE]:
            pygame.quit()

    Window.blit(bg, (0,0))
    clock.tick(10)
    Window.blit(animateNeutral)
    Window.blit(animateFiring)
pygame.quit()

我试着快速制作它,这样我就可以看到它的动画效果,但它抛出了这个错误:

 Traceback (most recent call last):
  File "/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/space rpg.py", line 22, in <module>
    Window.blit(animateNeutral)
TypeError: argument 1 must be pygame.Surface, not list

Tags: inimageloadspacewindowframepygameusers
2条回答

blit函数需要一个曲面参数

您可以使用内置的itertools.cycle来创建一个生成器,该生成器将永远在图像中循环

以下是基于您的示例的一些未经测试的代码:

import itertools
import pygame
pygame.init()

Window = pygame.display.set_mode((800, 600))
bg = pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Examples/pygame lesson spaceinvaders/space.jpg")
clock = pygame.time.Clock()

animateNeutral = [pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/Plasma T Neutral %s.png"% frame)for frame in range (1,3)] 
animateFiring = [pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/plasma T fire %s.png"% frame)for frame in range (1,7)]

neutral_animations = itertools.cycle(animateNeutral)
firing_animations = itertools.cycle(animateFiring)

neutral_dest = (50, 50)
firing_dest = (200, 50)

running = True
while running:
    events = pygame.event.get()
    keys = pygame.key.get_pressed()
    for event in events:             
        if event.type==pygame.QUIT or keys[pygame.K_ESCAPE]:
            pygame.quit()

    Window.blit(bg, (0,0))
    clock.tick(10)
    Window.blit(next(neutral_animations), neutral_dest)
    Window.blit(next(firing_animations), firing_dest)
pygame.quit()

或者,您可以手动跟踪当前动画帧的索引,每帧递增一次,然后重置一次,使其大于列表的长度

编辑:根据surface.blit()的要求添加目的地

https://www.pygame.org/docs/ref/surface.html?highlight=blit#pygame.Surface.blit

一定要查看上面的链接,获取一些文档,但是可能很难找到答案

回答 如果您试图按顺序执行blit,请尝试使用blit

为了

animateNeutral = [pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/Plasma T Neutral %s.png"% frame)for frame in range (1,3)] 
animateFiring = [pygame.image.load("/Users/luke.redwine/Documents/Python/PyGame/Space Shooter Game/plasma T fire %s.png"% frame)for frame in range (1,7)]

这是一个图像列表,blits()获取一系列曲面(在您的示例中为图像) 如果要使用多个精灵,则应考虑转换曲面,如果曲面具有透明度(alpha),请使用convert_alpha()

相关问题 更多 >

    热门问题