简体   繁体   English

Pygame 错误:FileNotFoundError:没有这样的文件或目录

[英]Pygame Error : FileNotFoundError: No such file or directory

I'm new to programming and just following the steps provided online to make a small game in pygame to learn.我是编程新手,只是按照网上提供的步骤在 pygame 中制作一个小游戏来学习。

I recently have a problem when I wanted to add an image.我最近想添加图像时遇到问题。

Here is my code.这是我的代码。

    #sprite
from turtle import Screen
import pygame
import random
import os 

WIDTH=500
HEIGHT=600
#遊戲初始化 and 創建視窗
pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("spacegame")
background_img=pygame.image.load(os.path.join("img","background.png")).convert()
screen.blit(background_img,(0,0))

The trouble come when I run it, and they say:当我运行它时,麻烦就来了,他们说:

    Traceback (most recent call last):
  File "d:\python\test.py", line 13, in <module>
    background_img=pygame.image.load(os.path.join("img","background.png")).convert()
FileNotFoundError: No file 'img\background.png' found in working directory 'D:\img'.
PS D:\img>

You are already in the 'img' directory.您已经在“img”目录中。

So you can use the following line to get the image:因此,您可以使用以下行来获取图像:

background_img=pygame.image.load("background.png").convert()

It is not enough to put the files in the same directory or sub directory.将文件放在同一目录或子目录中是不够的。 You also need to set the working directory.您还需要设置工作目录。 A file path has to be relative to the current working directory.文件路径必须相对于当前工作目录。 The working directory is possibly different to the directory of the python script.工作目录可能与 python 脚本的目录不同。
The name and path of the file can be retrieved with __file__ .可以使用__file__检索文件的名称和路径。 The current working directory can be get by os.getcwd() and can be changed with os.chdir(path) .当前工作目录可以通过os.getcwd()获取,也可以通过os.chdir(path)进行更改。
Put the following at the beginning of your code to set the working directory to the same as the script's directory:将以下内容放在代码的开头以将工作目录设置为与脚本的目录相同:

import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法打开资源文件,pygame 错误:“FileNotFoundError:没有这样的文件或目录。” - Could not open resource file, pygame error: "FileNotFoundError: No such file or directory." Pygame 错误:FileNotFoundError:没有这样的文件或目录。 他们已经在同一个文件夹中 - Pygame Error : FileNotFoundError: No such file or directory. They are already in the same folder FileNotFoundError: 没有这样的文件或目录错误 - FileNotFoundError: No such file or directory ERROR python_pygame - FileNotFoundError:没有这样的文件或目录 - python_pygame - FileNotFoundError: No such file or directory 错误:FileNotFoundError:[Errno 2]没有这样的文件或目录: - Error: FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError:[错误2]没有这样的文件或目录:错误 - FileNotFoundError: [Errno 2] No such file or directory: error JupyterLab 错误 FileNotFoundError: [Errno 2] 没有那个文件或目录 - JupyterLab Error FileNotFoundError: [Errno 2] No such file or directory Python 错误 FileNotFoundError: [Errno 2] 没有这样的文件或目录 - Python Error FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError: [Errno 2] 没有这样的文件或目录 Azure Python 错误 - FileNotFoundError: [Errno 2] No such file or directory Azure Python error Python 错误:FileNotFoundError: [Errno 2] 没有那个文件或目录 - Python error: FileNotFoundError: [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM