2016-02-13 84 views
2

我跟随this guide试图在Pygame窗口中显示一个基本的PNG图像。我的形象是一个简单的150×150绿球不透明度称为ball.png,位于同一目录作为我的Python脚本:Pygame PNG图像看起来腐败

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。

+0

什么是路径替换的东西应该做的到底是什么? – xXliolauXx

+0

@xXliolauXx'get_image'函数应该存储已经根据教程加载的图像。它似乎并不是问题,因为如果我将函数更改为仅返回pygame.image.load(path).convert(),问题仍然存在。 –

+2

Pygame + El Capitan已知有几个渲染问题。当我在我的机器上运行这段代码(Windows 10)时,一切都很正常。你可以在这里找到更多的信息:https://bitbucket.org/pygame/pygame/issues/284/max-osx-el-capitan-using-the-deprecated如何解决它。 – DJMcMayhem

回答

4

正如@DJ McGoathem所说,Pygame已经知道El Capitan的问题,因为SDL_image库的版本不同; 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的不兼容版本安装库的兼容版本

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

+0

似乎很好的回答,问题和解决方案很好地描述。 +1 – xXliolauXx