使用turtle库、time库编程实现自己的七段数码管式学号从窗口右侧向左侧滚动,从左侧移出后重新出现在右侧,循环往复
时间: 2024-03-01 21:55:49 浏览: 27
好的,我明白了。以下是使用Python的turtle和time库编写的程序,可以实现您描述的七段数码管式学号从窗口右侧向左侧滚动的效果。请将以下代码复制到Python环境中运行:
```python
import turtle
import time
# 定义数字0-9的七段数码管编码
digit_code = [
[1, 1, 1, 0, 1, 1, 1], # 0
[0, 0, 1, 0, 0, 1, 0], # 1
[1, 0, 1, 1, 1, 0, 1], # 2
[1, 0, 1, 1, 0, 1, 1], # 3
[0, 1, 1, 1, 0, 1, 0], # 4
[1, 1, 0, 1, 0, 1, 1], # 5
[1, 1, 0, 1, 1, 1, 1], # 6
[1, 0, 1, 0, 0, 1, 0], # 7
[1, 1, 1, 1, 1, 1, 1], # 8
[1, 1, 1, 1, 0, 1, 1] # 9
]
# 定义每个数字的绘制函数
def draw_digit(digit, x, y, size):
t.penup()
t.goto(x, y)
t.pendown()
# 上
if digit[0] == 1:
t.setheading(90)
t.forward(size)
else:
t.penup()
t.setheading(90)
t.forward(size)
t.pendown()
# 右上
if digit[1] == 1:
t.setheading(0)
t.forward(size)
else:
t.penup()
t.setheading(0)
t.forward(size)
t.pendown()
# 右下
if digit[2] == 1:
t.setheading(-90)
t.forward(size)
else:
t.penup()
t.setheading(-90)
t.forward(size)
t.pendown()
# 下
if digit[3] == 1:
t.setheading(-180)
t.forward(size)
else:
t.penup()
t.setheading(-180)
t.forward(size)
t.pendown()
# 左下
if digit[4] == 1:
t.setheading(-270)
t.forward(size)
else:
t.penup()
t.setheading(-270)
t.forward(size)
t.pendown()
# 左上
if digit[5] == 1:
t.setheading(180)
t.forward(size)
else:
t.penup()
t.setheading(180)
t.forward(size)
t.pendown()
# 中
if digit[6] == 1:
t.penup()
t.setheading(90)
t.forward(size)
t.setheading(0)
t.pendown()
t.forward(size)
else:
t.penup()
t.setheading(90)
t.forward(size)
t.setheading(0)
t.forward(size)
t.pendown()
# 定义滚动函数
def scroll_number(number, x, y, size):
for i in range(len(number)):
draw_digit(digit_code[int(number[i])], x + i * size * 4, y, size)
for i in range(40):
t.clear()
for j in range(len(number)):
draw_digit(digit_code[int(number[j])], x + (i - 40 + j) * size * 4, y, size)
time.sleep(0.05)
# 初始化turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
# 调用滚动函数,传入学号和起始坐标
scroll_number("202102", 200, 0, 50)
```
这段代码首先定义了数字0-9的七段数码管编码,以及每个数字的绘制函数。然后定义了滚动函数,该函数接受一个数字字符串、起始坐标和大小作为参数,可以将该数字以指定的大小和间距绘制出来,并实现从右向左滚动的效果。最后在初始化turtle后,调用滚动函数,传入学号和起始坐标,即可看到七段数码管式学号从窗口右侧向左侧滚动的效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)