-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparticle.py
119 lines (99 loc) · 3.83 KB
/
particle.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
108
109
110
111
112
113
114
115
116
117
118
119
from image_manager import ImageManager
from primitives import Pose
import random
import math
import pygame
import constants as c
from pyracy.sprite_tools import Sprite, Animation
class Particle:
def __init__(self, position=(0, 0), velocity=(0, 0), duration=1):
self.position = Pose(position)
self.velocity = Pose(velocity)
self.destroyed = False
self.duration = duration
self.age = 0
self.layer = c.BACKGROUND
def update(self, dt, events):
if self.destroyed:
return
self.position += self.velocity * dt
if self.age > self.duration:
self.destroy()
self.age += dt
def draw(self, surf, offset=(0, 0)):
if self.destroyed:
return
def through(self):
return min(0.999, self.age/self.duration)
def destroy(self):
self.destroyed = True
class SparkParticle(Particle):
img = None
frames = []
def __init__(self, position, velocity=None, duration=0.1):
if velocity is None:
velocity = Pose((1, 0))
self.angle = velocity.get_angle_of_position()*180/math.pi
self.angle_pos = velocity
self.angle_pos.scale_to(1)
super().__init__(position, velocity=(0, 0),duration=duration)
if SparkParticle.img==None:
SparkParticle.img=ImageManager.load("assets/images/flash.png")
SparkParticle.img = pygame.transform.scale(SparkParticle.img, (SparkParticle.img.get_width()*2, SparkParticle.img.get_height()*2))
SparkParticle.frames = [self.get_frame(i) for i in range(5)]
self.age = 0
self.layer = c.FOREGROUND
def update(self, dt, events):
super().update(dt, events)
def get_frame(self,ct=0):
frame = ct
slice = pygame.Surface((42, 28))
slice.fill((255, 0, 0))
slice.set_colorkey((255, 0, 0))
slice = slice.convert()
slice.blit(SparkParticle.img, (0, -frame*28))
return slice
def get_frame_cached(self):
scale = 5/self.duration
frame = min(int(self.age*scale), 4)
return SparkParticle.frames[frame]
def draw(self, surface, offset=(0, 0)):
if self.destroyed:
return
surf = self.get_frame_cached()
surf = pygame.transform.rotate(surf, self.angle)
pos = self.position + Pose(offset) - Pose((surf.get_width()//2, surf.get_height()//2)) + self.angle_pos*15
surface.blit(surf, pos.get_position())
class Poof(Particle):
def __init__(self, position=(0, 0), duration = 0.4):
velocity_angle = random.random()*360
velocity_magnitude = random.random()*200 + 300
velocity = Pose((velocity_magnitude, 0))
velocity.rotate_position(velocity_angle)
super().__init__(position=position, velocity=velocity.get_position(), duration=duration)
self.poof = ImageManager.load("assets/images/poof.png")
self.angle = random.random()*360
self.spin = random.random()*60 - 30
def update(self, dt, events):
super().update(dt, events)
self.velocity.x *= 0.001**dt
self.velocity.y *= 0.001**dt
self.angle += self.spin*dt
def draw(self, surface, offset=(0, 0)):
if self.destroyed:
return
x = self.position.x + offset[0]
y = self.position.y + offset[1]
if x < -100 or x > c.WINDOW_WIDTH + 100:
return
if y < -100 or y > c.WINDOW_HEIGHT + 100:
return
scale = 2 - 2*self.through()
surf = pygame.transform.scale(self.poof, (self.poof.get_width()*scale, self.poof.get_height()*scale))
surf = pygame.transform.rotate(surf, self.angle)
x -= surf.get_width()//2
y -= surf.get_height()//2
a = 128*(1 - self.through()**1.5)
a = 256
surf.set_alpha(a)
surface.blit(surf, (x, y))