-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.py
107 lines (89 loc) · 2.97 KB
/
particles.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import pygame
from random import randint
from constants import SCREEN_WIDTH, SCREEN_HEIGHT
class Particle(pygame.sprite.Sprite):
def __init__(self,
groups: pygame.sprite.Group,
pos: list[int],
color: str,
direction: pygame.math.Vector2,
speed: int):
super().__init__(groups)
self.pos = pos
self.color = color
self.direction = direction
self.speed = speed
self.alpha = 255
self.fade_speed = 200
self.size = 4
self.create_surf()
def create_surf(self):
self.image = pygame.Surface((self.size, self.size)).convert_alpha()
self.image.set_colorkey("black")
pygame.draw.circle(surface=self.image, color=self.color, center=(self.size / 2, self.size / 2), radius=self.size / 2)
self.rect = self.image.get_rect(center=self.pos)
def move(self, dt):
self.pos += self.direction * self.speed * dt
self.rect.center = self.pos
def fade(self, dt):
self.alpha -= self.fade_speed * dt
self.image.set_alpha(self.alpha)
def check_pos(self):
if (
self.pos[0] < -50 or
self.pos[0] > SCREEN_WIDTH + 50 or
self.pos[1] < -50 or
self.pos[1] > SCREEN_HEIGHT + 50
):
self.kill()
def check_alpha(self):
if self.alpha <= 0:
self.kill()
def update(self, dt):
self.move(dt)
self.fade(dt)
self.check_pos()
self.check_alpha()
class ExplodingParticle(Particle):
def __init__(self,
groups: pygame.sprite.Group,
pos: list[int],
color: str,
direction: pygame.math.Vector2,
speed: int):
super().__init__(groups, pos, color, direction, speed)
self.t0 = pygame.time.get_ticks()
self.lifetime = randint(1000, 1200)
self.exploding = False
self.size = 4
self.max_size = 50
self.inflate_speed = 500
self.fade_speed = 3000
def explosion_timer(self):
if not self.exploding:
t = pygame.time.get_ticks()
if t - self.t0 > self.lifetime:
self.exploding = True
def inflate(self, dt):
self.size += self.inflate_speed * dt
self.create_surf()
def check_size(self):
if self.size > self.max_size:
self.kill()
def update(self, dt):
self.move(dt)
self.explosion_timer()
if self.exploding:
self.inflate(dt)
self.fade(dt)
self.check_pos()
self.check_size()
self.check_alpha()
class FloatingParticle(Particle):
def __init__(self,
groups: pygame.sprite.Group,
pos: list[int],
color: str,
direction: pygame.math.Vector2,
speed: int):
super().__init__(groups, pos, color, direction, speed)