Pygame PNG 图像看起来已损坏

Aar*_*sen 3 pygame python-3.x

我正在按照本指南尝试在 Pygame 窗口中显示基本的 PNG 图像。我的图像是一个简单的 150x150 绿色球,没有透明度,名为ball.png,位于与我的 Python 脚本相同的目录中:

球.png

然而,当我启动程序时,出现的只是一个大小和位置正确的有问题的图像:

在此输入图像描述

我使用此代码来显示图像(与上面链接的教程中给出的代码相同):

import pygame
import os

_image_library = {}
def get_image(path):
        global _image_library
        image = _image_library.get(path)
        if image == None:
                canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
                image = pygame.image.load(canonicalized_path)
                _image_library[path] = image
        return image

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
clock = pygame.time.Clock()

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
        screen.fill((255, 255, 255))
        screen.blit(get_image('ball.png'), (20, 20))
        pygame.display.flip()
        clock.tick(60)
Run Code Online (Sandbox Code Playgroud)

为什么会出现这种情况呢?代码有缺陷吗?我尝试用不同的 PNG 替换,但这只会改变有问题的图像的外观 - 图像仍然无法正确显示。我使用的是 OS X 10.11 El Capitan、Python 3.5.0 和 Pygame 1.9.2。

Aar*_*sen 5

正如 @DJ McGoathem 所说,由于库版本不同,Pygame 已知 El Capitan 存在问题SDL_image;Pygame 需要 1.2.10 版本,但 El Capitan 有 1.2.12。这可以通过降级这个库来解决,我就是这样做的(需要 Brew):

  1. 将此代码复制到名为的文件中sdl_image.rb
  2. 在保存该文件的目录中打开终端
  3. 运行brew remove sdl_image卸载不兼容版本的库
  4. 运行brew install -f sdl_image.rb以安装兼容版本的库

之后,我的程序正确渲染了图像。