如何加速 python 的“turtle”函数并使其在最后停止冻结

2023-12-15

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

  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版本!


  1. Set turtle.speed("fastest").
  2. Use the turtle.mainloop()无需刷新屏幕即可完成工作的功能。
  3. 禁用屏幕刷新turtle.tracer(0, 0)然后最后做turtle.update()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何加速 python 的“turtle”函数并使其在最后停止冻结 的相关文章

随机推荐