-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_classes.py
316 lines (214 loc) · 9.04 KB
/
object_classes.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import pygame
from tileC import Tile
from random import randint
class Character(pygame.Rect):
width, height = 32, 32
def __init__(self, x, y):
self.tx, self.ty = None, None
pygame.Rect.__init__(self, x, y, Character.width, Character.height)
def __str__(self):
return str(self.get_number())
def set_target(self, next_tile): # Sets the target tile the character is moving to
if self.tx == None and self.ty == None:
self.tx = next_tile.x
self.ty = next_tile.y
def get_number(self):
return ((self.x / self.width) + Tile.H) + ((self.y / self.height) * Tile.V)
def get_tile(self):
return Tile.get_tile(self.get_number())
def rotate(self, direction, img):
if direction == 'n':
if self.direction != 'n': # only do this if the charater is heading this direction
self.direction = 'n'
self.img = img
if direction == 's':
if self.direction != 's':
self.direction = 's'
self.img = img
if direction == 'e':
if self.direction != 'e':
self.direction = 'e'
self.img = img
if direction == 'w':
if self.direction != 'w':
self.direction = 'w'
self.img = img
class Zombie(Character):
List = []
spawn_tiles = (316, 148)
zombie_s = pygame.image.load('images/zombie_front.png')
zombie_n = pygame.image.load('images/zombie_back.png')
zombie_e = pygame.image.load('images/zombie_right.png')
zombie_w = pygame.image.load('images/zombie_left.png')
health = 100
def __init__(self, x, y):
self.direction = 'w'
self.img = Zombie.zombie_w
self.health = Zombie.health
Character.__init__(self, x, y)
Zombie.List.append(self) # add every zombie that is instanciated
@staticmethod
def update(screen):
for zombie in Zombie.List:
screen.blit(zombie.img, (zombie.x, zombie.y))
if zombie.health <=0:
Zombie.List.remove(zombie)
if zombie.tx != None and zombie.ty != None: # Target is set
X = zombie.x - zombie.tx
Y = zombie.y - zombie.ty
vel = 2
if X < 0: # ---> Right (East)
zombie.x += vel
zombie.rotate('e', Zombie.zombie_e)
elif X > 0: # <---- Left (West)
zombie.x -= vel
zombie.rotate('w', Zombie.zombie_w)
if Y > 0: # up
zombie.y -= vel
zombie.rotate('n', Zombie.zombie_n)
elif Y < 0: # down
zombie.y += vel
zombie.rotate('s', Zombie.zombie_s)
if X == 0 and Y == 0:
zombie.tx, zombie.ty = None, None
@staticmethod
def spawn(total_frames, FPS):
if total_frames % (FPS) == 0:
if total_frames % (FPS * 48) == 0: # higher the multiplying value to slow the zombie spawn rate
r = randint(0, 2)
sounds = [pygame.mixer.Sound('audio/zs1.ogg'),
pygame.mixer.Sound('audio/zs2.ogg'),
pygame.mixer.Sound('audio/zs2.ogg')
]
sounds = sounds[r]
sounds.play()
r = randint(0, len(Zombie.spawn_tiles) - 1) # pick a randon spawn tile
tile_num = Zombie.spawn_tiles[r]
spawn_node = Tile.get_tile(tile_num)
Zombie(spawn_node.x, spawn_node.y)
class Survivor(Character):
weapons_img = [pygame.image.load('images/automatic.png'),
pygame.image.load('images/shotgun.png')]
def __init__(self, x, y):
self.current_weapon = 0 # 0 = automatic , 1 = shotgun
self.direction = 'w'
self.img = pygame.image.load('images/survivor_w.png')
Character.__init__(self, x, y)
def get_bullet_type(self):
if self.current_weapon == 0:
return 'automatic'
elif self.current_weapon == 1:
return 'shotgun'
def movement(self):
if self.tx != None and self.ty != None: # Target is set if it isn't moving don't do anything
X = self.x - self.tx
Y = self.y - self.ty
vel = 8
if X < 0: # --->
self.x += vel
elif X > 0: # <----
self.x -= vel
if Y > 0: # up
self.y -= vel
elif Y < 0: # down
self.y += vel
if X == 0 and Y == 0:
self.tx, self.ty = None, None
def draw(self, screen):
screen.blit(self.img, (self.x, self.y))
half = self.width/2
weapon_img = Survivor.weapons_img[self.current_weapon]
if self.direction == 'w':
screen.blit(weapon_img, (self.x, self.y + half))
elif self.direction == 'e':
weapon_img = pygame.transform.flip(weapon_img, True, False)
screen.blit(weapon_img, (self.x + half, self.y + half))
elif self.direction == 's':
weapon_img = pygame.transform.rotate(weapon_img, 90) # CCW
screen.blit(weapon_img, (self.x + half, self.y + half))
elif self.direction == 'n':
south = pygame.transform.rotate(weapon_img, 90)
weapon_img = pygame.transform.flip(south, False, True)
screen.blit(weapon_img, (self.x + half, self.y - half))
def rotate(self, direction):
path = 'images/survivor_'
png = '.png'
if direction == 'n':
if self.direction != 'n':
self.direction = 'n'
self.img = pygame.image.load(path + self.direction + png)
if direction == 's':
if self.direction != 's':
self.direction = 's'
self.img = pygame.image.load(path + self.direction + png)
if direction == 'e':
if self.direction != 'e':
self.direction = 'e'
self.img = pygame.image.load(path + self.direction + png)
if direction == 'w':
if self.direction != 'w':
self.direction = 'w'
self.img = pygame.image.load(path + self.direction + png)
class Bullet (pygame.Rect):
width, height = 7, 10
List = []
bullet_img = {'shotgun': pygame.image.load('images/shotgun_b.png'),
'automatic': pygame.image.load('images/automatic_b.png')
}
weapons_damage = {'shotgun': (Zombie.health/2) + 1,
'automatic': (Zombie.health/6) + 1
}
def __init__(self, x, y, velx, vely, direction, type_):
if type_ == 'shotgun':
try:
dx = abs(Bullet.List[-1].x - x)
dy = abs(Bullet.List[-1].y - y)
if dx < 50 and dy < 50 and type_ == 'shotgun':
return
except:
pass
self.type = type_
self.direction = direction
self.velx = velx
self.vely = vely
if direction == 'n':
south = pygame.transform.rotate(Bullet.bullet_img[type_], 90) # CCW
self.img = pygame.transform.flip(south, False, True)
if direction == 's':
self.img = pygame.transform.rotate(Bullet.bullet_img[type_], 90) # CCW
if direction == 'e':
self.img = pygame.transform.flip(Bullet.bullet_img[type_], True, False)
if direction == 'w':
self.img = Bullet.bullet_img[type_]
pygame.Rect.__init__(self, x, y, Bullet.width, Bullet.height)
Bullet.List.append(self)
def off_screen (self, screen):
if self.x < 0:
return True
elif self.y < 0:
return True
elif self.x + self.width > screen.get_width():
return True
elif self.y + self.height > screen.get_height():
return True
return False
@staticmethod
def super_massive_jumbo_loop(screen):
for bullet in Bullet.List:
bullet.x += bullet.velx
bullet.y += bullet.vely
screen.blit(bullet.img, (bullet.x, bullet.y))
if bullet.off_screen(screen):
Bullet.List.remove(bullet)
continue
for zombie in Zombie.List:
if bullet.colliderect(zombie):
zombie.health -= Bullet.weapons_damage[bullet.type]
Bullet.List.remove(bullet)
break
for tile in Tile.List:
if bullet.colliderect(tile) and not(tile.walkable):
try:
Bullet.List.remove(bullet)
except:
pass # bullet not in list