用Python写一个爱心跳动的代码
时间: 2023-11-18 09:11:46 浏览: 125
好的,以下是使用Python的turtle库编写的爱心跳动代码:
```python
import turtle
import math
# 初始化窗口
canvas = turtle.Screen()
canvas.title("爱心跳动")
canvas.bgcolor("black")
# 初始化画笔
pen = turtle.Turtle()
pen.color("red")
pen.shape("turtle")
pen.speed(0)
pen.penup()
# 定义爱心函数
def heart(x, y, size):
pen.goto(x, y)
pen.pendown()
pen.begin_fill()
pen.fillcolor("red")
pen.left(45)
pen.forward(200*size)
pen.circle(70*size, 180)
pen.right(90)
pen.circle(70*size, 180)
pen.forward(200*size)
pen.end_fill()
# 定义心跳函数
def heartbeat(x, y, size):
for i in range(10):
pen.goto(x, y)
pen.pendown()
pen.pensize(3)
pen.color("red")
pen.circle(10*size*(1+i/10))
pen.penup()
pen.goto(x+20*size*(1+i/10), y)
pen.pendown()
pen.circle(10*size*(1+i/10))
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.pensize(1)
pen.color("white")
pen.circle(10*size*(1+i/10))
pen.penup()
pen.goto(x+20*size*(1+i/10), y)
pen.pendown()
pen.circle(10*size*(1+i/10))
pen.penup()
# 绘制爱心
heart(0, 0, 1)
# 循环绘制心跳
while True:
heartbeat(0, 0, 1)
pen.reset()
heart(0, 0, 1)
```
运行代码后,你将看到一个闪烁着红色心形和跳动着的白色圆圈的窗口。
阅读全文