python绘制七彩数码管
时间: 2023-10-12 19:22:08 浏览: 231
下面是Python代码实现七彩数码管的绘制:
```python
import turtle
import time
# 数码管的编码,0~9
digitals = (
(1, 1, 1, 1, 1, 1, 0), # 0
(0, 1, 1, 0, 0, 0, 0), # 1
(1, 1, 0, 1, 1, 0, 1), # 2
(1, 1, 1, 1, 0, 0, 1), # 3
(0, 1, 1, 0, 0, 1, 1), # 4
(1, 0, 1, 1, 0, 1, 1), # 5
(1, 0, 1, 1, 1, 1, 1), # 6
(1, 1, 1, 0, 0, 0, 0), # 7
(1, 1, 1, 1, 1, 1, 1), # 8
(1, 1, 1, 1, 0, 1, 1), # 9
)
# 数码管的颜色
colors = ('red', 'orange', 'yellow', 'green', 'blue', 'purple')
# 绘制单个数码管
def draw_single_digital(digital, size, color):
turtle.pencolor(color)
turtle.pensize(size)
turtle.penup()
turtle.fd(size * 2)
turtle.pendown()
for i in range(7):
if digital[i]:
turtle.fd(size)
else:
turtle.penup()
turtle.fd(size)
turtle.pendown()
if i == 2 or i == 4:
turtle.right(90)
else:
turtle.right(180)
turtle.penup()
turtle.left(90)
turtle.fd(size * 4)
turtle.right(90)
# 绘制七彩数码管
def draw_colorful_digital(number, size):
turtle.speed(0)
turtle.hideturtle()
turtle.penup()
turtle.bk(size * 4 * len(str(number)))
turtle.pendown()
for i in range(len(str(number))):
draw_single_digital(digitals[int(str(number)[i])], size, colors[i % 6])
turtle.done()
# 测试
if __name__ == '__main__':
number = input('请输入数字:')
size = int(input('请输入数码管大小:'))
draw_colorful_digital(number, size)
```
运行代码后,会提示输入数字和数码管大小。输入完后,就会绘制出七彩数码管。
效果如下图所示:
注:本代码使用Python的turtle库进行绘制,需要安装该库。
阅读全文