Reputation:
I'm using turtle graphics and I would like to clear only a part of the screen and keep the rest of the image/drawings intact.
So far I've been drawing a white rectangle to clear a part of the image, but this is very slow and if the background isn't white then I can't use this method.
This is the function I have been using :
import turtle
t = turtle.Turtle()
def screen_clearer(coordinate_x, coordinate_y, length, width):
t.up()
t.goto(coordinate_x, coordinate_y)
t.color("white")
t.begin_fill()
t.setheading(0)
for i in range(2):
t.fd(length)
t.rt(90)
t.fd(width)
t.rt(90)
t.end_fill()
Is there a built-in function in turtle that clears only part of the screen? I have looked and haven't found anything so this is why I've been using this.
Upvotes: 3
Views: 4366
Reputation: 197
To delete your last line move back on it with the pen in screen color. For instance, on a white screen: color("black"); forward(100) color("white"); back(100)
Upvotes: -1
Reputation: 41925
One rough way to do this is to use different turtles to draw different parts of the screen. When you're ready, you can have one or more turtles clear()
or .undo()
their portion of the screen:
from time import sleep
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor("cyan")
left = Turtle(visible=False)
left.color("white")
right = Turtle(visible=False)
right.color("red")
left.penup()
left.goto(-200, -150)
left.pendown()
left.circle(150, 720, steps=5)
right.penup()
right.goto(200, -175)
right.pendown()
right.circle(175, 360, steps=6)
sleep(3)
left.clear() # or: while left.undobufferentries(): left.undo()
screen.mainloop()
Upvotes: 2