2

I'm working on a game with pygame and I have the following directory for it: enter image description here

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?

Mikecraft1224
  • 99
  • 1
  • 9
  • have answers here https://stackoverflow.com/questions/26392336/importing-images-from-a-directory-python-to-list-or-dictionary – ghost21blade Jan 25 '22 at 11:37

2 Answers2

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!

B. Bogdan
  • 56
  • 5
1

You should just be able to use pygame.image.load("./../../images/player/jumper-1.png") because the directory .. is reserved for parent directory.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 11 '22 at 05:44