forked from nvmao/bouncing-ball-pybox2d-part-5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.py
More file actions
47 lines (37 loc) · 1.23 KB
/
Particle.py
File metadata and controls
47 lines (37 loc) · 1.23 KB
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
import math
import random
import pygame
from utils import utils
class Particle:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
self.radius = random.uniform(0.5, 2)
angle = random.uniform(0,360)
speed = random.uniform(0.1,1)
self.vel_x = math.cos(math.radians(angle)) * speed
self.vel_y = math.sin(math.radians(angle)) * speed
self.life = random.randint(100, 1000)
def update(self):
self.x += self.vel_x
self.y += self.vel_y
self.life -= 1
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
class Explosion:
def __init__(self,x,y,color):
# Create particles
self.particles = []
COLORS = [color]
for _ in range(20):
color = random.choice(COLORS)
particle = Particle(x,y, color)
self.particles.append(particle)
def update(self):
for particle in self.particles:
particle.update()
self.particles = [particle for particle in self.particles if particle.life > 0]
def draw(self):
for particle in self.particles:
particle.draw(utils.screen)