【问题标题】:FileNotFound error when loading assets in Pygame [duplicate]在 Pygame 中加载资产时出现 FileNotFound 错误 [重复]
【发布时间】:2022-01-20 02:03:48
【问题描述】:

我正在学习如何使用 Pygame,当我加载要在我的项目中使用的资产时,它给了我一个 FileNotFound 错误。它给我一个错误的代码是NORMAL_BINGUS = pygame.image.load(os.path.join('assets', 'Bingus_Normal.jpg'))

assets文件夹在我的代码所在的同一个文件夹中,图片名称和我上面输入的完全一样,所以我不知道这可能是什么原因。

【问题讨论】:

  • 代码可能在不同的文件夹中运行,它可能会在错误的文件夹中搜索资产 - 你可能需要os.path.join(BASE, 'assets','Bingus_Normal.jpg') where BASE = os.path.dirname(os.path.abspath(__file__))
  • alr,我试试看
  • 没用,还有什么?
  • 更正:它必须是dirname() 而不是basename()
  • 哦,我要改了

标签: python python-3.x pygame pygame-surface


【解决方案1】:

代码可能在不同的文件夹中运行,并且可能在错误的位置搜索资产。

您需要使用代码创建文件夹的绝对路径。

BASE = os.path.dirname(os.path.abspath(__file__))

然后使用它为每个资产创建绝对路径:

os.path.join(BASE, 'assets', 'Bingus_Normal.jpg')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多