forked from seanmcortes/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.py
More file actions
246 lines (213 loc) · 8.59 KB
/
enemy.py
File metadata and controls
246 lines (213 loc) · 8.59 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import pygame
from sprites import GameObject, SpriteSheet
from settings import RED, GREEN, TILESIZE, ENEMY_SPEED, \
HIT_DELAY, ENEMY_ATTACK_DELAY, UP, DOWN, LEFT, RIGHT, \
BAT_SPRITE_SHEET, GHOST_SPRITE_SHEET
from helper import Animate
class Enemy(GameObject):
def __init__(self, scene, x, y, orientation, moves):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.enemies
pygame.sprite.Sprite.__init__(self, self.groups)
self.orientation = orientation
self.moves = moves
self.interactable = False
self.collidable = False
self.move_counter = 0
self.move_counter_increment = 1
self.reverse = False
self.health = 1
self.hit = False
self.hit_detected = False
self.attacking = False
self.attack_detected = False
if len(moves) > 0:
self.direction = None
# Animation
self.animation_index = 0
self.update_delay = ENEMY_SPEED
self.attack_delay = ENEMY_ATTACK_DELAY
self.last_update = pygame.time.get_ticks()
self.last_idle_update = pygame.time.get_ticks()
self.hit_delay = HIT_DELAY
sprite_sheet = SpriteSheet(BAT_SPRITE_SHEET)
self.image = sprite_sheet.get_image(0, 0, 32, 32)
self.walking_up = []
self.walking_down = []
self.walking_left = []
self.walking_right = []
# Append sprites to walking arrays
for x in range(0, 33, 32):
self.walking_right.append(sprite_sheet.get_image(x, 0, 32, 32))
self.walking_left.append(sprite_sheet.get_image(x, 32, 32, 32))
self.walking_down.append(sprite_sheet.get_image(x, 64, 32, 32))
self.walking_up.append(sprite_sheet.get_image(x, 96, 32, 32))
self.damage_up = []
self.damage_down = []
self.damage_left = []
self.damage_right = []
# Append sprites to damage arrays
for x in range(64, 128, 32):
self.damage_right.append(sprite_sheet.get_image(x, 0, 32, 32))
self.damage_left.append(sprite_sheet.get_image(x, 32, 32, 32))
self.damage_down.append(sprite_sheet.get_image(x, 64, 32, 32))
self.damage_up.append(sprite_sheet.get_image(x, 96, 32, 32))
"""
Move object a number of tiles
Args:
dx (int): number of tiles moved in x-coordinate
dy (int): number of tiles moved in y-coordinate
Returns:
bool: True if object can move, false if there is collision.
"""
def move(self, dx=0, dy=0):
if not self.collision_object(dx, dy):
self.x += dx
self.y += dy
self.orientation = (dx, dy)
return True
else:
return False
"""
Handle enemy patrol route. Reverses route if collision occurs.
"""
def move_algorithm(self):
if self.move_counter >= len(self.moves) or self.move_counter < 0: # enemy finishes movement
self.reverse = not self.reverse
self.move_counter_increment *= -1
self.orientation = self.opposite_direction(self.orientation)
else: # enemy has not finished movement, check for collisions then move
self.direction = self.moves[self.move_counter]
if self.reverse: # reverse movement
self.direction = self.opposite_direction(self.moves[self.move_counter])
if not self.move(self.direction[0], self.direction[1]): # collision, reverse movement
self.reverse = not self.reverse
self.move_counter_increment *= -1
self.move_counter += self.move_counter_increment
"""
Reverses direction (e.g. LEFT -> RIGHT, UP -> DOWN, vice-versa)
Args:
direction (tuple): (x, y) as represented by directions in settings.py
Returns:
tuple: (x, y) as represented by direction in settings.py
"""
def opposite_direction(self, direction):
return tuple(x * -1 for x in direction)
"""
Check health, kill if 0
Returns:
True: object has health remaining
False: object has no health and is dead
"""
def check_health(self):
if self.health <= 0:
self.kill()
return False
else:
return True
"""
Modify object state after being attacked by player
"""
def take_damage(self):
self.health -= 1
if self.check_health():
self.hit = True
"""
Display image based on orientation
"""
def render_orientation(self):
if self.orientation == UP:
self.image = self.walking_up[0]
elif self.orientation == DOWN:
self.image = self.walking_down[0]
elif self.orientation == LEFT:
self.image = self.walking_left[0]
else:
self.image = self.walking_right[0]
"""
Draw image. Handle movement
"""
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
now = pygame.time.get_ticks()
# Enemy is attacked by player
if self.hit and not self.hit_detected:
self.last_update = now
self.hit_detected = True
# Animate hurt
elif self.hit and self.hit_detected:
if now - self.last_update < self.hit_delay:
if self.orientation == UP:
Animate(self, self.damage_up)
elif self.orientation == LEFT:
Animate(self, self.damage_left)
elif self.orientation == DOWN:
Animate(self, self.damage_down)
else:
Animate(self, self.damage_right)
else:
self.last_update = now
self.hit = False
self.hit_detected = False
# Pause while player recovers from being attacked
elif self.attacking and not self.attack_detected:
self.last_update = now
self.attack_detected = True
elif self.attacking and self.attack_detected:
if now - self.last_update >= self.attack_delay:
self.last_update = now
self.attacking = False
self.attack_detected = False
# Animate idle
else:
if now - self.last_idle_update >= self.update_delay:
self.last_idle_update = now
if self.orientation == UP:
Animate(self, self.walking_up)
elif self.orientation == LEFT:
Animate(self, self.walking_left)
elif self.orientation == DOWN:
Animate(self, self.walking_down)
else:
Animate(self, self.walking_right)
# Continue pathing
if now - self.last_update >= self.update_delay:
self.last_update = now
if len(self.moves) > 0:
self.move_algorithm()
'''
Ghost enemy:
Inherits from Enemy class, sets new sprites and health value.
'''
class Ghost(Enemy):
def __init__(self, scene, x, y, orientation, moves):
super().__init__(scene, x, y, orientation, moves)
self.groups = scene.all_sprites, scene.enemies
pygame.sprite.Sprite.__init__(self, self.groups)
self.health = 2
sprite_sheet = SpriteSheet(GHOST_SPRITE_SHEET)
self.image = sprite_sheet.get_image(0, 0, 32, 32)
self.walking_up = []
self.walking_down = []
self.walking_left = []
self.walking_right = []
# Append sprites to walking arrays
for x in range(0, 33, 32):
self.walking_right.append(sprite_sheet.get_image(x, 0, 32, 32))
self.walking_left.append(sprite_sheet.get_image(x, 32, 32, 32))
self.walking_down.append(sprite_sheet.get_image(x, 64, 32, 32))
self.walking_up.append(sprite_sheet.get_image(x, 96, 32, 32))
self.damage_up = []
self.damage_down = []
self.damage_left = []
self.damage_right = []
# Append sprites to damage arrays
for x in range(64, 128, 32):
self.damage_right.append(sprite_sheet.get_image(x, 0, 32, 32))
self.damage_left.append(sprite_sheet.get_image(x, 32, 32, 32))
self.damage_down.append(sprite_sheet.get_image(x, 64, 32, 32))
self.damage_up.append(sprite_sheet.get_image(x, 96, 32, 32))
# Sources:
# https://github.com/kidscancode/pygame_tutorials/blob/master/tilemap/part%2002/sprites.py
# https://stackoverflow.com/questions/10762823/how-can-i-pause-one-pygame-function-without-pausing-others