-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation_manager.py
71 lines (62 loc) · 2.36 KB
/
animation_manager.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
import random
import pygame
from particle import ParticleSystem, ConfettiParticle, SmokeParticle
from config import GAME_STATE_GAMEOVER, GAME_STATE_WIN
class AnimationManager:
def __init__(self) -> None:
self.ps = ParticleSystem()
self.cell_locations = []
self.animation_ended = False
self.started = False
self.confetti_delay = 100
self.smoke_duration = 3000
self.current_cell = 0
self.timer = None
def set_cell_locations(self, cell_locations) -> None:
self.cell_locations = cell_locations
def animation_has_ended(self) -> bool:
return self.animation_ended
def start(self) -> None:
self.timer = pygame.time.get_ticks()
self.animation_ended = False
self.started = True
def stop(self) -> None:
self.animation_ended = False
self.started = False
def render(self, game_state) -> None:
if game_state == GAME_STATE_WIN:
self.render_confetti()
elif game_state == GAME_STATE_GAMEOVER:
self.render_smoke()
self.ps.update()
self.ps.draw(pygame.display.get_surface())
def render_confetti(self) -> None:
ticks = pygame.time.get_ticks()
if self.current_cell < len(self.cell_locations):
if ticks - self.timer > self.confetti_delay:
self.timer = ticks
rect = self.cell_locations[self.current_cell].get_rect()
px = rect.centerx
py = rect.centery
i = 0
mparts = 30
while i < mparts:
self.ps.add_particle(ConfettiParticle(px, py, random.randrange(1, 4)))
i += 1
self.current_cell += 1
elif len(self.ps.particles) == 0:
self.animation_ended = True
self.current_cell = 0
def render_smoke(self) -> None:
ticks = pygame.time.get_ticks()
if ticks - self.timer < self.smoke_duration:
for cell in self.cell_locations:
if cell.exploded:
rect = cell.get_rect()
px = rect.centerx
py = rect.centery
self.ps.add_particle(SmokeParticle(px, py))
break
else:
self.ps.particles.clear()
self.animation_ended = True