只清除海龟图形中的一部分屏幕

Clear only a part of the screen in turtle graphics

我正在使用海龟图形,我只想清除屏幕的一部分并保持 image/drawings 的其余部分完好无损。

到目前为止,我一直在绘制一个白色矩形来清除图像的一部分,但这非常慢,如果背景不是白色,则无法使用此方法。

这是我一直在使用的功能:

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

turtle中是否有只清除部分屏幕的内置功能?我已经看过但没有找到任何东西,所以这就是我一直在使用它的原因。

一种粗略的方法是使用不同的海龟绘制屏幕的不同部分。准备就绪后,您可以拥有一只或多只乌龟 clear().undo() 它们在屏幕上的部分:

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

要删除最后一行,请使用屏幕颜色的笔在其上移动。例如,在白色屏幕上: 颜色:黑色”);前进(100) 白颜色”);返回(100)