Public Group

Pygame importing images

This topic is 1835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have been importing image into pygame like this:

image = pygame.image.load("C:\\Users\\user\\Desktop\\folder\\images\\Entities\\image.png")


Yet if I were to give this game to someone they can't play it, due to having a different user name. How do I fix this?

Share on other sites

You just need to make sure you know where the root folder of your game is - i.e. make sure you know the structure of the files and folders and they are all contained in a single folder. Say you have "C:\Users\user\Desktop\Game" and the entirety of your game, including the images and code, are in Game. Say your code is in "Game\code" and your image is in "Game\images\Entities", you can load the image with the following:

image = pygame.image.load("..\\images\\Entities\\image.png")


Where the ".." tells the computer to check what directory you're in right now and go up one level - in this case, the computer sees you're in the code directory, goes up to the Game directory, then follows the remaining path. If the image is in a subdirectory of the directory the code is in, just use a single ".". If you need to go up multiple directories, add a "..\\" for each level you need to go up.

That way it doesn't matter where on the computer the entire folder is stored, as long as the files are in the same position relative to one another.

Share on other sites

You just need to make sure you know where the root folder of your game is - i.e. make sure you know the structure of the files and folders and they are all contained in a single folder. Say you have "C:\Users\user\Desktop\Game" and the entirety of your game, including the images and code, are in Game. Say your code is in "Game\code" and your image is in "Game\images\Entities", you can load the image with the following:

image = pygame.image.load("..\\images\\Entities\\image.png")


Where the ".." tells the computer to check what directory you're in right now and go up one level - in this case, the computer sees you're in the code directory, goes up to the Game directory, then follows the remaining path. If the image is in a subdirectory of the directory the code is in, just use a single ".". If you need to go up multiple directories, add a "..\\" for each level you need to go up.

That way it doesn't matter where on the computer the entire folder is stored, as long as the files are in the same position relative to one another.

Ah, I see thank you very much!

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• Forum Statistics

• Total Topics
634044
• Total Posts
3015213
×