error: Couldn't open .png file (Python - Pygame library)

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: "Couldn't open image" error, error: Couldn't open Image,我尝试实施不同的提示,但 window 只是变黑并挂起。

看一下 /../../ 我看到您正在尝试根据图像的用户文件路径寻找动态文件路径?这不是它的工作方式,这就是您收到该错误的原因。它找不到 .PNG 文件,因为没有这样的东西:

 \..\..\resources\mario_sprite_by_killer828-d3iw0tz.png

可以看看这篇解释的很好的文章:Relative paths in Python

我有一些想法。

  1. 可能是您输入的图片名称有误。
  2. 或者路径中的文件夹不存在。
  3. 您可能放错了文件。
  4. 你没有考虑你的操作系统。

这里有一些建议:

  1. pygame.image.load 中使用 os.path.join('..', '..', 'resources', 'mario_sprite_by_killer828-d3iw0tz.png') 而不是反斜杠。这将正确加载 Linux、Windows 和 Mac 您可能正在使用的任何一个。

  2. 尝试使用更有条理的安排,这样就不需要那么远的回溯。

  3. 检查 and/or 重命名文件以确保您使用的是正确的路径。确保文件实际上是 png。

  4. 使用os.chdir将当前目录更改为包含图像的文件夹,然后像往常一样使用pygame.image.load('mario_sprite_by_killer828-d3iw0tz.png')

试试这些。顺便说一句,您的关键事件不会按预期工作,因为 key_press 变量没有持续更新,您只是在开始时对其进行了初始化。

通常要确保可以加载图像文件而不会出现无法找到您的图像的错误,请将该图像放在与您的程序相同的文件夹中。对于 PyCharm 用户,只需将 文件 复制并粘贴到具有正确程序的当前项目中。如果您不喜欢另一种方式,使用 os.path.join() 可能会更好。如果您有时间,请尝试前往 Pygame Docs 了解更多信息。您的问题是您使用的是反斜杠 () 而不是正斜杠 (/)。你应该改变你的路径:

\..\..\resources\mario_sprite_by_killer828-d3iw0tz.png

至:

/../../resources/mario_sprite_by_killer828-d3iw0tz.png

可以在 Stack Overflow 中的这个问题中找到此答案的来源和对您问题的真正解释:Python 2.6: "Couldn't open image" error 我希望这可以帮助你!哦,如果上面的问题 link 没有帮助,请改用 Pygame 文档 link。