我刚刚开始使用Pygame和Pycharm编写第一款游戏。我正在尝试通过以下代码加载背景图片,但是会发生错误。
import os
import pygame
import pygame.locals
#objects (classes, functions)
#setup (run-once code)
worldx = 960
worldy = 720
fps = 40
ani = 4 #animation cycles
clock = pygame.time.Clock()
pygame.init()
world = pygame.display.set_mode([worldx,worldy])
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, 'images')
backdrop = pygame.image.load(os.path.join(image_path, 'stage.png'))
backdropbox = world.get_rect()
BLUE = (25,25,200)
BLACK = (23,23,23)
WHITE = (254,254,254)
#main loop (game loop)
这是错误消息:
pygame.error: Couldn't open C:/Users/Justus/PycharmProjects/Vulcanoreal_game.py\images\stage.png
有人可以帮我吗?顺便说一句,我的英语不是最好的,对不起;)
答案 0 :(得分:0)
导入:
import pathlib
您当前的路径应为:
current_path = pathlib.Path().absolute()
image_path = os.path.join(current_path, 'images')
backdrop = pygame.image.load(os.path.join(image_path, 'stage.png'))