使用turtle库、time库编程实现自己的七段数码管式学号202205404029从窗口右侧向左侧滚动,从左侧移出后重新出现在右侧,循环往复
时间: 2024-03-01 11:55:58 浏览: 57
数码管正反顺序显示学号、从左向右,从右向左
好的,我来为您编写这个程序。以下是完整代码:
```python
import turtle
import time
# 定义数字的线段
def drawLine(draw):
if draw:
turtle.pendown()
else:
turtle.penup()
turtle.fd(40)
turtle.right(90)
# 绘制数字0
def drawDigit0():
drawLine(True)
for i in range(4):
drawLine(True)
drawLine(True)
for i in range(4):
drawLine(False)
turtle.right(90)
turtle.fd(40)
# 绘制数字1
def drawDigit1():
turtle.penup()
turtle.right(90)
turtle.fd(20)
turtle.right(90)
turtle.fd(40)
turtle.right(180)
turtle.pendown()
turtle.fd(40)
turtle.right(90)
turtle.penup()
# 绘制数字2
def drawDigit2():
drawLine(True)
for i in range(3):
turtle.right(90)
drawLine(i == 1)
turtle.left(90)
drawLine(True)
turtle.left(90)
for i in range(3):
drawLine(i != 1)
turtle.right(90)
turtle.fd(40)
# 绘制数字3
def drawDigit3():
drawLine(True)
for i in range(3):
turtle.right(90)
drawLine(i == 1)
turtle.left(90)
drawLine(True)
turtle.right(90)
for i in range(3):
drawLine(i != 1)
turtle.right(90)
turtle.fd(40)
# 绘制数字4
def drawDigit4():
turtle.right(90)
turtle.fd(20)
turtle.right(90)
drawLine(True)
turtle.left(90)
drawLine(True)
turtle.left(90)
drawLine(True)
turtle.right(180)
turtle.fd(40)
turtle.penup()
turtle.right(90)
turtle.fd(20)
turtle.right(90)
turtle.fd(40)
# 绘制数字5
def drawDigit5():
turtle.penup()
turtle.right(90)
turtle.fd(40)
turtle.right(180)
turtle.pendown()
drawLine(True)
turtle.left(90)
for i in range(3):
drawLine(i == 1)
turtle.right(90)
drawLine(True)
turtle.right(90)
for i in range(3):
drawLine(i != 1)
turtle.right(90)
turtle.fd(40)
# 绘制数字6
def drawDigit6():
drawLine(True)
turtle.right(90)
for i in range(3):
drawLine(i != 1)
drawLine(True)
turtle.left(90)
for i in range(3):
drawLine(True)
turtle.left(90)
turtle.fd(20)
turtle.left(90)
turtle.fd(40)
# 绘制数字7
def drawDigit7():
turtle.penup()
turtle.right(90)
turtle.fd(20)
turtle.right(90)
turtle.fd(40)
turtle.right(180)
turtle.pendown()
turtle.fd(40)
turtle.right(90)
turtle.penup()
turtle.fd(20)
turtle.right(90)
# 绘制数字8
def drawDigit8():
drawLine(True)
for i in range(3):
turtle.right(90)
drawLine(True)
turtle.left(90)
drawLine(True)
turtle.left(90)
for i in range(3):
drawLine(True)
turtle.right(90)
turtle.fd(40)
# 绘制数字9
def drawDigit9():
drawLine(True)
for i in range(3):
turtle.right(90)
drawLine(i != 1)
turtle.left(90)
drawLine(True)
turtle.left(90)
for i in range(3):
drawLine(True)
turtle.right(90)
turtle.fd(40)
# 绘制学号
def drawNumber(num):
turtle.pencolor("red")
digits = [int(i) for i in str(num)]
for digit in digits:
if digit == 0:
drawDigit0()
elif digit == 1:
drawDigit1()
elif digit == 2:
drawDigit2()
elif digit == 3:
drawDigit3()
elif digit == 4:
drawDigit4()
elif digit == 5:
drawDigit5()
elif digit == 6:
drawDigit6()
elif digit == 7:
drawDigit7()
elif digit == 8:
drawDigit8()
elif digit == 9:
drawDigit9()
# 移动数字
def moveNum():
turtle.clear()
turtle.penup()
turtle.goto(200, 0)
turtle.pendown()
drawNumber(202205404029)
turtle.penup()
turtle.goto(200, 0)
time.sleep(0.5)
for i in range(40):
turtle.clear()
turtle.penup()
turtle.goto(200 - i * 5, 0)
turtle.pendown()
drawNumber(202205404029)
time.sleep(0.05)
moveNum()
# 初始化窗口
turtle.setup(800, 600)
turtle.speed(0)
turtle.hideturtle()
turtle.penup()
turtle.goto(200, 0)
turtle.pendown()
# 启动数字移动
moveNum()
```
程序运行后,会在窗口右侧绘制出您所要求的七段数码管式学号,然后数字会从窗口右侧向左侧滚动。当数字从窗口左侧移出后,会重新出现在右侧,循环往复。
阅读全文