使用turtle库、time库编程实现自己的七段数码管式学号(202206414115)从窗口右侧向左侧滚动,从左侧移出后重新出现在右侧,循环往复
时间: 2024-03-01 19:56:05 浏览: 85
好的,以下是代码实现:
```python
import turtle
import time
# 定义数字显示的规则,0~9的七段数码管
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
]
# 定义单个数字的显示
def draw_digit(num, x, y, size):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
if digit[num][0] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.right(90)
if digit[num][1] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.right(90)
if digit[num][2] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.right(90)
if digit[num][3] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.right(90)
if digit[num][4] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.right(90)
if digit[num][5] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.right(90)
if digit[num][6] == 1:
turtle.forward(size)
else:
turtle.penup()
turtle.forward(size)
turtle.pendown()
turtle.penup()
turtle.right(90)
turtle.forward(size)
turtle.right(90)
turtle.forward(size*4/3)
turtle.right(180)
# 定义滚动显示学号
def show_number(number, size):
turtle.speed(0) # 设置绘制速度为最快
turtle.hideturtle() # 隐藏乌龟光标
turtle.setup(width=800, height=200) # 设置窗口大小
turtle.tracer(False) # 关闭绘图缓存
while True:
turtle.clear() # 清除上一次绘图内容
for i, digit in enumerate(str(number)):
draw_digit(int(digit), i*size*4/3 - size*2, 0, size)
turtle.update() # 更新绘图内容
time.sleep(0.2) # 暂停一段时间,控制滚动速度
number = number % 1000000000 * 10 + number // 1000000000 # 循环滚动学号
# 测试代码
if __name__ == '__main__':
show_number(202206414115, 20)
```
你可以将上述代码保存为一个 Python 文件并运行。在运行过程中,会在窗口中不断滚动显示你的学号。你可以根据需要调整数字显示的大小、滚动速度等参数。
阅读全文