霓裳 发表于 2020-8-17 01:36:16

找不到文件路径是怎么回事

import pygame
import 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

zltzlt 发表于 2020-8-17 06:59:17

报的什么错?

Twilight6 发表于 2020-8-17 07:50:34



把路径改成这样试试看:

GRAPHICS = load_graphics(r'D:\python\Hello World\resources\graphics')


霓裳 发表于 2020-8-17 22:48:21

zltzlt 发表于 2020-8-17 06:59
报的什么错?

找不到指定文件路径:FileNotFoundError:

kogawananari 发表于 2020-8-26 23:59:30

这样GRAPHICS = load_graphics('/resources/graphics')应该可以保证在pycharm能运行
然后这样 GRAPHICS = load_graphics('./resources/graphics')应该可以保证至少cmd能运行
页: [1]
查看完整版本: 找不到文件路径是怎么回事