我的字体出现错误,我不确定为什么。我已将我的字体放在同一个文件夹中,但它说的是以下内容
I am getting an error with my font, and I am not sure why. I have put my font in the same folder, but it is saying the following
我的字体出现错误,我不确定为什么。我把我的字体放在同一个文件夹中,错误如下:
PS C:\Users\USERNAME\Desktop\Coding\Python\Advanced> & "C:/Users/USERNAME/AppData/Local/Programs/Python/Python39/python.exe" "c:/Users/USERNAME/Desktop/Coding/Python/Advanced/Snake Game/main.py"
pygame 2.1.2 (SDL 2.0.18, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
800 600
Traceback (most recent call last):
File "c:\Users\USERNAME\Desktop\Coding\Python\Advanced\Snake Game\main.py", line 3, in <module>
myGame = Game()
File "c:\Users\USERNAME\Desktop\Coding\Python\Advanced\Snake Game\game.py", line 25, in __init__
self.font = pygame.font.Font('./Roboto-Regular.ttf',20)
FileNotFoundError: [Errno 2] No such file or directory: './Roboto-Regular.ttf'
PS C:\Users\USERNAME\Desktop\Coding\Python\Advanced>
以下是我正在使用的文件夹的截图。我在网上查了一下,跟输入字母dir有关,然后是其他的,不知道怎么弄。
SCREENSHOT OF FOLDER THAT THE FONT DOCUMENT IS IN (which is the same folder as all other program files)
进入dir得到的目录如下:
Directory: C:\Users\USERNAME\Desktop\Coding\Python\Advanced
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2022-01-28 6:52 PM Class 1_Done
d----- 2022-02-09 5:00 PM Class 2_Done
d----- 2022-02-11 5:35 PM Class 3_Done
d----- 2022-02-18 6:12 PM Class 4_Done
d----- 2022-03-02 5:27 PM Class 5_Done
d----- 2022-03-04 6:36 PM Class 6_Done
d----- 2022-03-10 1:00 AM Snake Game
d----- 2022-03-04 6:12 PM zExamples
d----- 2022-02-02 5:46 PM zFonts_Roboto
-a---- 2022-03-02 5:57 PM 449 START FOR ALL CHALLENGES.py
PS C:\Users\USERNAME\Desktop\Coding\Python\Advanced>
我要目录到“贪吃蛇游戏”文件夹
将文件放在同一目录或子目录中是不够的。您还需要设置工作目录。
资源(图像、字体、声音等)文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。
可以使用 __file__
. The current working can be changed with os.chdir(path)
.
检索文件的名称和路径
将以下内容放在代码的开头,以将工作目录设置为与脚本目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
我的字体出现错误,我不确定为什么。我把我的字体放在同一个文件夹中,错误如下:
PS C:\Users\USERNAME\Desktop\Coding\Python\Advanced> & "C:/Users/USERNAME/AppData/Local/Programs/Python/Python39/python.exe" "c:/Users/USERNAME/Desktop/Coding/Python/Advanced/Snake Game/main.py"
pygame 2.1.2 (SDL 2.0.18, Python 3.9.1)
Hello from the pygame community. https://www.pygame.org/contribute.html
800 600
Traceback (most recent call last):
File "c:\Users\USERNAME\Desktop\Coding\Python\Advanced\Snake Game\main.py", line 3, in <module>
myGame = Game()
File "c:\Users\USERNAME\Desktop\Coding\Python\Advanced\Snake Game\game.py", line 25, in __init__
self.font = pygame.font.Font('./Roboto-Regular.ttf',20)
FileNotFoundError: [Errno 2] No such file or directory: './Roboto-Regular.ttf'
PS C:\Users\USERNAME\Desktop\Coding\Python\Advanced>
以下是我正在使用的文件夹的截图。我在网上查了一下,跟输入字母dir有关,然后是其他的,不知道怎么弄。
SCREENSHOT OF FOLDER THAT THE FONT DOCUMENT IS IN (which is the same folder as all other program files)
进入dir得到的目录如下:
Directory: C:\Users\USERNAME\Desktop\Coding\Python\Advanced
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2022-01-28 6:52 PM Class 1_Done
d----- 2022-02-09 5:00 PM Class 2_Done
d----- 2022-02-11 5:35 PM Class 3_Done
d----- 2022-02-18 6:12 PM Class 4_Done
d----- 2022-03-02 5:27 PM Class 5_Done
d----- 2022-03-04 6:36 PM Class 6_Done
d----- 2022-03-10 1:00 AM Snake Game
d----- 2022-03-04 6:12 PM zExamples
d----- 2022-02-02 5:46 PM zFonts_Roboto
-a---- 2022-03-02 5:57 PM 449 START FOR ALL CHALLENGES.py
PS C:\Users\USERNAME\Desktop\Coding\Python\Advanced>
我要目录到“贪吃蛇游戏”文件夹
将文件放在同一目录或子目录中是不够的。您还需要设置工作目录。
资源(图像、字体、声音等)文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。
可以使用 __file__
. The current working can be changed with os.chdir(path)
.
检索文件的名称和路径
将以下内容放在代码的开头,以将工作目录设置为与脚本目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))