-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
423 lines (376 loc) · 15.4 KB
/
main.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import pygame as pg
import sys
from random import choice, random
from os import path
import settings as st
import sprites as sp
import tilemap as tm
class Game:
def __init__(self):
pg.mixer.pre_init(44100, -16, 4, 2048)
pg.init()
self.screen = pg.display.set_mode((st.WIDTH, st.HEIGHT))
pg.display.set_caption(st.TITLE)
pg.mouse.set_visible(0)
self.clock = pg.time.Clock()
self.game_folder = path.dirname(__file__)
self.img_folder = path.join(self.game_folder, 'img')
self.snd_folder = path.join(self.game_folder, 'snd')
self.music_folder = path.join(self.game_folder, 'music')
self.map_folder = path.join(self.game_folder, 'maps')
self.title_font = path.join(self.img_folder, 'ZOMBIE.TTF')
self.hud_font = path.join(self.img_folder, 'Impacted2.0.ttf')
self.player_rot = 0
self.previous_room = None
self.current_room = 'map1.tmx'
self.next_room = ''
self.room_switch = False
self.initial = True
self.fog = pg.Surface((st.WIDTH, st.HEIGHT))
self.fog.fill(st.NIGHT_COLOR)
self.dim_screen = pg.Surface(self.screen.get_size()).convert_alpha()
self.dim_screen.fill((0, 0, 0, 180))
self.left = False
self.right = False
self.up = False
self.down = False
self.shoot = False
self.action = False
def draw_text(self, text, font_name, size, color, x, y):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=(x, y))
self.screen.blit(text_surface, text_rect)
def set_mask(self, rot=None):
#self.fog = pg.Surface((st.WIDTH, st.HEIGHT))
#self.fog.fill(st.NIGHT_COLOR)
self.light_mask = self.get_image(st.LIGHT_MASK)
if rot:
self.light_mask = pg.transform.rotate(self.light_mask, rot)
self.light_rect = self.light_mask.get_rect()
def get_imgel(self, image, x, y, width, height):
imagedest = pg.Surface((width, height))
imagedest.blit(image, (0, 0), (x, y, width, height))
return imagedest
def get_image(self, name):
""" -> pg.Surface """
image = pg.image.load(path.join(self.img_folder, name))
image = image.convert_alpha()
return image
def get_sound(self, name, vol:float=None):
""" -> pg.Sound """
sound = pg.mixer.Sound(path.join(self.snd_folder, name))
if vol is not None:
sound.set_volume(vol)
return sound
def load_image(self):
self.bullet_img = {}
self.item_img = {}
self.gun_flashes = []
self.player_img = []
self.mob_img = []
self.doors_img = self.get_image(st.DOORS_IMG)
self.bullet_img['lg'] = self.get_image(st.BULLET_IMG)
self.splat_img = self.get_image(st.SPLAT_IMG)
self.splat_img = pg.transform.scale(self.splat_img, (64, 64))
for img in st.PLAYER_IMG:
self.player_img.append(self.get_image(img))
for img in st.MOB_IMG:
self.mob_img.append(self.get_image(img))
for img in st.MUZZLE_FLASHES:
self.gun_flashes.append(self.get_image(img))
for item in st.ITEM_IMG:
self.item_img[item] = self.get_image(st.ITEM_IMG[item])
def load_sound(self):
self.zombie_moan_sounds = []
self.player_hit_sounds = []
self.zombie_hit_sounds = []
self.effects_sounds = {}
self.weapon_sounds = {}
for mytype in st.EFFECTS_SOUNDS:
self.effects_sounds[mytype] = self.get_sound(st.EFFECTS_SOUNDS[mytype], 0.1)
for weapon in st.WEAPON_SOUNDS:
self.weapon_sounds[weapon] = self.get_sound(st.WEAPON_SOUNDS[weapon], 0.1)
for snd in st.ZOMBIE_MOAN_SOUNDS:
self.zombie_moan_sounds.append(self.get_sound(snd, 0.2))
for snd in st.PLAYER_HIT_SOUNDS:
self.player_hit_sounds.append(self.get_sound(snd))
for snd in st.ZOMBIE_HIT_SOUNDS:
self.zombie_hit_sounds.append(self.get_sound(snd, 0.1))
def new(self, map_name = None):
# initialize all variables and do all the setup for a new game
self.all_sprites = pg.sprite.LayeredUpdates()
self.walls = pg.sprite.Group()
self.mobs = pg.sprite.Group()
self.bullets = pg.sprite.Group()
self.items = pg.sprite.Group()
self.load_image()
self.load_sound()
self.player = sp.Player(self, 256, 128)
# st.PLAYER_HEALTH = 100
self.player.rot = self.player_rot # remember facing direction when entering new rooms
self.load_map(map_name)
self.camera = tm.Camera(self.map.width, self.map.height)
self.set_mask() # torchlight
self.draw_debug = False
self.paused = False
self.night = False
def load_map(self, map_name):
if map_name is None:
# initial map loading
self.map = tm.TiledMap(path.join(self.map_folder, 'map1.tmx'))
self.current_room = 'map1.tmx'
else:
# dynamic map loading
self.map = tm.TiledMap(path.join(self.map_folder, map_name))
self.current_room = map_name
self.map_img = self.map.make_map()
self.map.rect = self.map_img.get_rect()
for tmx_obj in self.map.tmxdata.objects:
center_x = (tmx_obj.x + tmx_obj.width / 2)
center_y = (tmx_obj.y + tmx_obj.height / 2)
obj_center = sp.vec(center_x, center_y)
if tmx_obj.name == 'zombie':
sp.Mob(self, obj_center.x, obj_center.y)
if tmx_obj.name == 'wall':
sp.Obstacle(self, tmx_obj.x, tmx_obj.y, tmx_obj.width, tmx_obj.height)
if tmx_obj.name in ['health', 'shotgun', 'ammo']:
#if tmx_obj.name == 'key' and not st.GOT_KEY:
sp.Item(self, obj_center, tmx_obj.name)
if tmx_obj.name == 'key':
if not st.GOT_KEY:
sp.Item(self, obj_center, tmx_obj.name)
if tmx_obj.name == 'door':
newpos = self.pos_in_newmap(tmx_obj)
sp.Door(self, obj_center, tmx_obj.dir, tmx_obj.link)
if tmx_obj.link+'.tmx' == self.previous_room:
self.player.pos = sp.vec(newpos[0])
self.room_switch = False
def pos_in_newmap(self, door):
""" -> list """
newpos = ['','']
tempx = 0
tempy = 0
# door on left side
if door.x == 0:
tempx = (door.x + 128)
tempy = (door.y + 64)
newpos[1] = 'left'
# door on top side
elif door.x > 0 and door.y == 0:
tempx = (door.x + 64)
tempy = (door.y + 128)
newpos[1] = 'top'
# door on bottom side
elif door.x > 0 and door.y == self.map.height - 64:
tempx = (door.x + 64)
tempy = (door.y - 64)
newpos[1] = 'bottom'
# door on right side
elif door.x == self.map.width - 64 and door.y > 0:
tempx = (door.x - 64)
tempy = (door.y + 64)
newpos[1] = 'right'
newpos[0] = (tempx, tempy)
return newpos
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
# pg.mixer.music.play(loops=-1)
while self.playing:
self.dt = self.clock.tick(st.FPS) / 1000.0 # fix for Python 2.x
self.events()
if not self.paused:
if not self.room_switch:
self.update()
self.draw()
def quit(self):
self.playing = False
pg.quit()
sys.exit()
def hit_item(self):
# player hits items
hits = pg.sprite.spritecollide(self.player, self.items, False)
for hit in hits:
if hit.type == 'health' and st.PLAYER_HEALTH < 100:
hit.kill()
self.effects_sounds['health_up'].play()
self.player.add_health(st.HEALTH_PACK_AMOUNT)
if hit.type == 'door':
if self.player.action:
hit.change_state()
self.effects_sounds['door'].play()
self.action = False
self.player.action = False
if hit.type == 'ammo':
hit.kill()
self.effects_sounds['gun_pickup'].play()
st.AMMO += 12
if hit.type == 'key':
hit.kill()
self.effects_sounds['health_up'].play()
st.GOT_KEY=True
def go_next(self, newroom):
new_map = newroom+'.tmx'
if path.exists(path.join(self.map_folder, new_map)):
self.room_switch = True
self.previous_room = self.current_room
self.next_room = new_map
self.player_rot = self.player.rot
self.new(new_map)
else:
print(f"room ---{new_map} doesn't exist yet")
def hit_mob(self):
if self.player:
# mobs hit player
hits = pg.sprite.spritecollide(self.player, self.mobs, False, tm.collide_hit_rect)
for hit in hits:
if random() < 0.7:
choice(self.player_hit_sounds).play()
# self.player.health -= st.MOB_DAMAGE
st.PLAYER_HEALTH -= st.MOB_DAMAGE
hit.vel = sp.vec(0, 0)
if st.PLAYER_HEALTH <= 0:
self.playing = False
if hits:
self.player.hit()
self.player.pos += sp.vec(st.MOB_KNOCKBACK, 0).rotate(-hits[0].rot)
def hit_bullet(self):
# bullets hit mobs
hits = pg.sprite.groupcollide(self.mobs, self.bullets, False, True)
for mob in hits:
# hit.health -= WEAPONS[self.player.weapon]['damage']
# * len(hits[hit])
for bullet in hits[mob]:
mob.health -= bullet.damage
# mob.vel += sp.vec(bullet.dir[0]*100, bullet.dir[1]*100)
# mob gets stopped
mob.vel = sp.vec(0, 0)
# mob get knockback from bullet
mob.pos += sp.vec(bullet.dir[0]*10, bullet.dir[1]*10)
def update(self):
# update portion of the game loop
self.all_sprites.update()
self.camera.update(self.player)
self.hit_item()
self.hit_mob()
self.hit_bullet()
def draw_player_health(self, surf, x, y, pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 20
fill = pct * BAR_LENGTH
outline_rect = pg.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pg.Rect(x, y, fill, BAR_HEIGHT)
if pct > 0.6:
col = st.GREEN
elif pct > 0.3:
col = st.YELLOW
else:
col = st.RED
pg.draw.rect(surf, col, fill_rect)
pg.draw.rect(surf, st.WHITE, outline_rect, 2)
if st.GOT_KEY:
keypic = self.item_img['key']
keypic = pg.transform.scale(keypic, (32, 32))
key_rect = keypic.get_rect(center=(145, 23))
pg.Surface.blit(self.screen, keypic, key_rect)
def render_fog(self):
if self.night:
pg.Surface.fill(self.fog, st.NIGHT_COLOR)
self.light_rect.center = self.camera.apply(self.player).center
pg.Surface.blit(self.fog, self.light_mask, self.light_rect )
pg.Surface.blit(self.screen, self.fog, (0, 0), special_flags=pg.BLEND_MULT )
def draw(self):
pg.Surface.blit(self.screen, self.map_img, self.camera.apply(self.map))
self.draw_player_health(self.screen, 10, 10, st.PLAYER_HEALTH / 100)
self.draw_text('Zombies: {}'.format(len(self.mobs)), self.hud_font, 30, st.WHITE, st.WIDTH - 100, 10)
self.draw_text('Ammo: {}'.format(st.AMMO), self.hud_font, 30, st.WHITE, st.WIDTH - 300, 10)
if self.draw_debug:
self.draw_text("{:.2f} fps".format(self.clock.get_fps()), self.hud_font, 30, st.WHITE, 180, 16)
for wall in self.walls:
pg.draw.rect(self.screen, st.CYAN, self.camera.apply_rect(wall.rect), 1)
for sprite in self.all_sprites:
pg.Surface.blit(self.screen, sprite.image, self.camera.apply(sprite))
pg.draw.rect(self.screen, st.CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
pg.draw.rect(self.screen, st.BROWN, self.camera.apply_rect(sprite.rect), 2)
if isinstance(sprite, sp.Mob):
my_x = self.camera.apply(sprite).centerx
my_y = self.camera.apply(sprite).centery
pg.draw.circle(self.screen, st.RED, (my_x, my_y), st.DETECT_RADIUS, 2)
else:
for sprite in self.all_sprites:
pg.Surface.blit(self.screen, sprite.image, self.camera.apply(sprite))
self.render_fog()
pg.display.flip()
def events(self):
# catch all events here
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
if event.type == pg.KEYUP:
if event.key == pg.K_LEFT:
self.left = False
if event.key == pg.K_RIGHT:
self.right = False
if event.key == pg.K_UP:
self.up = False
if event.key == pg.K_DOWN:
self.down = False
if event.key == pg.K_w:
self.action=False
if event.key == pg.K_x:
self.shoot=False
if event.type == pg.KEYDOWN:
if event.key == pg.K_LEFT:
self.left = True
if event.key == pg.K_RIGHT:
self.right = True
if event.key == pg.K_UP:
self.up = True
if event.key == pg.K_DOWN:
self.down = True
if event.key == pg.K_w:
self.action=True
if event.key == pg.K_x:
self.shoot=True
if event.key == pg.K_h:
self.draw_debug = not self.draw_debug
if event.key == pg.K_p:
self.paused = not self.paused
if event.key == pg.K_n:
self.night = not self.night
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_f:
if pg.display.get_driver()=='x11':
pg.display.toggle_fullscreen()
if event.key == pg.K_d:
# debug output:
print("player action = ",self.player.action)
print("health=",st.PLAYER_HEALTH)
def show_start_screen(self):
pass
def show_go_screen(self):
self.screen.fill(st.BLACK)
pg.display.flip()
self.wait_for_key()
def wait_for_key(self):
pg.event.wait()
waiting = True
while waiting:
self.clock.tick(st.FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
waiting = False
self.quit()
if event.type == pg.KEYUP:
waiting = False
# create the game object
g = Game()
g.show_start_screen()
while True:
g.new()
g.run()
g.show_go_screen()