【问题标题】:Pygame PNG image looks corruptPygame PNG图像看起来很损坏
【发布时间】:2016-05-24 20:49:51
【问题描述】:

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

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

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

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。

【问题讨论】:

  • 路径替换的东西到底应该做什么?
  • @xXliolauXx get_image 函数应该存储根据教程已经加载的图像。但这似乎不是问题,因为如果我将函数更改为 return pygame.image.load(path).convert(),问题仍然存在。
  • Pygame + El Capitan 已知有几个渲染问题。当我在我的机器(Windows 10)上运行此代码时,一切看起来都很好。您可以在此处查看更多信息:bitbucket.org/pygame/pygame/issues/284/… 了解如何修复它。

标签: python-3.x pygame


【解决方案1】:

正如@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. 运行brew remove sdl_image 卸载不兼容版本的库
  4. 运行brew install -f sdl_image.rb 安装兼容版本的库

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

【讨论】:

  • 似乎是一个很好的答案,问题和解决方案很好地描述了。 +1
猜你喜欢
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
相关资源
最近更新 更多