Pygame 不会 blit 图像列表
Pygame will not blit lists of images
我开始在 python 中使用 pygame 构建一个简单的 window 来遍历图像列表并将它们 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
https://www.pygame.org/docs/ref/surface.html?highlight=blit#pygame.Surface.blit
请务必查看上面的 link 部分文档,但可能很难找到答案
回答
如果您尝试按序列进行 blit,请尝试使用 blits
对于
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()
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()
的要求添加目的地
我开始在 python 中使用 pygame 构建一个简单的 window 来遍历图像列表并将它们 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
https://www.pygame.org/docs/ref/surface.html?highlight=blit#pygame.Surface.blit
请务必查看上面的 link 部分文档,但可能很难找到答案
回答 如果您尝试按序列进行 blit,请尝试使用 blits
对于
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()
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()
的要求添加目的地