无法使用Pygame加载背景图像

2024-10-04 01:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始用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

谁能帮帮我吗?顺便说一句,我的英语不是最好的,对不起;)


Tags: pathimageimportloopworldpngos错误
1条回答
网友
1楼 · 发布于 2024-10-04 01:37:18

进口:

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'))

相关问题 更多 >