分类: Python/Ruby
2023-01-17 17:05:54
class Particle:
def __init__(self, x, y, firework, colour):
self.firework = firework
self.pos = vector(x, y)
self.origin = vector(x, y)
self.radius = 20
self.remove = False
self.explosion_radius = randint(5, 18)
self.life = 0
self.acc = vector(0, 0)
# trail variables
self.trails = [] # stores the particles trail objects
self.prev_posx = [-10] * 10 # stores the 10 last positions
self.prev_posy = [-10] * 10 # stores the 10 last positions
if self.firework:
self.vel = vector(0, -randint(17, 20))
self.size = 5
self.colour = colour
for i in range(5):
self.trails.append(Trail(i, self.size, True))
else:
self.vel = vector(uniform(-1, 1), uniform(-1, 1))
self.vel.x *= randint(7, self.explosion_radius + 2)
self.vel.y *= randint(7, self.explosion_radius + 2)
# 向量
self.size = randint(2, 4)
self.colour = choice(colour)
# 5 个 tails总计
for i in range(5):
self.trails.append(Trail(i, self.size, False))
def apply_force(self, force):
self.acc += force
def move(self):
if not self.firework:
self.vel.x *= 0.8
self.vel.y *= 0.8
self.vel += self.acc
self.pos += self.vel
self.acc *= 0
if self.life == 0 and not self.firework: # check if particle is outside explosion radius
distance = 外汇跟单gendan5.commath.sqrt((self.pos.x - self.origin.x) ** 2 + (self.pos.y - self.origin.y) ** 2)
if distance > self.explosion_radius:
self.remove = True
self.decay()
self.trail_update()
self.life += 1
def show(self, win):
pygame.draw.circle(win, (self.colour[0], self.colour[1], self.colour[2], 0), (int(self.pos.x), int(self.pos.y)),
self.size)
def decay(self): # random decay of the particles
if 50 > self.life > 10: # early stage their is a small chance of decay
ran = randint(0, 30)
if ran == 0:
self.remove = True
elif self.life > 50:
ran = randint(0, 5)
if ran == 0:
self.remove = True