-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_classes.py
More file actions
321 lines (229 loc) · 9.4 KB
/
object_classes.py
File metadata and controls
321 lines (229 loc) · 9.4 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import pygame
from tileC import Tile
from random import randint
__author__ = 'Bhavik & Ram'
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):
if self.tx == None and self.ty == None:
self.tx = next_tile.x
self.ty = next_tile.y
#this fucntion will return the number where our character is
def get_number(self):
return ((self.x / self.width) + Tile.H) + ((self.y / self.height) * Tile.V)
#this function will return tile where our character is
def get_tile(self):
return Tile.get_tile(self.get_number())
def rotate(self, direction, original_img):
if direction == 'n':
if self.direction != 'n':
self.direction = 'n'
south = pygame.transform.rotate(original_img, 90) # CCW
self.img = pygame.transform.flip(south, False, True)
if direction == 's':
if self.direction != 's':
self.direction = 's'
self.img = pygame.transform.rotate(original_img, 90) # CCW
if direction == 'e':
if self.direction != 'e':
self.direction = 'e'
self.img = pygame.transform.flip(original_img, True, False)
if direction == 'w':
if self.direction != 'w':
self.direction = 'w'
self.img = original_img
class Zombie(Character):
List = [] #creating zombies
spawn_tiles = (9,42,91,134,193,219,274) #zombies will be added from here
original_img = pygame.image.load('images/zombie.png')
health = 100
def __init__(self, x, y):
self.direction = 'w'
self.health = Zombie.health
self.img = Zombie.original_img
Character.__init__(self, x, y) #zombie created
Zombie.List.append(self) #zombie added
@staticmethod
def draw_zombies(screen):
for zombie in Zombie.List:
screen.blit(zombie.img, (zombie.x, zombie.y))
if zombie.health <= 0:
Zombie.List.remove(zombie)
@staticmethod
def movement():
for zombie in Zombie.List:
if zombie.tx != None and zombie.ty != None: # Target is set
X = zombie.x - zombie.tx
Y = zombie.y - zombie.ty
vel = 4
if X < 0: # --->
zombie.x += vel
zombie.rotate('e', Zombie.original_img)
elif X > 0: # <----
zombie.x -= vel
zombie.rotate('w', Zombie.original_img)
if Y > 0: # up
zombie.y -= vel
zombie.rotate('n', Zombie.original_img)
elif Y < 0: # dopwn
zombie.y += vel
zombie.rotate('s', Zombie.original_img)
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 * 6) == 0:
r = randint(0, 2)
sounds = [pygame.mixer.Sound('audio/zs1.ogg'),
pygame.mixer.Sound('audio/zs2.ogg'),
pygame.mixer.Sound('audio/zs3.ogg')]
sound = sounds[ r ]
sound.play()
r = randint(0, len(Zombie.spawn_tiles) - 1)
tile_num = Zombie.spawn_tiles[r]
spawn_node = Tile.get_tile(tile_num)
Zombie(spawn_node.x, spawn_node.y)
#creating survivor-player class
class Survivor(Character):
guns_img =[pygame.image.load('images/pistol.png'),
pygame.image.load('images/shotgun.png'),
pygame.image.load('images/automatic.png')]
def __init__(self, x, y):
self.current = 0 # 0 -> pistol, 1 -> shotgun, 2 -> automatic
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 == 0:
return 'pistol'
elif self.current == 1:
return 'shotgun'
elif self.current == 2:
return 'automatic'
def movement(self):
if self.tx != None and self.ty != None: # Target is set
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: # dopwn
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))
h = self.width / 2 # to make center our gun
img = Survivor.guns_img[self.current]
if self.direction == 'w':
screen.blit(img, (self.x, self.y + h))
elif self.direction == 'e':
img = pygame.transform.flip(img, True, False)
screen.blit(img, (self.x + h, self.y + h))
elif self.direction == 's':
img = pygame.transform.rotate(img, 90) # CCW
screen.blit(img, (self.x + h, self.y + h))
elif self.direction == 'n':
south = pygame.transform.rotate(img, 90)
img = pygame.transform.flip(south, False, True)
screen.blit(img, (self.x + h, self.y - h))
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 = []
imgs = { 'pistol' : pygame.image.load('images/pistol_b.png'),
'shotgun' : pygame.image.load('images/shotgun_b.png'),
'automatic' : pygame.image.load('images/automatic_b.png') }
gun_dmg = {'pistol' : (Zombie.health / 3) + 1,
'shotgun' : Zombie.health / 2,
'automatic' : (Zombie.health / 6) + 1 }
def __init__(self, x, y, velx, vely, direction, type_):
if type_ == 'shotgun' or type_ == 'pistol':
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
if dx < 30 and dy < 30 and type_ == 'pistol':
return
except: pass
self.type = type_
self.direction = direction
self.velx, self.vely = velx, vely
if direction == 'n':
south = pygame.transform.rotate(Bullet.imgs[type_], 90) # CCW
self.img = pygame.transform.flip(south, False, True)
if direction == 's':
self.img = pygame.transform.rotate(Bullet.imgs[type_], 90) # CCW
if direction == 'e':
self.img = pygame.transform.flip(Bullet.imgs[type_], True, False)
if direction == 'w':
self.img = Bullet.imgs[type_]
pygame.Rect.__init__(self, x, y, Bullet.width, Bullet.height)
Bullet.List.append(self)
# draw
# update
# collision --> zombies, tiles
def offscreen(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.offscreen(screen):
Bullet.List.remove(bullet)
continue
for zombie in Zombie.List:
if bullet.colliderect(zombie):
"""
The same bullet cannot be used to kill
multiple zombies and as the bullet was
no longer in Bullet.List error was raised
"""
zombie.health -= Bullet.gun_dmg[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:
break # if bullet cannot be removed, then GTFO