python时钟罗盘酷炫代码
时间: 2023-05-23 19:07:01 浏览: 999
以下是Python时钟罗盘的酷炫代码:
```python
import turtle
import time
# 绘制刻度线和数字
def draw_line(m):
turtle.pensize(3) # 设置笔的大小
for i in range(m):
turtle.penup()
turtle.goto(0, -240)
turtle.pendown()
turtle.forward(20)
turtle.penup()
turtle.goto(0, -260)
turtle.write(i, align="center", font=("Arial", 12, "normal"))
turtle.right(360 // m)
# 绘制指针
def draw_pointer(name, length):
turtle.penup()
turtle.goto(0, 0)
turtle.color("red")
turtle.setheading(0 - name * (360 // 12))
turtle.pensize(5)
turtle.pendown()
turtle.forward(length)
# 绘制时钟罗盘
def draw_compass():
turtle.setworldcoordinates(-300, -300, 300, 300)
turtle.penup()
turtle.speed(0)
turtle.goto(0, -200)
turtle.pendown()
turtle.circle(200)
turtle.penup()
turtle.goto(0, -180)
turtle.pendown()
turtle.circle(180)
turtle.penup()
turtle.goto(0, -160)
turtle.pendown()
turtle.circle(160)
draw_line(60)
turtle.color("orange")
draw_pointer(time.localtime().tm_sec, 120)
turtle.color("blue")
draw_pointer(time.localtime().tm_min, 100)
turtle.color("green")
draw_pointer(time.localtime().tm_hour % 12, 70)
turtle.hideturtle()
# 主函数
if __name__ == '__main__':
draw_compass()
# 持续更新
while True:
turtle.color("orange")
draw_pointer(time.localtime().tm_sec, 120)
turtle.color("blue")
draw_pointer(time.localtime().tm_min, 100)
turtle.color("green")
draw_pointer(time.localtime().tm_hour % 12, 70)
time.sleep(1)
turtle.undo()
turtle.undo()
turtle.undo()
```
这个代码使用了Turtle Graphics库来创建一个时钟罗盘。其中,利用time库来获取当前时间,以此来绘制三个指针。同时,我们使用循环来持续更新指针的位置,从而实现不断更新显示的效果。
阅读全文