ProgrammingEnthusiast
ProgrammingEnthusiast

Reputation: 304

How to gradually erase the path traced by turtle in python?

I have been making some programs using python turtle in which I want to see path traced by turtle.I know that turtle.penup() makes the turtle lift up and turtle.clear() clears the everything, but it is not what I want, I want the path traced by turtle to fade gradually until it just gets erased, So is there any way I can achieve this?

Upvotes: 0

Views: 1281

Answers (2)

Red
Red

Reputation: 27547

Have a look at this, from the turtledemo source code:

from turtle import Screen, Turtle, mainloop
from time import perf_counter, sleep

def mn_eck(p, ne,sz):
    turtlelist = [p]
    #create ne-1 additional turtles
    for i in range(1,ne):
        q = p.clone()
        q.rt(360.0/ne)
        turtlelist.append(q)
        p = q
    for i in range(ne):
        c = abs(ne/2.0-i)/(ne*.7)
        # let those ne turtles make a step
        # in parallel:
        for t in turtlelist:
            t.rt(360./ne)
            t.pencolor(1-c,0,c)
            t.fd(sz)

def main():
    s = Screen()
    s.bgcolor("black")
    p=Turtle()
    p.speed(0)
    p.hideturtle()
    p.pencolor("red")
    p.pensize(3)

    s.tracer(36,0)

    at = perf_counter()
    mn_eck(p, 36, 19)
    et = perf_counter()
    z1 = et-at

    sleep(1)

    at = perf_counter()
    while any([t.undobufferentries() for t in s.turtles()]):
        for t in s.turtles():
            t.undo()
    et = perf_counter()
    return "runtime: %.3f sec" % (z1+et-at)


if __name__ == '__main__':
    msg = main()
    print(msg)
    mainloop()

Upvotes: 0

cdlane
cdlane

Reputation: 41872

I want the path traced by turtle to fade gradually until it just gets erased, So is there any way I can achieve this?

There's nothing built-in to turtle to support this. Below is my crude emulation of gradual fade using lots of turtles and timers:

from turtle import Screen, Turtle

PEN_WIDTH = 5
SEGMENTS_PER_LINE = 12
MILLISECONDS_PER_FADE = 500
AMOUNT_PER_FADE = 0.05

def fade_forward(t, distance):
    stride = delta = distance / SEGMENTS_PER_LINE
    heading = t.heading()

    while stride < distance:
        position = t.position()
        t.forward(delta)

        fader = faders.pop() if faders else fader_prototype.clone()
        fader.setheading(heading)
        fade(fader, position, delta)

        t.clear()
        stride += delta

def fade(f, position, distance, shade=0.0):
    screen.tracer(False)
    f.clear()

    if shade < 1.0:
        f.pencolor(shade, shade, shade)
        f.setposition(position)
        f.pendown()
        f.forward(distance)
        f.penup()

        shade += AMOUNT_PER_FADE
        screen.ontimer(lambda: fade(f, position, distance, shade), MILLISECONDS_PER_FADE)
    else:
        faders.append(f)

    screen.tracer(True)

faders = []

screen = Screen()

fader_prototype = Turtle()
fader_prototype.hideturtle()
fader_prototype.speed('fastest')
fader_prototype.width(PEN_WIDTH)
fader_prototype.penup()

turtle = Turtle()
turtle.shape('turtle')
turtle.width(PEN_WIDTH)
turtle.penup()
turtle.setposition(-170, -125)
turtle.pendown()

for _ in range(10):
    fade_forward(turtle, 340)
    turtle.left(126)
    fade_forward(turtle, 400)
    turtle.left(126)

screen.exitonclick()

enter image description here

Upvotes: 2

Related Questions