python-3.x - Pygame PNG 图像看起来损坏

标签 python-3.x pygame

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

Ball.png

但是,当我启动我的程序时,出现的只是一个大小和位置正确的故障图像:

enter image description here

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

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)

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

最佳答案

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

  1. 复制this code放入名为 sdl_image.rb
  2. 的文件中
  3. 在保存该文件的目录中打开一个终端
  4. 运行brew remove sdl_image卸载库的不兼容版本
  5. 运行brew install -f sdl_image.rb安装库的兼容版本

在此之后,我的程序可以正确渲染图像。

关于python-3.x - Pygame PNG 图像看起来损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384036/

相关文章:

python - 在 Python 3 上使用 winsound 同时演奏多个音符

python - 无法构建使用 PEP 517 且无法直接安装的密码学轮子

python - 如何将图像添加到列表中

追加时Python列表更新元素

python - 在 pygame 中我可以故意触发一个事件吗?

python - 使用 PyGame 时的非阻塞串行读取线

python - 每当我尝试在 PyCharm 上配置 Python 解释器时都会收到错误

python - Python中的抽象类,添加有实现和没有实现的方法是错误的吗

python-3.x - Google-Colab 没有这样的文件或目录

python - 在pygame中用鼠标移动可缩放矩形