0

I want to add an image to my pygame program. The image is in the same directory as that of the py file. But still, when I run the program there is a problem. My code is:

background=pygame.image.load("background.png")

but still it does not work.

I searched it on the web and tried this:

background=pygame.image.load(os.path.abspath("/Users/Administrator/Vs code programming/Python/PYgame/background.png"))

this worked fine, but when I made my game I added the various images in the same folder with the exe file. But there was an error on the cmd saying "could not open the file". The location of the file was stated as the same as I entered in the abs path. People told me to investigate what my current paths and working directories were. But what does that mean? Please help me solve this. I am using VS Code.

1 Answers1

0

I think i figured out why the first code is not working.

When you run a file with the buil-in "run" function in VS Code, it creates a copy of your python file to a temporary folder.

In this case try to use your cmd to run you script

CMD:

cd "c:/directory/of/python/script/"
python script.py
Fredericka
  • 296
  • 1
  • 7