用python写一个爱心
时间: 2024-05-08 21:19:23 浏览: 63
非常感谢您的提问!以下是用 Python 编写一个爱心的代码:
```python
import turtle
# 设置画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("black")
pen = turtle.Turtle()
pen.color("red")
pen.shape("turtle")
pen.speed(0)
# 绘制爱心
def draw_heart():
for i in range(200):
pen.right(1)
pen.forward(2)
if i == 0:
pen.color("blue")
if i == 50:
pen.color("green")
if i == 100:
pen.color("yellow")
if i == 150:
pen.color("orange")
if i == 199:
pen.color("red")
pen.right(140)
pen.forward(5)
# 移动画笔到绘制爱心的起点
pen.penup()
pen.goto(0, -150)
pen.pendown()
# 绘制爱心
draw_heart()
# 隐藏画笔
pen.hideturtle()
# 关闭画布
canvas.exitonclick()
```
希望这个代码能够带给您快乐和惊喜!如果您有更多的问题,可以随时向我提问哦!
阅读全文