使用turtle库、time库编程实现自己的七段数码管式学号202205404029滚动显示
时间: 2023-08-14 16:06:11 浏览: 55
好的,让我来给您编写这个程序。
首先,我们需要导入 turtle 和 time 库:
```python
import turtle
import time
```
接下来,我们定义一个函数 `draw_digit(digit, x, y)`,用于绘制单个数字:
```python
def draw_digit(digit, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
if digit in [2, 3, 4, 5, 6, 8, 9]:
turtle.forward(20)
if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9]:
turtle.left(90)
turtle.forward(30)
if digit in [0, 2, 3, 5, 6, 8, 9]:
turtle.right(90)
turtle.forward(20)
if digit in [0, 2, 6, 8]:
turtle.right(90)
turtle.forward(30)
if digit in [0, 4, 5, 6, 8, 9]:
turtle.right(90)
turtle.forward(20)
if digit in [0, 2, 3, 5, 6, 7, 8, 9]:
turtle.right(90)
turtle.forward(30)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
```
然后,我们定义一个函数 `draw_num(num)`,用于绘制整个学号:
```python
def draw_num(num):
turtle.clear()
turtle.speed(0)
turtle.pensize(5)
x = -200
y = 0
for digit in num:
draw_digit(int(digit), x, y)
x += 50
turtle.hideturtle()
```
最后,我们可以使用一个无限循环来不断滚动显示学号:
```python
while True:
for i in range(11):
draw_num('202205404029')
time.sleep(0.5)
turtle.clear()
time.sleep(0.5)
```
完整代码如下:
```python
import turtle
import time
def draw_digit(digit, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
if digit in [2, 3, 4, 5, 6, 8, 9]:
turtle.forward(20)
if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9]:
turtle.left(90)
turtle.forward(30)
if digit in [0, 2, 3, 5, 6, 8, 9]:
turtle.right(90)
turtle.forward(20)
if digit in [0, 2, 6, 8]:
turtle.right(90)
turtle.forward(30)
if digit in [0, 4, 5, 6, 8, 9]:
turtle.right(90)
turtle.forward(20)
if digit in [0, 2, 3, 5, 6, 7, 8, 9]:
turtle.right(90)
turtle.forward(30)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
def draw_num(num):
turtle.clear()
turtle.speed(0)
turtle.pensize(5)
x = -200
y = 0
for digit in num:
draw_digit(int(digit), x, y)
x += 50
turtle.hideturtle()
while True:
for i in range(11):
draw_num('202205404029')
time.sleep(0.5)
turtle.clear()
time.sleep(0.5)
```
运行程序,您将看到学号以七段数码管的形式滚动显示。
相关推荐
![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)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)