-
Notifications
You must be signed in to change notification settings - Fork 0
/
particleSystem.py
80 lines (59 loc) · 2.31 KB
/
particleSystem.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
import pygame
class Particle(pygame.sprite.Sprite):
def __init__(self, x, y, angle, length, width=0, height=0, color=(255,255,255), image=None, gravity=True, grav=(0,0.1)):
"""Init
Args:\n
x : pos x
y : pos y
angle : angle where the particle will go
length : speed vector
width (int, optional): width of the particle. Defaults to 0.
height (int, optional): height of the particle. Defaults to 0.
color (tuple, optional): color of the particle. Defaults to (255,255,255).
image (pygame.Surface, optional): image of the particle. Defaults to None.
gravity (bool, optional): is the particle has gravity. Defaults to True.
"""
super().__init__()
if image == None:
self.image = pygame.Surface([width, height])
self.image.fill(color)
else:
self.image = image
self.rect = self.image.get_rect()
self.pos = pygame.math.Vector2(x, y)
self.speed = pygame.math.Vector2(0,0)
self.speed.from_polar((length, angle))
self.acceleration = pygame.math.Vector2(0,0)
if gravity:
self.gravity = pygame.math.Vector2(grav[0], grav[1])
else:
self.gravity = pygame.math.Vector2(0, 0)
def set_color(self, color):
self.image.fill(color)
def update(self, dt=1):
self.speed += self.acceleration
self.speed += self.gravity
self.pos += self.speed * dt * 10
self.rect.x = int(self.pos.x)
self.rect.y = int(self.pos.y)
def draw(self, screen):
screen.blit_cam(self.image, self.rect)
def draw_raw(self, screen):
screen.blit(self.image, self.rect)
def get_pos(self):
return (self.pos.x, self.pos.y)
class ParticleSystem(pygame.sprite.Group):
def __init__(self, raw=False):
super().__init__()
self.has_finish = False
self.raw = raw
def draw(self, screen):
if self.raw:
for sprite in self.sprites():
sprite.draw_raw(screen)
else:
for sprite in self.sprites():
sprite.draw(screen)
def remove_first(self, screen):
if (len(self.sprites()) > 0):
self.sprites()[0].kill()