[英]error: Couldn't open .png file (Python - Pygame library)
import pygame
import os
import sys
import time
from pygame.locals import *
pygame.init()
#Colours here
RED = pygame.Color(150, 0, 0)
GREEN = pygame.Color( 0, 150, 0)
BLUE = pygame.Color( 0, 0, 150)
BLACK = pygame.Color( 0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
FPS = pygame.time.Clock()
WIDTH = 1024
HEIGHT = 768
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Penaldo v0.1')
#Set BACKGROUND to an instance of pygame.Surface, which will get its coordinates from the previous ones we assigned to the game's display
BACKGROUND = pygame.Surface((SCREEN.get_width(), SCREEN.get_height()))
SCREEN.blit(BACKGROUND, (0, 0))
key_press = pygame.key.get_pressed()
class Player():
def __init__(self):
#Try to load our sprite.
#'r' makes it a raw string so it ignores escape sequences
self.player = pygame.image.load(r"\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png")
self.p_rect = self.player.get_rect()
#Spawn the player just in the middle of our screen
self.p_rect.centerx = WIDTH / 2
self.p_rect.centery = HEIGHT / 2
def move(self):
if key_press[UP]:
self.p_rect.y -= 5
elif key_press[DOWN]:
self.p_rect.y += 5
elif key_press[LEFT]:
self.p_rect.x -= 5
elif key_press[RIGHT]:
self.p_rect.x += 5
while True:
for event in pygame.event.get():
if event.type == QUIT:
#Some IDLE friendliness (prevents from hanging)
pygame.quit()
sys.exit()
BACKGROUND.fill(GREEN)
p1 = Player()
# FOR THE GAME TO WORK you have to include this one inside main while-loop (no display update = no game)
pygame.display.update()
FPS.tick(40)
完整的錯誤消息是:
Traceback (most recent call last):
File "C:\Users\NZXT\Documents\GameLab\Penaldo\game\version1\Penaldo.py", line 79, in <module>
p1 = Player()
File "C:\Users\NZXT\Documents\GameLab\Penaldo\game\version1\Penaldo.py", line 49, in __init__
self.player = pygame.image.load(r"\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png")
error: Couldn't open \..\..\resources\mario_sprite_by_killer828-d3iw0tz.png
我讀了一些問題,例如Python 2.6:“無法打開圖像”錯誤 , 錯誤:無法打開圖像,並且我嘗試實現各種技巧,但窗口只是變黑並掛起。
看那個/../../我看到您正在嘗試根據用戶到圖像的文件路徑來尋找動態文件路徑? 那不是它的工作原理,這就是為什么您會收到該錯誤。 無法找到.PNG文件,因為沒有以下內容:
\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png
您可以看一下對它有很好解釋的文章: Python中的相對路徑
我有一些想法。
以下是一些建議:
而不是反斜線,使用os.path.join('..', '..', 'resources', 'mario_sprite_by_killer828-d3iw0tz.png')
內pygame.image.load
。 這將正確加載Linux,Windows和Mac(您可能正在使用的任何一個)。
嘗試使用更有條理的安排,這樣就不必回溯那么遠了。
檢查和/或重命名文件,以確保使用正確的路徑。 確保文件實際上是png。
使用os.chdir
將當前目錄更改為包含圖像的文件夾,然后pygame.image.load('mario_sprite_by_killer828-d3iw0tz.png')
使用pygame.image.load('mario_sprite_by_killer828-d3iw0tz.png')
。
試試看。 順便說一句,由於key_press變量不會持續更新,因此您的按鍵事件將無法按預期進行,您只需在開始時對其進行初始化。
通常,為了確保可以加載圖像文件而不會引起無法找到圖像的錯誤,請將該圖像與程序放在同一文件夾中。 對於PyCharm用戶,只需將文件復制並粘貼到具有正確程序的當前項目中即可。 如果您不喜歡使用os.path.join()
可能會更好。 如果有時間,請嘗試訪問Pygame文檔以獲取更多信息。 您的問題是您使用的是反斜杠()而不是使用正斜杠(/)。 您應該從以下位置更改路徑:
\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png
至:
/../../resources/mario_sprite_by_killer828-d3iw0tz.png
值得一提的是,您的問題的真正解釋可以在Stack Overflow的此問題中找到: Python 2.6:“無法打開圖像”錯誤 ,希望對您有所幫助! 哦,如果上面的問題鏈接沒有幫助,請改用Pygame Docs鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.