'Get images from images folder within pygame
I'm working on a game with pygame and I have the following directory for it:
As you can see, I have an images folder in the package.
Now I'm struggling, to get the image with the path "JumpnRun/images/player/jumper-1.png" from within the path "JumpnRun/Player/Player.py".
I tried to get that image using the following code:
self.image:pygame.Surface = pygame.image.load(str(Path("..") / "images" / "player" / "jumper-1.png")).convert_alpha()
But this doesn't seem to work and I get the error "FileNotFoundError: No such file or directory."
Any ideas on that?
Solution 1:[1]
So, to get the image with pygame.image.load
function, you should do in this way: carImg = pygame.image.load('image.png')
.
To find out more, I recommand you to check out this link : https://pythonprogramming.net/displaying-images-pygame/
The code from the link worked for me!
I hope I helped you!
Solution 2:[2]
You should just be able to use pygame.image.load("./../../images/player/jumper-1.png") because the directory .. is reserved for parent directory.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | B. Bogdan |
Solution 2 | Hyan K Hamid |