-
Notifications
You must be signed in to change notification settings - Fork 1
/
moveableobject.py
48 lines (43 loc) · 1.66 KB
/
moveableobject.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
from constants import EXPLOSION_FRAME_UPDATE_WAIT
from loadstaticres import explosion
from gameobject import GameObject
from timer import Timer
# all sprites inherit from this class
class MoveableObject(GameObject):
def __init__(self, x, y, speed, img):
GameObject.__init__(self, x, y, img)
self.orig_image = self.image
self.speed = speed
self.exploding = False
self.explosion_index = 0
self.explosion_timer = Timer()
self.explosion_timer.subscribe("timeout", self.update_explosion)
def update(self):
super().update()
if self.exploding:
self.explosion_timer.tick()
def start_exploding(self):
"""
Start the explosion sequence
queues up update_explosion calls on explosion_timer timeout event
NB: If this is called again while the explosion is occurring, the sequence will reset
"""
self.exploding = True
self.image = explosion[self.explosion_index]
self.speed = 0
self.explosion_timer.startwatch(EXPLOSION_FRAME_UPDATE_WAIT)
def update_explosion(self, event):
"""
Event handler to update the explosion, called from the explosion timer
:param event: the timer notify event
:return: True if explosion is complete, False if not
"""
if self.explosion_index < len(explosion)-1:
self.explosion_index += 1
self.image = explosion[self.explosion_index]
self.explosion_timer.startwatch(EXPLOSION_FRAME_UPDATE_WAIT)
return False
else:
self.explosion_index = 0
self.exploding = False
return True