Set no animation in Turtle - Python 3.4?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 03:43:27

问题


The code below creates a fractal tree. I want to draw it as quick as possible -- I don't want any animation to occur, otherwise it takes a long time to draw. In earlier versions of python, this is achieved with turtle.speed(0), as shown below. This doesn't work in python 3.4

import turtle
import random
red = 125
green = 70
blue = 38        
pen = 10
def tree(branchLen, t, red, green, blue, pen):
    if branchLen > 3:
        pen = pen*0.8
        t.pensize(pen)
        red = red - 15
        green = green + 8
    if branchLen > 5:
        angle = random.randrange(10, 70)
        angleTwo = 0.50*angle
        sub = (0.8*(random.randrange(1,24)))
        t.forward(branchLen)
        t.right(angleTwo)
        tree(branchLen-sub,t, red, green, blue, pen)
        t.left(angle)
        tree(branchLen-sub, t, red, green, blue, pen)
        t.right(angleTwo)
        t.backward(branchLen)

def main():
    t = turtle.Turtle()
    t.hideturtle()
    t.speed(0)
    myWin = turtle.Screen()
    t.left(90)
    t.up()
    t.backward(170)
    t.down()
    tree(random.randrange(40,47),t,red,green,blue, pen)
    myWin.exitonclick()

main()

It seems the speed module doesn't do anything in 3.4. No matter what number (0-10) used, it's always the same speed and doesn't display an error -- Meaning it still animates.

How can I achieve no animation with turtle in python 3.4? http://interactivepython.org/runestone/static/pythonds/Recursion/graphical.html This is a good place to run the code in Python 2, just replace one of the windows codes with mine.


回答1:


I think using turtle.tracer(False) would be useful as it ignores the animation.




回答2:


First, I recommend speed('fastest') over speed(0) to make your intent clear. Second, before considering tracer(False), first make sure your code is optimal. Finally, always use tracer(True) when you're done drawing as some operations won't behave as expected (e.g. hideturtle() at the end of the code) if tracing isn't turned back on.

As far as my second recommendation goes, your combination of if branchLen > 3: and if branchLen > 5: makes no real difference if you follow the logic so simply do if branchLen > 5:. You pass around colors and manipulate them but never actually use them -- if you want optimal speed, toss incomplete code. Plus other little optimizations:

from turtle import Screen, Turtle
from random import randrange

PEN_SIZE = 8

def tree(branch_length, turtle, pen_size):
    if branch_length < 5:
        return

    angle = randrange(5, 35)
    double_angle = angle * 2
    sub_length = branch_length - randrange(1, 19)

    turtle.pensize(pen_size)
    pen_size *= 0.8

    turtle.forward(branch_length)
    turtle.right(angle)
    tree(sub_length, turtle, pen_size)
    turtle.left(double_angle)
    tree(sub_length, turtle, pen_size)
    turtle.right(angle)
    turtle.backward(branch_length)

def main():
    myWin = Screen()
    yertle = Turtle(visible=False)

    yertle.left(90)
    yertle.penup()
    yertle.backward(170)
    yertle.pendown()

    myWin.tracer(False)
    tree(randrange(40, 47), yertle, PEN_SIZE)
    myWin.tracer(True)

    myWin.exitonclick()

main()

Once you commit to tracer(False) then speed('fastest') probably stops making a difference so you can optionally toss it.



来源:https://stackoverflow.com/questions/25083377/set-no-animation-in-turtle-python-3-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!