使用turtle库、time库编程实现循环交替绘制当前时间和自己学号的七段数码管,即绘制七段数码管显示当前时间(时分秒),一秒后绘制七段数码管显示自己学号,不断循环
时间: 2024-03-01 12:52:19 浏览: 49
好的,我会按照您的要求编写程序。以下是代码:
```python
import turtle
import time
# 定义数字的点阵编码
digit = [
[1, 1, 1, 1, 1, 1, 0], # 0
[0, 1, 1, 0, 0, 0, 0], # 1
[1, 1, 0, 1, 1, 0, 1], # 2
[1, 1, 1, 1, 0, 0, 1], # 3
[0, 1, 1, 0, 0, 1, 1], # 4
[1, 0, 1, 1, 0, 1, 1], # 5
[1, 0, 1, 1, 1, 1, 1], # 6
[1, 1, 1, 0, 0, 0, 0], # 7
[1, 1, 1, 1, 1, 1, 1], # 8
[1, 1, 1, 1, 0, 1, 1] # 9
]
# 设置窗口和画笔
win = turtle.Screen()
win.setup(800, 600)
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
# 定义绘制单个数字的函数
def draw_digit(d, x, y, size):
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.pensize(3)
pen.color("red")
if d[0]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
pen.right(90)
if d[1]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
pen.right(90)
if d[2]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
pen.right(90)
pen.penup()
pen.forward(size)
pen.right(90)
if d[3]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
pen.right(90)
if d[4]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
pen.right(90)
if d[5]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
pen.right(90)
pen.penup()
pen.forward(size)
pen.right(90)
if d[6]:
pen.forward(size)
else:
pen.penup()
pen.forward(size)
pen.pendown()
# 定义绘制时间的函数
def draw_time():
pen.clear()
now = time.strftime("%H%M%S")
x = -200
y = 0
size = 50
for i in range(len(now)):
draw_digit(digit[int(now[i])], x, y, size)
x += size * 1.5
# 定义绘制学号的函数
def draw_number():
pen.clear()
number = "2019123456"
x = -200
y = 0
size = 50
for i in range(len(number)):
draw_digit(digit[int(number[i])], x, y, size)
x += size * 1.5
# 循环绘制时间和学号
while True:
draw_time()
time.sleep(1)
draw_number()
time.sleep(1)
turtle.done()
```
程序中使用了turtle库来绘制七段数码管,使用time库来获取当前时间。在程序中定义了数字的点阵编码digit,以及绘制单个数字的函数draw_digit。在绘制时间和学号的函数中,通过循环调用draw_digit来绘制每个数字。
程序通过循环绘制时间和学号,并使用time库中的sleep函数来实现间隔一秒钟绘制一次。最终效果为循环交替绘制当前时间和自己学号的七段数码管。
阅读全文