爱心粒子代码Python
时间: 2023-11-30 09:43:06 浏览: 194
以下是一个使用turtle库绘制爱心粒子的Python代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('black')
# 定义爱心粒子的形状
heart = turtle.Turtle()
heart.hideturtle()
heart.speed(0)
heart.penup()
heart.color('white')
heart.goto(0, 0)
heart.getscreen().register_shape('heart', ((-5, 3), (-3, 5), (0, 7), (3, 5), (5, 3), (7, 0), (5, -3), (3, -5), (0, -7), (-3, -5), (-5, -3), (-7, 0), (-5, 3)))
# 定义爱心粒子的运动轨迹
particles = []
for _ in range(50):
particle = turtle.Turtle()
particle.hideturtle()
particle.speed(0)
particle.penup()
particle.shape('heart')
particle.color(random.choice(['white', 'pink', 'red', 'purple']))
particle.goto(random.randint(-400, 400), random.randint(-300, 300))
particle.showturtle()
particles.append(particle)
while True:
for particle in particles:
particle.goto(particle.xcor() + random.randint(-10, 10), particle.ycor() + random.randint(-10, 10))
# 点击关闭窗口
turtle.done()
```
阅读全文