【问题标题】:error: Couldn't open .png file (Python - Pygame library)错误:无法打开 .png 文件(Python - Pygame 库)
【发布时间】:2015-01-13 19:57:15
【问题描述】:
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" errorerror: Couldn't open Image,并尝试实施不同的提示,但窗口只是变黑并挂起。

【问题讨论】:

  • ... \Penaldo\game\version1 ... \Penaldo\resources 所以我希望它返回两次,然后输入资源。
  • 你试过只用全路径吗,会简单很多
  • 是的。我收到相同的错误消息,但使用完整路径。
  • 所以你一直使用r"C:\Users..." 到.png 是吗?
  • 你是对的。 " 错误:无法打开 C:\Users\username\etc\Penaldo\resources "

标签: python pygame


【解决方案1】:

我有一些想法。

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

这里有一些建议:

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

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

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

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

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

【讨论】:

  • 重装Python和Pygame终于解决了。
【解决方案2】:

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

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

你可以看看这篇解释清楚的文章:Relative paths in Python

【讨论】:

    【解决方案3】:

    通常为了确保可以加载图像文件而不会引发关于无法找到图像的错误,请将图像放在与程序相同的文件夹中。对于 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 我希望这可以帮助你!哦,如果上面的问题链接没有帮助,请改用 Pygame Docs 链接。

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 2022-06-25
      • 2014-02-08
      • 2016-05-25
      • 2019-02-25
      • 2016-04-12
      相关资源
      最近更新 更多