python - 如何快速撤消海龟?
问题描述
我正在制作一个创建螺旋计的程序。这是我的代码:
from turtle import *
from random import randint
speed(10000)
for i in range(20):
col = randint(1, 5)
if col == 1:
pencolor("orange")
elif col == 2:
pencolor("blue")
elif col == 3:
pencolor("green")
elif col == 4:
pencolor("purple")
elif col == 5:
pencolor("dark blue")
circle(50)
left(20)
while undobufferentries():
undo()
但是,程序的最后一部分是问题。我希望这部分,即撤消,非常快,就像创建螺旋一样快。如何快速撤消?
解决方案
三遍你说你想要它“非常快”,但你也说,“就像创造螺旋一样快”。我们可以轻松地让它比创建 spiro 的速度更快,但只需稍微多做一些工作以使其“尽可能快”。
这个问题指出了undo()
机制的弱点。要撤消您的一个圈子,需要执行 80 多个步骤。但是,即使这样也不能完全清楚为什么它这么慢。如果我们关闭屏幕更新,我们可以让它即时更新。但如果这是可以接受的,那么你不妨替换undo()
为clear()
.
我很少鼓励人们弄乱tracer()
,即使我这样做了,我通常也会写一些简单的示例tracer(False)
和tracer(True)
,以避免使用数字参数。但这是我们想要一个特定的数字参数tracer()
来控制undo()
图形速度的罕见情况:
from turtle import *
from random import choice
COLORS = ["orange", "blue", "green", "purple", "dark blue"]
hideturtle()
speed('fastest')
for _ in range(20):
pencolor(choice(COLORS))
circle(50)
left(20)
tracer(20) # value unrelated to either 20 above
while undobufferentries() > 1:
undo() # undo everything but hideturtle()
tracer(1)
exitonclick()
在我的系统上,绘图的撤消现在与do的速度相同,但您可能需要微调参数以tracer()
达到您想要的效果。
推荐阅读
- java - 使用 Tomcat 服务器在 NetBeans 中部署我的第一个 Servlet 时出现错误 500
- excel - 如何将以下数据转换为表格格式?
- java - 避免在磁盘上创建pdf文件以在java中作为附件发送电子邮件
- c++ - 在 Google Kick start 中提交时出错
- javascript - MUI 自动完成 onchange 未在芯片删除时触发
- python - CS50 Pset7 房屋问题 - 我的数据库未收集信息
- python - 从当前工作目录相对导入 python 模块
- mongoose - For循环内的猫鼬findone
- python - 为什么我的函数在 REPL 中没有被识别
- python - 遍历列表并使用 remove() 不会产生所需的结果