python粒子爱心特效代码
时间: 2023-10-23 13:14:04 浏览: 665
以下是一个简单的 Python 粒子爱心特效代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('black')
# 定义粒子类
class Particle(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self, shape='circle')
self.penup()
self.speed(0)
self.color(random.choice(['red', 'pink', 'white', 'purple']))
self.goto(0, 200)
self.direction = random.randint(0, 360)
self.velocity = random.randint(1, 4)
def move(self):
x = self.xcor() + self.velocity * math.cos(math.radians(self.direction))
y = self.ycor() + self.velocity * math.sin(math.radians(self.direction))
self.goto(x, y)
# 如果粒子超出画布范围,则将其移动到画布中心
if abs(self.xcor()) > 400 or abs(self.ycor()) > 300:
self.goto(0, 200)
self.direction = random.randint(0, 360)
# 创建粒子列表
particles = []
for i in range(50):
particles.append(Particle())
# 循环移动粒子
while True:
for particle in particles:
particle.move()
# 隐藏海龟
turtle.hideturtle()
# 相关问题:
阅读全文