1

我正在 Pyzo 中做一个练习项目,并且我在使用 turtle.tracer() 时遇到了一些问题。

当 n =/= 0 或 1 时,我遇到了屏幕上的对象开始闪烁的问题。

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

当n = 1时,没有闪烁,但这也意味着动画无论如何都以基本速度进行,因此没有效果。当 n = 0 时,没有闪烁。但是,这对我的目的来说太快了。我尝试更改“延迟”值,但无济于事:

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

我在这一点上很困。我不确定,我可能缺少一个非常明显的解决方案。

谢谢!

4

2 回答 2

1

我终于找到了答案!

在 Screen.update() 之后可以使用一个函数,称为 time.sleep()。如果你在 time.sleep() 中输入一个值,它会在再次刷新之前暂停几秒钟。

例如,我通过这样做修复了自己的代码:

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

希望这对任何人都有帮助!

于 2020-04-06T21:07:29.687 回答
1

你不需要,也不想要,time.sleep()。首先,您需要适当地安排您的程序关于tracer()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()

其次,我已经用while True:计时器事件替换了你的 ,它在乌龟这样的事件驱动环境中没有位置。注意第二个参数ontimer()是再次调用此方法之前的时间延迟。

于 2020-04-10T02:35:36.537 回答