-
Notifications
You must be signed in to change notification settings - Fork 0
/
FireworkParticle.py
71 lines (55 loc) · 2.34 KB
/
FireworkParticle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""This code adapted from a project by Adam Binks -- see his repo here: https://github.com/adam-binks/Fireworks"""
import random
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame as pg
def isOnScreen(pos, WindowDimensions):
for axis in (0, 1):
if pos[axis] < 0 or pos[axis] > WindowDimensions[axis]:
return False
return True
# hasTrail is whether this firework has the little different coloured trailer particles too
class Particle:
allParticles = []
def __init__(self, pos, colour, direction, velocity, size, lifetime, WindowDimensions,
hasTrail=False, shrink=False,
trailColour=None, trailPercent=0.4, gravity=0.005):
self.pos = [float(pos[0]), float(pos[1])] # (x, y)
self.colour = colour
self.direction = direction # (x movement, y movement)
self.velocity = velocity
self.size = size
self.hasTrail = hasTrail
self.lifetime = lifetime
self.age = 0
self.shrink = shrink
self.gravity = gravity
self.WindowDimensions = WindowDimensions
self.surface = pg.Surface((size, size))
self.surface.fill(self.colour)
Particle.allParticles.append(self)
if hasTrail and random.uniform(0, 1) < trailPercent:
self.spawnTrail(trailColour)
def update(self, dt):
# move
for axis in (0, 1):
self.pos[axis] += self.direction[axis] * self.velocity * dt
# gravity acts
self.direction[1] += self.gravity * dt
# check if needs to die (off screen or lifetime is over)
self.age += dt
if not isOnScreen(self.pos, self.WindowDimensions) or self.age > self.lifetime:
self.die()
return
if self.shrink:
newSurfSize = self.size*(1 - self.age/self.lifetime)
self.surface = pg.Surface((newSurfSize, newSurfSize))
self.surface.fill(self.colour)
def draw(self, screen):
screen.blit(self.surface, self.pos)
def die(self):
if self in Particle.allParticles:
Particle.allParticles.remove(self)
def spawnTrail(self, trailColour):
Particle(self.pos, trailColour, self.direction, self.velocity*2, self.size*0.25,
lifetime=self.lifetime*2.5, WindowDimensions=self.WindowDimensions)