我如何从乌龟的底部旋转形状
我正在尝试制作一个乌龟模拟时钟,为了制作第二,分钟,一个小时的手,我正在用乌龟制作形状,并使用.tilt()
每秒将其倾斜6度。 问题是,当我运行.tilt()
时,它会从中间旋转船,而我想从下点(例如模拟时钟)旋转它。 有没有办法做到这一点,还是我需要找到另一种方法来制定此程序?
这是我的代码:
from turtle import *
import time
turtle = Turtle()
turtle.shape("square")
turtle.shapesize(6, .1)
tilt_amnt = 0
for x in range (60):
turtle.tilt(tilt_amnt)
tilt_amnt = tilt_amnt + 6
time.sleep(1)
I am trying to make a turtle analog clock, and to make the second, minute, an hour hands I am making a shape with turtle, and using .tilt()
to tilt it 6 degrees every second.
The thing is, when I run .tilt()
it rotates the ship from the middle, whereas I want to have it rotate from the bottom point (like an analog clock).
Is there a way to do that, or do I need to find another way to make this program?
Here is my code:
from turtle import *
import time
turtle = Turtle()
turtle.shape("square")
turtle.shapesize(6, .1)
tilt_amnt = 0
for x in range (60):
turtle.tilt(tilt_amnt)
tilt_amnt = tilt_amnt + 6
time.sleep(1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为没有一种方法可以以从底部转动的方式将乌龟倾斜,但是您可以重写代码以实质上做同样的事情,而是使用前进。检查一下。
I don't think there is a way to tilt the turtle in such a way where it turns from the bottom but you can rewrite the code to essentially do the same thing but using forward instead. Check this out.
一些想法:首先,我不会使用
tilt()
(高高开销,强制更新),即使您确实使用海龟作为手,请考虑使用right()
或setheading()
;其次,我将使用模式('徽标')
,因为这使0度成为屏幕的顶部,并使标题 colkatwise (例如,“时钟”),而不是逆时针;第三,如果您需要准确性,请不要使用sleep()
,而是从系统中提取当前时间并相应地设置您的手。大型现实世界的时钟确实会从中间的中心转动他们的手。这可以防止手在上升的途中减慢机制,或者在向下驶下的机构。诀窍是使手在视觉上看起来不对称,但保持重量对称。这是我早些时候提出了一种解决方案,它用乌龟在他们的中心转动。
最后,这是提供@AlexjoSlin提供的示例的重写,以使用实时准确性,可能会在一秒钟内多次更新手位置:
Some thoughts: first, I wouldn't use
tilt()
(high overhead, forces an update), even if you do use turtles as hands, consider usingright()
orsetheading()
; second, I would usemode('logo')
as this makes 0 degrees the top of the screen and makes headings clockwise (like, say a 'clock') instead of counterclockwise; third, if you want accuracy, don't usesleep()
but rather extract the current time from the system and set your hands accordingly.Large real world clocks do turn their hands from the middle, or more specifially, the center of gravity. This keeps the hands from slowing the mechanism on the way up, or rushing it on the way down. The trick is to make the hands look visually asymmetric but keep the weight symmetric. Here's a solution I came up with earlier that does something like this with turtles as the hands, turning from their centers.
Finally, here's a rewrite of the example @AlexJoslin provided to use realtime accuracy, potentially updating hand positions multiple times within a second: