pygame.error: File is not a Windows BMP file on m1 Mac running Mac os Monteray

pygame.error: File is not a Windows BMP file on m1 Mac running Mac os Monteray

我环顾四周,但没有找到任何有效的解决方案。

我正在使用教程学习 pygame:https://www.youtube.com/watch?v=jO6qQDNa2UY&list=WL&index=44&t=1379s

但是当我尝试 link 图像时出现错误:pygame.error:文件不是 Windows BMP 文件。

除了再次重新安装 pygame(上一期)或重新安装 python.

之外,我已经尝试了所有方法

这是我的代码

import pygame
import os

pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game")

WHITE = (255,255,255)
RED = (255,0,0)

FPS = 60

YELLOW_SPACESHIP_IMAGE = pygame.image.load(
    os.path.join('Assets', 'spaceship_yellow.png')).convert()
RED_SPACESHIP_IMAGE = pygame.image.load(
    os.path.join('Assets', 'spaceship_red.png')).convert()

def draw_window():
    WIN.fill((RED))
    WIN.blit(YELLOW_SPACESHIP_IMAGE, (300,100))
    pygame.display.update()

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        
        draw_window()
    pygame.QUIT()

if __name__ == "__main__":
    main()

您尝试加载 spaceship_yellow.pngspaceship_red.png,从扩展判断是 PNG 而不是 BMP,您的错误

pygame.error: File is not a Windows BMP file

建议 pygame 需要 BMP 文件。 pygame.image docs

The image module is a required dependency of pygame, but it only optionally supports any extended file formats. By default it can only load uncompressed BMP images. When built with full image support, the pygame.image.load() function can support the following formats.

然后枚举格式,显然你没有完整的图像支持,我认为最简单的解决方案是将你必须的 PNG 转换为未压缩的 BMP 图像格式。

将图像转换为 BMP 确实有效,但我宁愿仍然拥有 pygame 的全部功能。我所做的最终使它工作的是来自一个线程 here。我把它放在终端中让它工作:

python3 -m pip install git+https://github.com/nelsonlove/pygame.git

这会将您的 pygame 更新到版本 2.0.2.dev1,针对 m1 Mac 进行了修改。