Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]

大城市里の小女人 提交于 2021-01-29 22:30:24

问题


I am working on a game in pygame and I published some of my previous games on itch.io. When I am loading image I used to do it like this:

player = pygame.image.load(r"C:\Users\user\folder\folder1\player.png")

But when I publish the game, other people can't run the game. How do I fix this?


回答1:


Place the image file in the same directory as the Python file. Change the current working directory to the directory of the file.
The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path):

import os

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)

Now you can use a relative path to load the file:

player = pygame.image.load("player.png")


来源:https://stackoverflow.com/questions/64835525/is-there-any-other-way-to-load-a-resource-like-an-image-sound-or-font-into-pyg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!