我用python编写了一个turtle程序,但是有两个问题。
到目前为止这是我的代码:
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版的!
Python turtle的速度非常慢,因为每次对turtle进行修改后都会执行屏幕刷新。
你可以禁用屏幕刷新,直到所有的工作都完成,然后绘制屏幕,它将消除毫秒延迟,因为屏幕激烈地试图从每次海龟的变化中更新屏幕。
例如:
这段代码在随机位置生成一千只海龟,并在大约200毫秒内显示图片。
如果您没有使用
turtle.tracer(0, 0)
命令禁用屏幕刷新,那么在尝试刷新屏幕3000次时,将需要几分钟的时间。https://docs.python.org/2/library/turtle.html#turtle.delay
turtle.speed()
设置为fastest
。turtle.mainloop()
功能在不刷新屏幕的情况下执行工作。turtle.tracer(0, 0)
禁用屏幕刷新,然后在结束时执行turtle.update()
作为参考,海龟慢是一个存在的问题。 即使速度设为最大值,乌龟也可能需要很长时间才能完成分形等动作。 尼克·奥德尔在这里重新完成了海龟的速度:Hide Turtle Window?
相关问题 更多 >
编程相关推荐