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

2024-05-15 19:41:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  1. 对于更大的数字来说,速度太慢了,我想知道我怎样才能加快海龟的速度。
  2. 完成后会冻结,点击时会显示“无响应”

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

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

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


Tags: youinputgreenbluered速度manybk
3条回答

Python turtle的速度非常慢,因为每次对turtle进行修改后都会执行屏幕刷新。

你可以禁用屏幕刷新,直到所有的工作都完成,然后绘制屏幕,它将消除毫秒延迟,因为屏幕激烈地试图从每次海龟的变化中更新屏幕。

例如:

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)

这段代码在随机位置生成一千只海龟,并在大约200毫秒内显示图片。

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

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

  1. turtle.speed()设置为fastest
  2. 使用turtle.mainloop()功能在不刷新屏幕的情况下执行工作。
  3. 使用turtle.tracer(0, 0)禁用屏幕刷新,然后在结束时执行turtle.update()

作为参考,海龟慢是一个存在的问题。 即使速度设为最大值,乌龟也可能需要很长时间才能完成分形等动作。 尼克·奥德尔在这里重新完成了海龟的速度:Hide Turtle Window?

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

相关问题 更多 >