0
votes

i am working on a space invaders game, and i have a problem, my background is not working. Here is he code that was necessary, but didn't work:

wn = turtle.Screen() 
wn.bgcolor("black")
wn.title("Space Invaders")
wn.bgpic('space_invaders_background.gif')

and the error message said

'[Running] python -u "d:\USERS\chedl\Documents\Apps\games_and_apps\offline\python\space_invaders\main.py" Traceback (most recent call last): File "d:\USERS\chedl\Documents\Apps\games_and_apps\offline\python\space_invaders\main.py", line 10, in wn.bgpic('space_invaders_background.gif') File "C:\Users\chedl\AppData\Local\Programs\Python\Python36\lib\turtle.py", line 1481, in bgpic self._bgpics[picname] = self._image(picname) File "C:\Users\chedl\AppData\Local\Programs\Python\Python36\lib\turtle.py", line 479, in _image return TK.PhotoImage(file=filename) File "C:\Users\chedl\AppData\Local\Programs\Python\Python36\lib\tkinter__init__.py", line 3539, in init Image.init(self, 'photo', name, cnf, master, **kw) File "C:\Users\chedl\AppData\Local\Programs\Python\Python36\lib\tkinter__init__.py", line 3495, in init self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "space_invaders_background.gif": no such file or directory

[Done] exited with code=1 in 0.273 seconds'

but the space_invaders_background.gif is in the same file as the actual python application.

2
Try providing the full path of the file and see if it helps - Sheldore
i did, and it is a file downloaded on my computer - nate ethan
also, if you want the path, here it is D:\USERS\chedl\Documents\Apps\games_and_apps\offline\python\space_invaders\space_invaders_background.gif - nate ethan
I don't need this path. You should try using it in your code. I can't hack into your directory ;) - Sheldore

2 Answers

0
votes

I will list the documentation needed to change the colors. Furthermore, I do not see how you have a file that is both a gif and a python script. Are you saying the main.py and my.gif are in the same directory? If so, list the directory name as a full path. What is your OS? Windows? Then full path is C:\my\path\to\script\my.gif. If Unix, /this/is/a/path/and/dir/my.gif.

Per docs:

turtle.bgcolor(*args)
    Parameters: args – a color string or three numbers in the range 0..colormode or 
    a 3-tuple of such numbers
    Set or return background color of the TurtleScreen.

>>> screen.bgcolor("orange")
>>> screen.bgcolor()
'orange'
>>> screen.bgcolor("#800080")
>>> screen.bgcolor()
(128, 0, 128)

turtle.bgpic(picname=None)
    Parameters: picname – a string, name of a gif-file or "nopic", or None
    Set background image or return name of current backgroundimage. 
    If picname is a filename, set the corresponding image as background. 
    If picname is "nopic", delete background image, if present. 
    If picname is None, return the filename of the current backgroundimage.

>>> screen.bgpic()
'nopic'
>>> screen.bgpic("landscape.gif")
>>> screen.bgpic()
"landscape.gif"
0
votes

If you're on Visual Studio Code, make sure to restart the application and select the folder that contains the python file as the workspace folder like this

Hope this solves your problem