使用turtle库画太极图
时间: 2023-07-11 17:30:33 浏览: 154
用python的turtle库画太极八卦阴阳图画奥运五环画玫瑰花
太极图是一种传统的中国文化图案,可以使用turtle库轻松绘制。以下是绘制太极图的代码示例:
```python
import turtle
# 绘制半圆
def draw_half_circle(radius, color):
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(radius, 180)
turtle.circle(radius/2, 180)
turtle.end_fill()
# 绘制太极图
def draw_taiji(size):
turtle.speed(0)
turtle.penup()
turtle.goto(0, -size)
turtle.pendown()
# 绘制黑白两个半圆
draw_half_circle(size, "black")
draw_half_circle(size, "white")
# 绘制两个小圆和两条直线
turtle.fillcolor("black")
turtle.penup()
turtle.goto(0, -size/2)
turtle.pendown()
turtle.begin_fill()
turtle.circle(size/4)
turtle.end_fill()
turtle.fillcolor("white")
turtle.penup()
turtle.goto(0, size/2)
turtle.pendown()
turtle.begin_fill()
turtle.circle(size/4)
turtle.end_fill()
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.pensize(3)
turtle.right(90)
turtle.forward(size/2)
turtle.right(90)
turtle.forward(size)
turtle.right(180)
turtle.forward(size/2)
turtle.right(90)
turtle.forward(size/4)
turtle.right(90)
turtle.forward(size/2)
# 隐藏画笔
turtle.hideturtle()
# 设置画布和画笔
turtle.setup(500, 500)
turtle.pensize(2)
# 绘制太极图
draw_taiji(200)
# 点击画布退出
turtle.exitonclick()
```
运行后,可以看到一个黑白相间的太极图。点击画布可以退出程序。
阅读全文