python用turtle写电子数字
时间: 2024-05-18 21:18:42 浏览: 96
可以使用turtle库来绘制电子数字,下面是一个示例代码,可以画出0到9的电子数字:
```python
import turtle
def draw_segment(x, y, angle, length):
turtle.penup()
turtle.goto(x, y)
turtle.setheading(angle)
turtle.pendown()
turtle.forward(length)
def draw_0(x, y, size):
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x - size, y - size, 0, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x + size, y + size, 180, 2 * size)
draw_segment(x + size, y - size, 180, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
def draw_1(x, y, size):
draw_segment(x, y + size, 180, 2 * size)
draw_segment(x, y - size, 180, 2 * size)
def draw_2(x, y, size):
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x + size, y - size, 180, 2 * size)
draw_segment(x - size, y - size, 0, 2 * size)
def draw_3(x, y, size):
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x, y, 90, 2 * size)
draw_segment(x + size, y - size, 180, 2 * size)
draw_segment(x - size, y - size, 0, 2 * size)
def draw_4(x, y, size):
draw_segment(x - size, y + size, 180, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x, y + size, 180, 2 * size)
draw_segment(x, y - size, 180, 2 * size)
def draw_5(x, y, size):
draw_segment(x + size, y + size, 180, 2 * size)
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x - size, y - size, 0, 2 * size)
def draw_6(x, y, size):
draw_segment(x + size, y + size, 180, 2 * size)
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x + size, y - size, 180, 2 * size)
draw_segment(x - size, y - size, 0, 2 * size)
def draw_7(x, y, size):
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x, y - size, 180, 2 * size)
def draw_8(x, y, size):
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y + size, 180, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x + size, y - size, 180, 2 * size)
draw_segment(x - size, y - size, 0, 2 * size)
def draw_9(x, y, size):
draw_segment(x - size, y + size, 0, 2 * size)
draw_segment(x + size, y + size, 180, 2 * size)
draw_segment(x + size, y, 90, 2 * size)
draw_segment(x - size, y, 90, 2 * size)
draw_segment(x, y - size, 180, 2 * size)
# 绘制电子数字
size = 30
draw_0(-150, 0, size)
draw_1(-90, 0, size)
draw_2(-30, 0, size)
draw_3(30, 0, size)
draw_4(90, 0, size)
draw_5(150, 0, size)
draw_6(210, 0, size)
draw_7(270, 0, size)
draw_8(330, 0, size)
draw_9(390, 0, size)
turtle.done()
```
这段代码将绘制0到9的电子数字并排显示在屏幕上。你可以根据需要修改绘制的数字的位置和大小。
阅读全文