user13825319
user13825319

Reputation:

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

I have the following code:

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

I am trying to remove the turtle called flag, through adding it to a list then deleting it with del(testlist[0]), but it didn't work. The output is:

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)?

Upvotes: 1

Views: 1718

Answers (2)

cdlane
cdlane

Reputation: 41872

Your question is confusing as the title and text ask one thing,
while your example code and output show something completely different.

Let's address this question:

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

Generally turtle.hideturtle() will do what you want. The only way to dispose of turtles once created is via a screen.clear() which will destroy all of them.

(The variable turtle above needs to be set to an instance of Turtle() and the variable screen needs to be set to the singular instance of Screen().

Upvotes: 0

Red
Red

Reputation: 27567

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

Basically, you can use either turtle.hideturtle() or turtle.ht() to make a turtle invisible.
But, that does not mean that the turtle is removed, and so it still takes up memory.

You can call turtle.Screen.clear(), but that resets everything, even the things you might want to keep.

If I were in a situation where I want to delete turtles instead of hiding them because doing that over and over again will take up too much memory, I'd simply hide the turtle, and when the program need another turtle, instead of creating another one, simply unhide the hidden turtle to be used again.

Upvotes: 0

Related Questions