使用turtle库、time库编程实 现自己的七段数码管式学号从窗口右侧 向左侧滚动,从左侧移出后重新出现在 右侧,循环往复
时间: 2024-03-01 11:56:06 浏览: 61
好的,以下是使用turtle库和time库编写的代码实现你的要求:
```python
import turtle
import time
# 设置画布和画笔
window = turtle.Screen()
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.penup()
# 数字画笔列表
pens = [
# 0
[
(0, 50), (50, 50), (50, 0), (0, 0), (0, 50)
],
# 1
[
(25, 50), (25, 0)
],
# 2
[
(0, 50), (50, 50), (50, 25), (0, 25), (0, 0), (50, 0)
],
# 3
[
(0, 50), (50, 50), (50, 25), (0, 25), (50, 25), (50, 0), (0, 0)
],
# 4
[
(0, 50), (0, 25), (50, 25), (50, 50), (50, 0)
],
# 5
[
(50, 50), (0, 50), (0, 25), (50, 25), (50, 0), (0, 0)
],
# 6
[
(50, 50), (0, 50), (0, 0), (50, 0), (50, 25), (0, 25)
],
# 7
[
(0, 50), (50, 50), (50, 0)
],
# 8
[
(0, 50), (50, 50), (50, 0), (0, 0), (0, 25), (50, 25)
],
# 9
[
(50, 0), (0, 0), (0, 50), (50, 50), (50, 25), (0, 25)
]
]
# 绘制数字的函数
def draw_num(num):
global pen
pen.pendown()
for p in pens[num]:
pen.goto(p)
pen.penup()
pen.goto(pen.xcor() + 60, pen.ycor())
# 学号
student_id = "202000001"
# 循环滚动
while True:
for num in student_id:
draw_num(int(num))
pen.goto(0, 0)
time.sleep(0.5)
pen.clear()
```
这段代码实现了自己的七段数码管式学号从窗口右侧向左侧滚动,从左侧移出后重新出现在右侧,循环往复的效果。其中,使用了turtle库来绘制数字,使用了time库来控制滚动的速度。你可以尝试运行这段代码,看看效果~
阅读全文