python turtle画药丸
时间: 2023-07-09 15:52:43 浏览: 64
好的,下面是 Python turtle 绘制药丸的代码:
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('white')
# 定义绘制药丸的函数
def draw_pill(x, y, width, height, color):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
turtle.begin_fill()
# 绘制药丸的矩形部分
for i in range(2):
turtle.fd(width)
turtle.lt(90)
turtle.fd(height)
turtle.lt(90)
turtle.end_fill()
# 绘制药丸的圆形部分
turtle.penup()
turtle.goto(x + width/2, y + height/2)
turtle.pendown()
turtle.color('white')
turtle.begin_fill()
turtle.circle(width/4)
turtle.end_fill()
turtle.penup()
turtle.goto(x - width/2, y - height/2)
turtle.pendown()
turtle.begin_fill()
turtle.circle(width/4)
turtle.end_fill()
# 调用绘制药丸的函数
draw_pill(0, 0, 100, 50, 'purple')
```
代码中使用了 turtle 库,通过定义 draw_pill 函数绘制药丸。函数接收药丸的位置、宽度、高度和颜色等参数,先绘制药丸的矩形部分,再绘制药丸的圆形部分。药丸的矩形部分使用填充,圆形部分使用白色填充。运行代码,即可在 turtle 画布上看到药丸的效果。