2017-06-02 194 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等。