-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactorEngine.py
72 lines (61 loc) · 2.28 KB
/
actorEngine.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
# class that allows sprites to have health events or have special attributes.
import pygame, spriteEngine, math, sys
# objects are things in the world that can be changed and edited.
class Object(spriteEngine.SuperSprite):
def __init__(self, screen, background, image, **kwargs):
#INIT
super().__init__(screen, background, **kwargs)
self.setImage(image)
self.imageFile = image
def updateOverride(self):
# object update
self.objectUpdate()
def objectUpdate(self):
# update method for object
pass
# actor class. this is a class that defines health and has events for when health is depleted.
class Actor(Object):
def __init__(self, screen, background, image, health, **kwargs):
super().__init__(screen, background, image, **kwargs)
# health
self.health = health
self.maxHealth = health
self.isDead = False
self.timeSinceLastDamaged = pygame.time.get_ticks()
self.canBeDamaged = True
def objectUpdate(self):
#wait 0.2 seconds before we can be damaged again
if self.canBeDamaged == False:
seconds=(pygame.time.get_ticks()-self.timeSinceLastDamaged)/1000
if seconds > 0.2:
self.timeSinceLastDamaged = pygame.time.get_ticks()
self.canBeDamaged = True
#check our health. If we are dead, call the death event.
if self.health > 0:
# make sure we don't go above max health.
if self.health > self.maxHealth:
self.health = self.maxHealth
#update our actor
self.actorUpdate()
elif self.health <= 0:
# declare the actor dead
self.health = 0
self.isDead = True
self.actorOnDeath()
def damage(self, amt):
# damages the actor. Note that negative values can heal the actor.
if self.canBeDamaged == True:
self.health -= amt
self.canBeDamaged = False
def actorUpdate(self):
# update method for actor
pass
def actorOnDeath(self):
# actor death event
pass
# adds the actor/object to a group
def addObject(actor, pos, group):
(x,y) = pos
actor.x = x
actor.y = y
group.add(actor)