还有其他方法可以将图像,声音或字体等资源加载到Pygame中吗? [英] Is there any other way to load a resource like an image, sound, or font into Pygame?

查看:83
本文介绍了还有其他方法可以将图像,声音或字体等资源加载到Pygame中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在pygame中开发游戏,我在itch.io上发布了一些以前的游戏.当我加载图像时,我通常是这样做的:

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?

推荐答案

将图像文件与Python文件放在同一目录中.将当前工作目录更改为文件目录.
可以通过 <代码> __ file __ .当前工作目录可以通过 os.getcwd()获取.,可以通过 os.chdir(path)进行更改代码> :

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

这篇关于还有其他方法可以将图像,声音或字体等资源加载到Pygame中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆