最近,我在python上想出了如何制作移动线。
我现在要做的是知道如何使用移动的秒针(分钟)和没有时针进行时钟动画。
我想要实现的目标:
因为它们是截图,所以它们并没有显示它们在移动
我的代码
import turtle
import time
turtle.hideturtle()
def draw_line(x,y,heading,length,color):
turtle.up()
turtle.goto(x,y)
turtle.down()
turtle.setheading(heading)
turtle.color(color)
turtle.fd(length)
turtle.tracer(0,0)
for heading in range(0,-360*100,-6):
turtle.clear()
draw_line(0,0,heading,200,'blue')
turtle.update()
time.sleep(1)
如果有人提供如何绘制时钟,它也会有所帮助
目前尚不清楚你在问什么,但我假设它有两件事:第一,如何添加另一条移动线;第二,如何设置这些线的角度以匹配实际(本地)时间。我已经重新编写了下面的代码来完成这两件事,并且花费了一小时,而且成本很低。要考虑的事情:
你不想用一只乌龟这样做 - 你至少想要两只乌龟。否则,当你打电话给clear()
时,你会失去一切,包括表盘,并且必须重新绘制它。您可以通过使用undo()
来解决这个问题,或者更简单地说,有一只乌龟的图纸是永久性的(表盘),另一只乌龟的图纸在每个蜱(手)上清除。
下面的其他变化包括:抛掷time.sleep()
,支持龟自己的ontimer()
事件;从turtle的基于函数的API切换到面向对象的API,这样我们就可以管理两只乌龟。我将乌龟切换到徽标模式,在屏幕顶部放置零度,并使图形的角度为顺时针而不是逆时针(如果您正在实施时钟,这非常有用!)
from turtle import Screen, Turtle, Vec2D
from time import localtime
CENTER = Vec2D(0, 0)
def draw_line(position, heading, length, color):
hands.up()
hands.goto(position)
hands.down()
hands.setheading(heading)
hands.color(color)
hands.forward(length)
def tick():
time = localtime()
second_heading = time.tm_sec * 6
minute_heading = time.tm_min * 6 + second_heading / 60
hour_heading = time.tm_hour % 12 * 30 + minute_heading / 12
hands.clear()
draw_line(CENTER, second_heading, 300, 'red')
draw_line(CENTER, minute_heading, 200, 'blue')
draw_line(CENTER, hour_heading, 100, 'green')
screen.update()
screen.ontimer(tick, 1000)
screen = Screen()
screen.mode("logo") # 0 degrees at top, clockwise angles!
screen.tracer(False) # force manual screen updates
# What this turtle draws is "permanent"
dial = Turtle(visible=False)
dial.penup()
dial.dot()
dial.setx(330) # remember mode is "logo"
dial.pendown()
dial.circle(330)
# What this turtle draws has to be redrawn on every tick
hands = Turtle(visible=False)
tick()
screen.mainloop()