我的字体出错了,我不知道为什么。我已将字体放在同一个文件夹中,错误如下:
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有关,然后是其他一些东西,但我不知道该如何做。
输入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>我希望目录转到“蛇游戏”文件夹
发布于 2022-03-17 05:56:33
仅仅将文件放在同一个目录或子目录中是不够的。您还需要设置工作目录。资源(图像、字体、声音等)文件路径必须相对于当前工作目录。工作目录可能与python脚本的目录不同。
可以使用__file__检索文件的名称和路径。目前的工作可以改变与os.chdir(path)。
将以下内容放在代码的开头,将工作目录设置为与脚本目录相同的目录:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))https://stackoverflow.com/questions/71505790
复制相似问题