python七段数码管绘制数字二代码
时间: 2023-11-04 10:33:44 浏览: 85
七段数码管绘制
```python
import turtle
# 7段数码管的线段坐标数据
segments = [
[(0, 0), (0, 20)], # a
[(0, 20), (20, 20)], # b
[(20, 20), (20, 40)], # c
[(20, 40), (0, 40)], # d
[(0, 40), (0, 60)], # e
[(0, 60), (20, 60)], # f
[(20, 60), (20, 80)], # g
]
def draw_segment(x, y, segment):
# 在位置 (x, y) 画出一个线段
turtle.penup()
turtle.goto(x + segment[0][0], y + segment[0][1])
turtle.pendown()
for point in segment[1:]:
turtle.goto(x + point[0], y + point[1])
def draw_digit(digit, x=0, y=0):
# 绘制一个数字
if digit in [2, 3, 4, 5, 6, 8, 9]:
draw_segment(x, y, segments[0]) # a
if digit in [0, 3, 4, 5, 6, 7, 8, 9]:
draw_segment(x, y, segments[1]) # b
if digit in [0, 1, 2, 3, 4, 7, 8, 9]:
draw_segment(x, y, segments[2]) # c
if digit in [0, 2, 3, 5, 6, 8, 9]:
draw_segment(x, y, segments[3]) # d
if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9]:
draw_segment(x, y, segments[4]) # e
if digit in [0, 2, 6, 8]:
draw_segment(x, y, segments[5]) # f
if digit in [0, 1, 4, 7]:
draw_segment(x, y, segments[6]) # g
def draw_number(number):
# 绘制一个数字串
for digit in number:
draw_digit(int(digit))
turtle.penup()
turtle.forward(40)
turtle.pendown()
# 测试代码
turtle.speed(0)
turtle.hideturtle()
turtle.penup()
turtle.goto(-200, 0)
turtle.pendown()
draw_number('2022')
turtle.done()
```
运行结果:
![seven-segment-display](https://cdn.luogu.com.cn/upload/image_hosting/w54v56mn.png)
阅读全文