【问题标题】:Python - error: couldn't open .png file [duplicate]Python - 错误:无法打开 .png 文件 [重复]
【发布时间】:2020-08-13 13:12:33
【问题描述】:

不知道我做错了什么,我正在学习如何使用 Python 和 PyGame 制作游戏的教程,但我得到了错误:

 pygame.error: Couldn't open resources/images/dude.png    

我的代码如下:

import pygame
from pygame.locals import *


pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width,height))

player = pygame.image.load("resources/images/dude.png")

while 1:

    screen.fill(0)

    screen.blit(player, (100,100))

    pygame.display.flip()

    for event in pygame.event.get():


        if event.type==pygame.QUIT:
            pygame.quit()
            exit(0)

完整的错误信息是:

ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
ALSA lib conf.c:4292:(_snd_config_evaluate) function
snd_func_card_driver  returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4292:(_snd_config_evaluate) function snd_func_concat
returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4292:(_snd_config_evaluate) function snd_func_refer
returned error: No such file or directory
ALSA lib conf.c:4771:(snd_config_expand) Evaluate error: No such file  or
directory
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM default
Traceback (most recent call last):
  File "/root/Documents/PyGame/game.py", line 9, in <module>
    player = pygame.image.load("resources/images/dude.png")
pygame.error: Couldn't open resources/images/dude.png

【问题讨论】:

  • player = pygame.image.load(os.path.join(".", "resources/images/dude.png")) 工作吗?

标签: python pygame


【解决方案1】:

Python 在程序所在的同一文件夹中查找文件。你写的代码说在你有你的程序的同一个文件夹中,有一个名为资源的文件夹,其中有一个名为图像的文件夹。如果是这种情况,我无能为力,但如果它不是,然后使用完整的文件位置。

【讨论】:

  • 我不确定您是否在谈论 Windows,但在 linux 上,python 会在您运行程序的文件夹中查找要打开的文件。除非它是绝对路径,否则路径应该相对于您启动程序的文件夹。
  • 还要检查大小写。这听起来很愚蠢,但我个人花了很多时间想知道为什么当文件实际调用Dude.png时它不导入dude.png
【解决方案2】:

改用相对路径(这样做总是更好):

import os

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

通过这样做,无论您将包含.py 文件的文件夹移动到何处,都可以访问其子目录(以及它们包含的任何内容),而无需修改代码。


最终代码:

import pygame
import os
from pygame.locals import *


pygame.init()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))

current_path = os.path.dirname(__file__) # Where your .py file is located
resource_path = os.path.join(current_path, 'resources') # The resource folder path
image_path = os.path.join(resource_path, 'images') # The image folder path

player_image = pygame.image.load(os.path.join(image_path, 'dude.png'))

while 1:

    screen.fill(0)

    screen.blit(player, (100,100))

    pygame.display.flip()

    for event in pygame.event.get():


        if event.type==pygame.QUIT:
            pygame.quit()
            exit(0)

对所有其他文件使用这种访​​问方法,可以避免很多问题。

【讨论】:

    【解决方案3】:

    要让它在没有相对路径的情况下工作,您需要将您创建的 .py 文件放在 pygame 文件夹中。资源文件夹也应该在那里。

    【讨论】:

      【解决方案4】:

      您需要做的就是将此图片放在您的代码文件所在的同一文件夹中。但这是针对 Linux 的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        • 2011-04-17
        • 1970-01-01
        • 2021-04-20
        • 2021-05-03
        • 1970-01-01
        相关资源
        最近更新 更多