如何加速python的'turtle'功能并在最后阻止它冻结

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16119991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:48:18  来源:igfitidea点击:

How to speed up python's 'turtle' function and stop it freezing at the end

pythongraphicsdrawturtle-graphics

提问by clickonMe

I have written a turtle program in python, but there are two problems.

我用python写了一个turtle程序,但是有两个问题。

  1. It goes way too slow for larger numbers, I was wonder how I can speed up turtle.
  2. It freezes after it finishes and when clicked on, says 'not responding'
  1. 对于较大的数字,它的速度太慢了,我想知道如何加快乌龟的速度。
  2. 它在完成后冻结,单击时显示“无响应”

This is my code so far:

到目前为止,这是我的代码:

import turtle

#Takes user input to decide how many squares are needed
f=int(input("How many squares do you want?"))
c=int(input("What colour would you like? red = 1, blue = 2 and green =3"))
n=int(input("What background colour would you like? red = 1, blue = 2 and green =3"))

i=1

x=65

#Draws the desired number of squares.
while i < f:
    i=i+1
    x=x*1.05
    print ("minimise this window ASAP")
    if c==1:
        turtle.pencolor("red")
    elif c==2:
        turtle.pencolor("blue")
    elif c==3:
        turtle.pencolor("green")
    else:
        turtle.pencolor("black")
    if n==1:
        turtle.fillcolor("red")
    elif n==2:
        turtle.fillcolor("blue")
    elif n==3:
        turtle.fillcolor("green")
    else:
        turtle.fillcolor("white")
    turtle.bk(x)
    turtle.rt(90)
    turtle.bk(x)
    turtle.rt(90)
    turtle.bk(x)
    turtle.rt(90)
    turtle.bk(x)
    turtle.rt(90)
    turtle.up()
    turtle.rt(9)
    turtle.down()

By the way: I am on version 3.2!

顺便说一句:我使用的是 3.2 版!

采纳答案by kalgasnik

  1. Set turtle.speed()to fastest.
  2. Use the turtle.mainloop()functionality to do work without screen refreshes.
  3. Disable screen refreshing with turtle.tracer(0, 0)then at the end do turtle.update()
  1. 设置turtle.speed()fastest
  2. 使用该turtle.mainloop()功能无需刷新屏幕即可完成工作。
  3. 禁用屏幕刷新,turtle.tracer(0, 0)然后在最后做turtle.update()

回答by c0d3rman

For reference, turtle being slow is an existing problem. Even with speed set to max, turtle can take quite a long time on things like fractals. Nick ODell reimplemented turtle for speed here: Hide Turtle Window?

作为参考,龟慢是一个存在的问题。即使将速度设置为最大,海龟在分形等事物上也可能需要很长时间。Nick ODell 在这里重新实现了海龟以提高速度:隐藏海龟窗口?

import math

class UndrawnTurtle():
def __init__(self):
    self.x, self.y, self.angle = 0.0, 0.0, 0.0
    self.pointsVisited = []
    self._visit()

def position(self):
    return self.x, self.y

def xcor(self):
    return self.x

def ycor(self):
    return self.y

def forward(self, distance):
    angle_radians = math.radians(self.angle)

    self.x += math.cos(angle_radians) * distance
    self.y += math.sin(angle_radians) * distance

    self._visit()

def backward(self, distance):
    self.forward(-distance)

def right(self, angle):
    self.angle -= angle

def left(self, angle):
    self.angle += angle

def setpos(self, x, y = None):
    """Can be passed either a tuple or two numbers."""
    if y == None:
        self.x = x[0]
        self.y = x[1]
    else:
        self.x = x
        self.y = y
    self._visit()

def _visit(self):
    """Add point to the list of points gone to by the turtle."""
    self.pointsVisited.append(self.position())

# Now for some aliases. Everything that's implemented in this class
# should be aliased the same way as the actual api.
fd = forward
bk = backward
back = backward
rt = right
lt = left
setposition = setpos
goto = setpos
pos = position

ut = UndrawnTurtle()

回答by Eric Leschinski

Python turtle goes very slowly because screen refreshes are performed after every modification is made to a turtle.

Python 海龟运行非常缓慢,因为每次修改海龟后都会执行屏幕刷新。

You can disable screen refreshing until all the work is done, then paint the screen, it will eliminate the millisecond delays as the screen furiously tries to update the screen from every turtle change.

您可以禁用屏幕刷新,直到所有工作完成,然后绘制屏幕,​​这将消除毫秒延迟,因为屏幕会疯狂地尝试从每次海龟更改更新屏幕。

For example:

例如:

import turtle
import random
import time
screen = turtle.Screen()

turtlepower = []

turtle.tracer(0, 0)
for i in range(1000):
    t = turtle.Turtle()
    t.goto(random.random()*500, random.random()*1000)
    turtlepower.append(t)

for i in range(1000):
    turtle.stamp()

turtle.update()

time.sleep(3)

This code makes a thousand turtles at random locations, and displays the picture in about 200 milliseconds.

此代码在随机位置制作一千只海龟,并在大约 200 毫秒内显示图片。

Had you not disabled screen refreshing with turtle.tracer(0, 0)command, it would have taken several minutes as it tries to refresh the screen 3000 times.

如果您没有使用turtle.tracer(0, 0)命令禁用屏幕刷新,则在尝试刷新屏幕 3000 次时将需要几分钟时间。

https://docs.python.org/2/library/turtle.html#turtle.delay

https://docs.python.org/2/library/turtle.html#turtle.delay