有没有办法从屏幕上删除乌龟?

Is there a way to remove a turtle from the screen?

我有以下代码:

answer = "ABC"
flag.goto(-999, -999)
while (answer.lower != 'y' or answer.lower != 'n'):
    print("You got the flag! Free play(y/n)?")
    answer = input("")
    if answer.lower == 'y':
        pass
    if answer.lower == 'n':
        return None

我试图删除名为 flag 的海龟,方法是将其添加到列表中,然后使用 del(testlist[0]) 将其删除,但没有成功。 输出是:

You got the flag! Free play(y/n)?
y
You got the flag! Free play(y/n)?
n
You got the flag! Free play(y/n)?

你的问题令人困惑,因为标题和正文问的是一回事,
而你的示例代码和输出显示的是完全不同的东西。

让我们来解决这个问题:

Is there a way to remove a turtle from the screen?

一般turtle.hideturtle()会做你想做的。 处置 海龟的唯一方法是通过 screen.clear() 来销毁 所有 海龟。

(上面的变量turtle需要设置为Turtle()的一个实例,变量screen需要设置为Screen()的单数实例。

You can get a better view on the Visibility of turtles from this documentation.

基本上,您可以使用 turtle.hideturtle()turtle.ht() 来使海龟不可见。
但是,这并不意味着海龟被移除,所以它仍然需要上内存。

可以调用turtle.Screen.clear(),但这会重置一切,甚至是你可能想要保留的东西。

如果我想删除海龟而不是隐藏它们,因为一遍又一遍地这样做会占用太多内存,我会简单地隐藏海龟,当程序需要另一个海龟时, 而不是创建另一个,只需取消隐藏隐藏的乌龟以再次使用。