Python模拟电梯七段数码管绘制显示编程 利用turtle函数 假设你要乘坐电梯,假设只有一部电梯,其显示面板上有七段数码管显味 电梯当前所在楼层。假设你的学号为M, 且第(M%7+1)段数码管坏了(一直是亮的),问题:你现在在1楼,电梯停在 (M与N均小于等于9)请模拟你按了电梯后,到你到达 M楼,你要去N楼, N楼这个过程中,显示面饭上七段数码管的显示过程(假设这期间没有其他人 按电梯)
时间: 2024-03-06 17:47:40 浏览: 104
好的,我理解了你的问题。以下是 Python 代码实现:
```python
import turtle
import time
# 设置数码管的位置和大小
def drawLine(draw, x1, y1, x2, y2):
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.goto(x2, y2)
turtle.penup()
# 绘制数字 0
def drawZero():
drawLine(True, -30, 60, -30, 10)
drawLine(True, -30, 10, 30, 10)
drawLine(True, 30, 10, 30, 60)
drawLine(True, 30, 60, -30, 60)
# 绘制数字 1
def drawOne():
drawLine(True, 0, 60, 0, 10)
# 绘制数字 2
def drawTwo():
drawLine(True, -30, 60, 30, 60)
drawLine(True, 30, 60, 30, 35)
drawLine(True, 30, 35, -30, 35)
drawLine(True, -30, 35, -30, 10)
drawLine(True, -30, 10, 30, 10)
# 绘制数字 3
def drawThree():
drawLine(True, -30, 60, 30, 60)
drawLine(True, 30, 60, 30, 35)
drawLine(True, 30, 35, -30, 35)
drawLine(True, 30, 35, 30, 10)
drawLine(True, -30, 10, 30, 10)
# 绘制数字 4
def drawFour():
drawLine(True, -30, 60, -30, 35)
drawLine(True, -30, 35, 30, 35)
drawLine(True, 30, 60, 30, 35)
drawLine(True, 30, 35, 30, 10)
# 绘制数字 5
def drawFive():
drawLine(True, 30, 60, -30, 60)
drawLine(True, -30, 60, -30, 35)
drawLine(True, -30, 35, 30, 35)
drawLine(True, 30, 35, 30, 10)
drawLine(True, 30, 10, -30, 10)
# 绘制数字 6
def drawSix():
drawLine(True, 30, 60, -30, 60)
drawLine(True, -30, 60, -30, 10)
drawLine(True, -30, 10, 30, 10)
drawLine(True, 30, 10, 30, 35)
drawLine(True, 30, 35, -30, 35)
# 绘制数字 7
def drawSeven():
drawLine(True, -30, 60, 30, 60)
drawLine(True, 30, 60, 0, 10)
# 绘制数字 8
def drawEight():
drawLine(True, -30, 60, -30, 10)
drawLine(True, -30, 10, 30, 10)
drawLine(True, 30, 10, 30, 60)
drawLine(True, 30, 60, -30, 60)
drawLine(True, -30, 35, 30, 35)
# 绘制数字 9
def drawNine():
drawLine(True, 30, 60, -30, 60)
drawLine(True, -30, 60, -30, 35)
drawLine(True, -30, 35, 30, 35)
drawLine(True, 30, 35, 30, 60)
drawLine(True, 30, 35, -30, 10)
# 绘制数字
def drawDigit(digit):
if digit == 0:
drawZero()
elif digit == 1:
drawOne()
elif digit == 2:
drawTwo()
elif digit == 3:
drawThree()
elif digit == 4:
drawFour()
elif digit == 5:
drawFive()
elif digit == 6:
drawSix()
elif digit == 7:
drawSeven()
elif digit == 8:
drawEight()
elif digit == 9:
drawNine()
# 绘制当前楼层
def drawFloor(floor):
turtle.clear()
turtle.pensize(5)
turtle.pencolor("blue")
drawDigit(floor)
# 电梯运行到指定楼层
def elevatorMove(curFloor, targetFloor):
if curFloor == targetFloor:
return
elif curFloor < targetFloor:
for i in range(curFloor, targetFloor):
drawFloor(i)
time.sleep(0.5)
else:
for i in range(curFloor, targetFloor, -1):
drawFloor(i)
time.sleep(0.5)
# 模拟按电梯
def pressElevator(M, N):
turtle.speed(0)
turtle.hideturtle()
# 坏掉的数码管
badDigit = (M % 7) + 1
# 当前楼层
curFloor = 1
# 前一次绘制的数字
prevDigit = -1
while curFloor != M:
drawFloor(curFloor)
curFloor += 1
time.sleep(0.5)
drawFloor(M)
time.sleep(1)
while curFloor != N:
drawFloor(curFloor)
digit = curFloor % 10
if digit == badDigit:
continue
if prevDigit != digit:
drawDigit(digit)
prevDigit = digit
curFloor += 1
time.sleep(0.5)
drawFloor(N)
time.sleep(1)
turtle.clear()
# 测试
pressElevator(4, 8)
```
在这个程序中,我们使用 Turtle 模块来绘制数码管,每次绘制一个数字时,都会清除之前的数字。我们模拟了按电梯的过程,从当前楼层到目标楼层,期间不显示坏掉的数码管。你可以替换 `pressElevator(4, 8)` 中的参数来测试其他的楼层。
阅读全文