1
votes

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
Do you mean yo don't want to lad the image from a file? Or do you want to load the image from a relative path? - Rabbid76
I want to load a image but when I load a image I take the whole path to the image if you know what I am saying - idk how to name myself
See the answer. - Rabbid76

1 Answers

2
votes

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