如何在龟蟒中设置背景

问题描述 投票:0回答:1

我正在从事太空侵略者游戏,我有一个问题,我的背景不起作用。这是他必要的代码,但没有用:

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

并且错误消息说

'[运行] python -u“d:\ USERS \ chedl \ Documents \ Apps \ games_and_apps \ offline \ python \ space_invaders \ main.py”Traceback(最近一次调用最后一次):文件“d:\ USERS \ chedl \ Documents \ Apps \ games_and_apps \ offline \ python \ space_invaders \ main.py“,第10行,在wn.bgpic('space_invaders_background.gif')文件”C:\ Users \ chedl \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ turtle .py“,第1481行,bgpic中的self._bgpics [picname] = self._image(picname)文件”C:\ Users \ chedl \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ turtle.py“,line 479,在_image中返回TK.PhotoImage(file = filename)文件“C:\ Users \ chedl \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ tkinter__init __。py”,第3539行,在init Image.init中(self, 'photo',name,cnf,master,** kw)文件“C:\ Users \ chedl \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ tkinter__init __。py”,第3495行,在init self.tk.call中(('image','create',imgtype,name,)+ options)_tkinter.TclError:无法打开“space_invaders_background.gif”:没有这样的文件或目录

[完成]退出,代码= 1,在0.273秒内'

但是space_invaders_background.gif与实际的python应用程序在同一个文件中。

python python-3.x visual-studio-code turtle-graphics
1个回答
0
投票

我将列出更改颜色所需的文档。此外,我不知道你是如何拥有一个gif和python脚本的文件。你是说main.pymy.gif在同一个目录里?如果是,请将目录名称列为完整路径。你的操作系统是什么?视窗?然后完整的路径是C:\my\path\to\script\my.gif。如果是Unix,/this/is/a/path/and/dir/my.gif

对于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"
© www.soinside.com 2019 - 2024. All rights reserved.