简体   繁体   中英

How to Increase Delay on turtle.tracer()? [PYTHON]

I'm doing a practice project in Pyzo, and I've been having some issues with turtle.tracer().

When n =/= 0 or 1, I run into an issue where the objects on screen start to flicker.

import turtle
sketcher = turtle.Turtle()
canvas = turtle.Screen()

sketcher.speed(0)
sketcher.hideturtle()

def drawPinwheelAnimation(length):
    sketcher.setheading(90)
    backwardLength = length
    lengthChange = 1
    while(True):
        canvas.tracer(36)
        drawPinwheel(0, 0, length, backwardLength)
        drawPinwheel(3*length, 0, length, backwardLength)
        drawPinwheel(0, 3*length, length, backwardLength)
        drawPinwheel(-3*length, 0, length, backwardLength)
        drawPinwheel(0, -3*length, length, backwardLength)
        canvas.update()
        backwardLength += lengthChange
        if(backwardLength == (1.5*length) or backwardLength == (0.5*length)):
            lengthChange *= -1
        sketcher.clear()

def drawSwingingPinwheel(xPos, yPos, initialLength):
    while(True):
        drawPinwheel(xPos, yPos, forwardLength, backwardLength)
        backwardLength += lengthChange
        if(backwardLength == (1.5*initialLength) or backwardLength == (0.5*initialLength)):
            lengthChange *= -1
        sketcher.clear()


def drawPinwheel(xPos, yPos, fdLength, bkLength):
    sketcher.penup()
    sketcher.setpos(xPos, yPos)
    sketcher.pendown()
    for side in range(12):
        sketcher.fd(fdLength)
        sketcher.bk(bkLength)
        sketcher.rt(30)

drawPinwheelAnimation(50)

turtle.mainloop()

When n = 1, there is no flicker, but it also means that the animation goes at the basic speed anyways, so it has no effect. When n = 0, there is no flicker. However, it's far too fast for my purposes. I tried changing the "delay" value, but to no avail:

        canvas.tracer(0, 1000) #Changing the second value to 1000 has no notable difference.
        drawPinwheel(0, 0, length, backwardLength)
        drawPinwheel(3*length, 0, length, backwardLength)
        drawPinwheel(0, 3*length, length, backwardLength)
        drawPinwheel(-3*length, 0, length, backwardLength)
        drawPinwheel(0, -3*length, length, backwardLength)
        canvas.update()

I'm pretty stuck at this point. There might be a pretty obvious solution I'm missing, I'm not sure.

Thanks!

I ended up finding the answer!

There's a function that can be used after Screen.update() called time.sleep(). If you put a value within time.sleep(), it will pause for that many seconds before refreshing once more.

For example, I fixed my own code by doing this:

import turtle
import time
sketcher = turtle.Turtle()
canvas = turtle.Screen()

sketcher.speed(0)
sketcher.hideturtle()

def drawPinwheelAnimation(length):
    sketcher.setheading(90)
    backwardLength = length
    lengthChange = 1
    while(True):
        canvas.tracer(0)
        drawPinwheel(0, 0, length, backwardLength)
        drawPinwheel(3*length, 0, length, backwardLength)
        drawPinwheel(0, 3*length, length, backwardLength)
        drawPinwheel(-3*length, 0, length, backwardLength)
        drawPinwheel(0, -3*length, length, backwardLength)
        canvas.update()
        time.sleep(.01) # <-------- Here's the time.sleep() function!
        backwardLength += lengthChange
        if(backwardLength == (1.5*length) or backwardLength == (0.5*length)):
            lengthChange *= -1
        sketcher.clear()

def drawSwingingPinwheel(xPos, yPos, initialLength):
    while(True):
        drawPinwheel(xPos, yPos, forwardLength, backwardLength)
        backwardLength += lengthChange
        if(backwardLength == (1.5*initialLength) or backwardLength == (0.5*initialLength)):
            lengthChange *= -1
        sketcher.clear()


def drawPinwheel(xPos, yPos, fdLength, bkLength):
    sketcher.penup()
    sketcher.setpos(xPos, yPos)
    sketcher.pendown()
    for side in range(12):
        sketcher.fd(fdLength)
        sketcher.bk(bkLength)
        sketcher.rt(30)

drawPinwheelAnimation(50)

turtle.mainloop()

Hope this helps anyone!

You don't need, nor want, time.sleep() . First you need to arrange your program appropriately in regards to tracer() and update() :

from turtle import Screen, Turtle

def drawPinwheelAnimation(length, lengthChange=1, backwardLength=None):
    if backwardLength is None:
        backwardLength = length

    turtle.clear()

    drawPinwheel(0, 0, length, backwardLength)
    drawPinwheel(3*length, 0, length, backwardLength)
    drawPinwheel(0, 3*length, length, backwardLength)
    drawPinwheel(-3*length, 0, length, backwardLength)
    drawPinwheel(0, -3*length, length, backwardLength)

    screen.update()

    backwardLength += lengthChange

    if backwardLength == (1.5*length) or backwardLength == 0.5*length:
        lengthChange *= -1

    screen.ontimer(lambda: drawPinwheelAnimation(length, lengthChange, backwardLength), 100)

def drawPinwheel(xPos, yPos, fdLength, bkLength):
    turtle.penup()
    turtle.setpos(xPos, yPos)
    turtle.pendown()

    for _ in range(12):
        turtle.forward(fdLength)
        turtle.backward(bkLength)
        turtle.right(30)

screen = Screen()
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.setheading(90)

drawPinwheelAnimation(50)

screen.mainloop()

Second, I've replaced your while True: , which has no place in an event-driven environment like turtle, with a timer event. Note the second argument to ontimer() which is the time delay before calling this method again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM