• Advertisement
Sign in to follow this  

Pygame importing images

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement