python动态爱心烟花代码
时间: 2023-09-12 09:03:08 浏览: 143
下面是一个简单的Python动态爱心烟花代码:
```python
import turtle
import random
# 设置画布大小
turtle.setup(800, 600)
# 初始化画笔
pen = turtle.Turtle()
pen.hideturtle()
# 定义红色心形函数
def draw_heart(x, y, size):
pen.up()
pen.goto(x, y)
pen.down()
pen.begin_fill()
pen.fillcolor("red")
pen.pensize(size)
pen.pencolor("red")
for i in range(200):
pen.right(1)
pen.forward(size)
pen.left(140)
for i in range(200):
pen.right(1)
pen.forward(size)
pen.end_fill()
# 定义烟花函数
def firework():
# 随机生成爆炸位置
x = random.randint(-350, 350)
y = random.randint(-250, 250)
size = random.randint(10, 30)
# 绘制爆炸效果
for i in range(30):
pen.up()
pen.goto(x, y)
pen.down()
pen.pensize(random.randint(1, 5))
pen.pencolor("white")
pen.setheading(random.randint(0, 360))
pen.forward(random.randint(10, 20))
# 绘制心形
draw_heart(x, y - 50, size)
# 循环绘制烟花效果
while True:
firework()
```
运行代码后会出现一个画布,随机出现爆炸效果和红色心形。
阅读全文