FileNotFoundError: No such file or directory
FileNotFoundError: No such file or directory
我是编程新手,只是按照 python 在线提供的步骤构建了一个简单的游戏。我正在使用 VSC。
这是我的代码:
import pygame
pygame.init()
# size screen
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))
# title
pygame.display.set_caption("Practice")
#load strite
character = pygame.image.load(('character.png'))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = screen_width / 2 - character_width
character_y_pos = screen_height - character_height
# event loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((117,217,242))
screen.blit(character, (character_x_pos, character_y_pos))
pygame.display.update()
# end game
pygame.quit()
我得到了输出:
pygame 2.0.0.dev10 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/Users/suyeon/Documents/pythonWorkspace/pygame_basic/3_main_strite.py", line 15, in <module>
character = pygame.image.load(('character.png'))
FileNotFoundError: No such file or directory.
我试过 character =
pygame.image.load(('C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png'))
pygame.image.load(("C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png"))
pygame.image.load(('C:\Users\suyeon\Documents\pythonWorkspace\pygame_basic\character.png'))
但他们都犯了同样的错误
character.png 在同一文件夹中。
也许你应该尝试使用
pygame.image.load((r'C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png'))
同时指定路径。
无论路径如何,从 docs 我认为您不应该在双括号中传递路径。
pygame.image.load(r'C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png')
我会检查您是否可以使用 os
模块访问文件,您也可以传递一个文件对象,而不是路径。
我认为您的终端不在同一个目录上工作。
使您的 python 目录和终端目录相同
我是编程新手,只是按照 python 在线提供的步骤构建了一个简单的游戏。我正在使用 VSC。
这是我的代码:
import pygame
pygame.init()
# size screen
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))
# title
pygame.display.set_caption("Practice")
#load strite
character = pygame.image.load(('character.png'))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = screen_width / 2 - character_width
character_y_pos = screen_height - character_height
# event loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((117,217,242))
screen.blit(character, (character_x_pos, character_y_pos))
pygame.display.update()
# end game
pygame.quit()
我得到了输出:
pygame 2.0.0.dev10 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "/Users/suyeon/Documents/pythonWorkspace/pygame_basic/3_main_strite.py", line 15, in <module>
character = pygame.image.load(('character.png'))
FileNotFoundError: No such file or directory.
我试过 character =
pygame.image.load(('C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png'))
pygame.image.load(("C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png"))
pygame.image.load(('C:\Users\suyeon\Documents\pythonWorkspace\pygame_basic\character.png'))
但他们都犯了同样的错误
character.png 在同一文件夹中。
也许你应该尝试使用
pygame.image.load((r'C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png'))
同时指定路径。
无论路径如何,从 docs 我认为您不应该在双括号中传递路径。
pygame.image.load(r'C:/Users/suyeon/Documents/pythonWorkspace/pygame_basic/character.png')
我会检查您是否可以使用 os
模块访问文件,您也可以传递一个文件对象,而不是路径。
我认为您的终端不在同一个目录上工作。 使您的 python 目录和终端目录相同