使用turtle库、time库编程实现循环交替绘制当前时间和202205404383的七段数码管,即绘制七段数码管显示当前时间(时分秒),一秒后绘制七段数码管显示202205404383,不断循环。
时间: 2024-03-01 14:52:48 浏览: 65
好的,以下是您需要的代码:
```python
import turtle
import time
# 定义数字 0-9 的七段数码管对应的绘制路径
digit_paths = [
[(0, 0), (0, 100), (50, 150), (100, 100), (100, 0), (50, -50), (0, 0)], # 0
[(0, 0), (100, 0), (100, 50), (50, 100), (0, 100), (0, 0), (100, 100)], # 1
[(0, 0), (0, 100), (50, 150), (100, 100), (100, 50), (50, 0), (0, 0)], # 2
[(0, 0), (0, 100), (50, 150), (100, 100), (100, 50), (50, 50), (0, 0)], # 3
[(0, 0), (100, 0), (100, 50), (50, 100), (0, 50), (0, 0), (100, 100)], # 4
[(0, 0), (100, 0), (100, 50), (50, 100), (0, 50), (0, 0), (50, 50)], # 5
[(0, 0), (0, 100), (50, 150), (100, 100), (100, 50), (50, 50), (0, 0)], # 6
[(0, 0), (100, 0), (100, 100), (0, 100), (0, 50), (50, 0), (100, 0)], # 7
[(0, 0), (0, 100), (50, 50), (100, 100), (100, 0), (50, -50), (0, 0)], # 8
[(0, 0), (0, 100), (50, 50), (100, 100), (100, 0), (50, 0), (0, 0)] # 9
]
# 定义画笔参数
pen_size = 10
pen_color = 'red'
# 定义坐标系参数
start_x = -300
start_y = 0
gap_x = 150
gap_y = 200
# 初始化画布和画笔
turtle.setup(800, 400)
turtle.bgcolor('black')
turtle.speed(0)
turtle.hideturtle()
turtle.pensize(pen_size)
turtle.pencolor(pen_color)
while True:
# 获取当前时间并格式化
current_time = time.strftime('%H%M%S')
# 绘制当前时间的七段数码管
current_x = start_x
current_y = start_y
for digit in current_time:
turtle.penup()
turtle.goto(current_x, current_y)
turtle.pendown()
for path in digit_paths[int(digit)]:
x, y = path
turtle.goto(current_x + x, current_y + y)
current_x += gap_x
# 休眠一秒
time.sleep(1)
# 清空画布
turtle.clear()
# 绘制学号的七段数码管
student_id = '202205404383'
current_x = start_x
current_y = start_y
for digit in student_id:
turtle.penup()
turtle.goto(current_x, current_y)
turtle.pendown()
for path in digit_paths[int(digit)]:
x, y = path
turtle.goto(current_x + x, current_y + y)
current_x += gap_x
# 等待用户关闭窗口
turtle.done()
```
这段代码会不断地循环绘制当前时间和您的学号,每次绘制之间间隔一秒钟。您可以根据需要调整画笔参数、坐标系参数和休眠时间。
阅读全文