python用turtle画火柴人发射爱心
时间: 2023-11-02 16:06:53 浏览: 113
下面是使用Python的turtle模块画火柴人发射爱心的示例代码:
```python
import turtle
# 设置画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("white")
pen = turtle.Turtle()
# 画火柴人
pen.color("black")
pen.pensize(4)
pen.penup()
pen.goto(0, -100)
pen.pendown()
pen.circle(100)
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.right(90)
pen.forward(100)
pen.left(120)
pen.forward(80)
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.right(60)
pen.forward(80)
pen.penup()
pen.goto(0, -20)
pen.pendown()
pen.right(30)
pen.forward(40)
pen.penup()
pen.goto(0, -20)
pen.pendown()
pen.left(60)
pen.forward(40)
# 画爱心
pen.color("red")
pen.penup()
pen.goto(-100, 50)
pen.pendown()
pen.begin_fill()
pen.left(45)
pen.forward(70)
pen.right(135)
pen.circle(50, -180)
pen.right(135)
pen.forward(70)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
```
运行以上代码,可以得到一个火柴人发射爱心的图形,如下所示:
![火柴人发射爱心](https://img-blog.csdnimg.cn/20210704095545363.png)
阅读全文