Advertisement

Pygame importing images

Started by January 06, 2014 10:15 PM
1 comment, last by Crazylegs830 11 years, 1 month ago

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?

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.

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.

Ah, I see thank you very much!

This topic is closed to new replies.

Advertisement