简体   繁体   English

Pygame - TypeError:必须是 pygame.Surface,而不是在翻转图像时列出

[英]Pygame - TypeError: must be pygame.Surface, not list when flipping an image

boss.ani = glob.glob("Mobs\Mob_2\stand_*.png")
boss.img = pygame.transform.flip(boss.ani,True,False)

I'm trying to take 5 images, boss.ani, and flip them so they face the opposite direction, this is what's wrong and I get this error:我正在尝试拍摄 5 张图像,boss.ani,然后将它们翻转,使它们面向相反的方向,这是错误的,我收到此错误:

  line 54, in __init__
    boss.img = pygame.transform.flip(boss.ani,True,False)
TypeError: must be pygame.Surface, not list

What would I have to do to flip the whole list of images?我该怎么做才能翻转整个图像列表?

I'm assuming that glob.glob creates a list of pygame.Surface objects using pygame.image.load().我假设 glob.glob 使用 pygame.image.load() 创建了一个 pygame.Surface 对象列表。

If this is in fact the case, your issue is that your calling the whole list instead of each object.如果情况确实如此,那么您的问题是您调用整个列表而不是每个对象。 To fix this, all you need to do is loop through the bos.ani list like so:为了解决这个问题,你需要做的就是像这样循环遍历 bos.ani 列表:

boss.img = [] # Creates the object to hold the transformed images
for image in boss.ani: # Loop through the boss.ani list
    boss.img.append(pygame.transform.flip(image,True,False)) # Append each transformed image to the boss.img list

Hopefully this helps.希望这会有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM