为什么找不到文件?
Why is the file not found?
#A game about a glue stick that is trying to glue the world back together
import pygame
import sys
import os
import movement
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
#STAGNANT VARS
VEL = 10
#Load all image assets here
char = pygame.transform.scale(pygame.image.load(os.path.join('../img/player.png')), (100, 200))
pygame.display.set_caption('The Adventures of Glue Boy')
WINDOW_SIZE = (1920, 1080)
screen = pygame.display.set_mode(WINDOW_SIZE,0,32)
while True: #Game loop (main)
screen.fill((225,225,225))
screen.blit(char, (500, 500))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys_pressed = pygame.key.get_pressed()
movement.__init__(char, screen)
movement.control(keys_pressed, VEL)
pygame.display.update()
clock.tick(60)
那是我的代码,出于某种原因我收到了这个错误:
Traceback (most recent call last):
File "c:\Users\snowb\Desktop\Glue Game\src\main.py", line 16, in <module>
char = pygame.transform.scale(pygame.image.load(os.path.join('../img/player.png')), (100, 200))
FileNotFoundError: No such file or directory.
有人可以帮帮我吗??我已经盯着这个看了好几个小时了。这可能是显而易见的事情,但我无法弄清楚,我已经尝试了书中所有我能想到的东西。
将文件放在子目录中是不够的。您还需要设置工作目录。
资源(图像、字体、声音等)文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。您必须确保工作目录设置正确。
python 文件的名称和路径可以用 __file__
and the current working directory can be changed with os.chdir(path)
.
检索
将以下内容放在代码的开头,以将工作目录设置为与脚本目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
#A game about a glue stick that is trying to glue the world back together
import pygame
import sys
import os
import movement
clock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
#STAGNANT VARS
VEL = 10
#Load all image assets here
char = pygame.transform.scale(pygame.image.load(os.path.join('../img/player.png')), (100, 200))
pygame.display.set_caption('The Adventures of Glue Boy')
WINDOW_SIZE = (1920, 1080)
screen = pygame.display.set_mode(WINDOW_SIZE,0,32)
while True: #Game loop (main)
screen.fill((225,225,225))
screen.blit(char, (500, 500))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys_pressed = pygame.key.get_pressed()
movement.__init__(char, screen)
movement.control(keys_pressed, VEL)
pygame.display.update()
clock.tick(60)
那是我的代码,出于某种原因我收到了这个错误:
Traceback (most recent call last):
File "c:\Users\snowb\Desktop\Glue Game\src\main.py", line 16, in <module>
char = pygame.transform.scale(pygame.image.load(os.path.join('../img/player.png')), (100, 200))
FileNotFoundError: No such file or directory.
有人可以帮帮我吗??我已经盯着这个看了好几个小时了。这可能是显而易见的事情,但我无法弄清楚,我已经尝试了书中所有我能想到的东西。
将文件放在子目录中是不够的。您还需要设置工作目录。
资源(图像、字体、声音等)文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。您必须确保工作目录设置正确。
python 文件的名称和路径可以用 __file__
and the current working directory can be changed with os.chdir(path)
.
检索
将以下内容放在代码的开头,以将工作目录设置为与脚本目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))