2017-06-02 248 views
0

首先,这似乎是人们常见的问题,但帮助他们的决心对我自己没有什么帮助。预赛,我在RaspberryPi3B上运行Raspbian w/Pixel。我已阅读所有相关的pygame文档以及其他资源。这里是文档的链接; https://www.pygame.org/docs/ref/image.html#pygame.image.load错误,pygame.image.load,无法解决

我已经重新安装使用shell的一切:

$ sudo apt-get install mercurial 

$ hg clone https://bitbucket.org/pygame/pygame 

$ cd pygame 

$ sudo apt-get install libsdl-dev libsdl-image1.2-dev libsdl- 
mixer1.2-dev libsdl-ttf2.0-dev 

$ sudo apt-get install libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev 

$ sudo apt-get install python3-dev python3-numpy 

$ python3 setup.py build 
$ sudo python3 setup.py install 

所以现在开始的乐趣.. 我收到以下错误:

Traceback (most recent call last): File "/home/pi/Python_Code/Revenge of the Morning Wood.py", line 19, in lumberJack = pygame.image.load('/home/pi/Python_Code/Revenge_of_the_Morning_wood/lumberJack.png')

pygame.error: Couldn't open /home/pi/Python_Code/Revenge_of_the_Morning_wood/lumberJack.png

代码多达错误点:

import pygame 
import time 

pygame.init() 

display_width = 800 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption("Revenge of the Morning Wood") 
clock = pygame.time.Clock() 

lumberJack = pygame.image.load('/home/pi/Python_Code/Revenge_of_the_Morning_wood/lumberJack.png' 

def lumberJack(x,y): 
    gameDisplay.blit(lumberJack,(x,y)) 

x = (display_width * 0.45) 
y = (display_height * 0.8) 

它甚至不会显示未压缩的.bmp文件。我已经尝试了所有文件扩展名和所有与源代码/ image/pygame_module相关的文件位置组合。

pygame.image.get_extended()最初在python shell中弹出一个错误,但在上面详述的安装现在返回值“1”。

+0

该错误意味着无法打开该图像文件。你确定它在正确的目录(和/或正确的文件名)吗? –

+0

我试过目录的所有组合。我的第一个想法是认为pygame已经损坏,但从另一个来源的第二次安装排除了。我尝试了几个不同的扩展名的图像。今晚我会再玩一次。你很可能是正确的,它是程序在目录中导航的问题,我似乎可以解决它。 –

+0

尝试使用项目的文件结构编辑您的问题。 –

回答

0

期待已久的决心!我完全删除了图像扩展名。确保图像位于pygame目录中:

lumber_Jack = pygame.image.load('/home/pi/pygame/lumberJack') 

这实际上是在前面包含的文档中提到的,但我忽略了它。 https://www.pygame.org/docs/ref/image.html#pygame.image.load

Pygame will automatically determine the image type (e.g., GIF or bitmap) and create a new Surface object from the data. In some cases it will need to know the file extension (e.g., GIF images should end in ”.gif”).

  • 对于那些你想知道为什么我改变了我的变量的名称。我还注意到我的变量伐木工人与我的伐木工人功能共享同一个名称的问题,但这不会导致问题。对于有此问题的任何其他人,请放弃.PNG,.BMP,JPG等。