找不到文件路径是怎么回事
import pygameimport random
import os
class Game:
def __init__(self):
self.screen = pygame.display.get_surface()
self.clock = pygame.time.Clock()
def run(self):
GRAPHICS = load_graphics('resources/graphics')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
elif event.type == pygame.KEYDOWN:
self.keys = pygame.key.get_pressed()
elif event.type == pygame.KEYUP:
self.keys = pygame.key.get_pressed()
self.screen.fill((random.randint(0, 255), random.randint(0, 255),random.randint(0, 255)))
image = get_image(GRAPHICS['mario_bros'], 145, 32, 16, 16, (0, 0 ,0), 5)
self.screen.blit(image, (300, 300))
pygame.display.update()
self.clock.tick(60)
def load_graphics(path, accept=('jpg', 'png', 'bmp', 'gif')):
graphics = {}
for pic in os.listdir(path):
name, ext = os.path.splitext(pic)
if ext.lower() in accept:
img = pygame.image.load(os.path.join(path, pic))
if img.get_alpha():
img = img.convert_alpha()
else:
img = img.convert()
graphics = img
return graphics
def get_image(sheet, x, y, width, height, colorkey, scale):
image = pygame.Surface((width, height))
image.blit(sheet, (0, 0), (x, y, width, height))
image.set_colorkey(colorkey)
image = pygame.transform.scale(image, (int(width*height), int(width*height)))
return image
报的什么错?
把路径改成这样试试看:
GRAPHICS = load_graphics(r'D:\python\Hello World\resources\graphics')
zltzlt 发表于 2020-8-17 06:59
报的什么错?
找不到指定文件路径:FileNotFoundError: 这样GRAPHICS = load_graphics('/resources/graphics')应该可以保证在pycharm能运行
然后这样 GRAPHICS = load_graphics('./resources/graphics')应该可以保证至少cmd能运行
页:
[1]