python代码画爱心
时间: 2023-10-06 09:06:30 浏览: 40
以下是Python代码画爱心的示例:
```python
import turtle
# 设置画布大小和背景颜色
turtle.setup(600, 600)
turtle.bgcolor("black")
# 设置画笔颜色和形状
turtle.pencolor("red")
turtle.shape("turtle")
# 定义画心函数
def draw_heart():
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
# 画出两个心
turtle.penup()
turtle.goto(-150, 0)
turtle.pendown()
draw_heart()
turtle.penup()
turtle.goto(150, 0)
turtle.pendown()
draw_heart()
# 隐藏画笔
turtle.hideturtle()
# 完成绘制
turtle.done()
```
运行代码,即可在画布上看到两个红色的爱心:
![python-heart](https://user-images.githubusercontent.com/46395514/115982624-2d1c2280-a5c9-11eb-9cd9-7a00e3fc76e3.png)
阅读全文