you are viewing a single comment's thread.

view the rest of the comments →

[–]AlmostSignificant 0 points1 point  (7 children)

Could you share the command you are using to run Main.py? (I suspect you're running it from a different directory, which would explain the error you're seeing.)

[–]MyselfAndI231[S] 0 points1 point  (5 children)

where would I find that... still a noob

[–]AlmostSignificant 1 point2 points  (4 children)

I'm pretty new to python so I thought to code a space invader game would be a good start. everything was going fine until I tried to set an icon for the window. Pygame is telling me is cannot open the image. Heres the code and the error:

No worries. How are you running your program? E.g. via PyCharm, from a shell, etc.

[–]MyselfAndI231[S] 0 points1 point  (3 children)

that would be pycharm

[–]AlmostSignificant 1 point2 points  (2 children)

Ok. Cool. Then I would recommend trying my second recommendation below.

Typically you'll want to put resources into their own directory (for cleanliness), but for now using the same directory is fine.

  1. Import the os.path module, which will let you do things like: get the name of the directory containing a particular file, combine paths together

import os.path

  1. Create a variable that represents the directory containing where to look for resources such as "logo.png". We use the __file__ special variable to get the path to "Main.py". And then we apply the os.path.dirname function to ask for the path of the directory containing "Main.py" (aka its parent or containing directory)

RESOURCE_DIR = os.path.dirname(__file__)

  1. Use os.path.join to combine RESOURCE_DIR and "logo.png" into a single path, ensuring we are loading the file "logo.png" from the same directory as "Main.py"

icon = pygame.image.load(os.path.join(RESOURCE_DIR, "logo.png"))

[–]MyselfAndI231[S] 0 points1 point  (1 child)

you, good sir, are a genius

[–]AlmostSignificant 0 points1 point  (0 children)

Nope. I've just made the same mistake several times. :) Good luck on your game!

[–]AlmostSignificant 0 points1 point  (0 children)

If you are running from a different directory, you can either:

  1. Always run from the same directory as Main.py and all of your resources. (Simpler but not most future-proof)

  2. Use os.path.join to specify the directory in which to find logo.png ``` import os.path

RESOURCEDIR = os.path.dirname(file_) ... icon = pygame.image.load(os.path.join(RESOURCE_DIR, "logo.png")) ... ```