-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·3099 lines (2917 loc) · 158 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# The Legend of Sky Realm by Raven Ironwing
import tracemalloc
import gc
import pygame as pg
import sys
import pickle
import pygame.surfarray as surfarray
from random import choice, random, choices
from os import path, makedirs
from settings import *
from npcs import *
from quests import *
from menu import *
from sprites import *
from tilemap import *
import datetime
from time import sleep, perf_counter
import math
from pygame.locals import *
from pytmx.util_pygame import load_pygame
import pyscroll
import pyscroll.data
from pyscroll.group import PyscrollGroup
tracemalloc.start()
#npc_q = Queue()
def group_draw(self, surface, game = None): # This is a modded version of the pyscroll group draw function that only draws sprites that are on screen (otherwise it's super slow).
""" Draw all sprites and map onto the surface
:param surface: pygame surface to draw to
:type surface: pygame.surface.Surface
"""
ox, oy = self._map_layer.get_center_offset()
new_surfaces = list()
spritedict = self.spritedict
gl = self.get_layer_of_sprite
new_surfaces_append = new_surfaces.append
if game == None: #Used if there is not game running yet... kind of useless, but makes it not error out.
for spr in self.sprites():
new_rect = spr.rect.move(ox, oy)
try:
new_surfaces_append((spr.image, new_rect, gl(spr), spr.blendmode))
except AttributeError: # generally should only fail when no blendmode available
new_surfaces_append((spr.image, new_rect, gl(spr)))
spritedict[spr] = new_rect
else:
for spr in self.sprites(): # This modded version only adds the sprite images that are on screen using the game.camera and the on_screen method
if game.on_screen_no_edge(spr):
new_rect = spr.rect.move(ox, oy)
try:
new_surfaces_append((spr.image, new_rect, gl(spr), spr.blendmode))
except AttributeError: # generally should only fail when no blendmode available
new_surfaces_append((spr.image, new_rect, gl(spr)))
spritedict[spr] = new_rect
#hits = pg.sprite.spritecollide(game.camera, self.sprites(), False)
#for spr in hits: # This modded version only adds the sprite images that are on screen by using only the sprites that collide with the camera object.
# new_rect = spr.rect.move(ox, oy)
# try:
# new_surfaces_append((spr.image, new_rect, gl(spr), spr.blendmode))
# except AttributeError: # generally should only fail when no blendmode available
# new_surfaces_append((spr.image, new_rect, gl(spr)))
# spritedict[spr] = new_rect
self.lostsprites = []
return self._map_layer.draw(surface, surface.get_rect(), new_surfaces)
PyscrollGroup.draw = group_draw # Replaces the default PyscrollGroup.draw method
def get_tile_number(sprite, layer): # Gets the type of tile a sprite is on.
x = int(sprite.pos.x / sprite.game.map.tile_size)
y = int(sprite.pos.y / sprite.game.map.tile_size)
if x < 0: x = 0
if y < 0: y = 0
if x >= sprite.game.map.tiles_wide: x = sprite.game.map.tiles_wide - 1
if y >= sprite.game.map.tiles_high: y = sprite.game.map.tiles_high - 1
return sprite.game.map.tmxdata.get_tile_gid(x, y, layer)
def trace_mem():
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
print("[ Top 10 ]")
for stat in top_stats[:10]:
print(stat)
# HUD functions
def draw_player_stats(surf, x, y, pct, color = GREEN, bar_length = 100):
if pct < 0:
pct = 0
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 = color
elif pct > 0.3:
col = YELLOW
else:
col = RED
pg.draw.rect(surf, col, fill_rect)
pg.draw.rect(surf, WHITE, outline_rect, 2)
# Used for loading sprite sheet images into a list of images
def load_spritesheet(sheet, size):
image_list = []
sheet_width = sheet.get_width()
sheet_height = sheet.get_height()
columns = int(sheet_width / size)
rows = int(sheet_height / size)
# Create a new blank image
for col in range(0, columns):
y = col * size
for row in range(0, rows):
x = row * size
image = pg.Surface([size, size], pg.SRCALPHA).convert_alpha()
# Copy the sprite from the large sheet onto the smaller image
image.blit(sheet, (0, 0), (x, y, size, size))
image_list.append(image)
# Return the separate images stored in a list.
return image_list
# Used to see if the player is in talking range of an Npc
def npc_talk_rect(one, two):
if one.hit_rect.colliderect(two.talk_rect):
return True
else:
return False
def mob_hit_rect(one, two):
if one.hit_rect.colliderect(two.hit_rect):
return True
else:
return False
# Used to define hits from melee attacks
def melee_hit_rect(one, two):
if one.mother.weapon_hand == 'weapons':
if True in (one.mid_weapon_melee_rect.colliderect(two.hit_rect), one.weapon_melee_rect.colliderect(two.hit_rect), one.melee_rect.colliderect(two.hit_rect)): #checks for either the fist hitting a mob or the cente or tip of weapon.
if one.swing_weapon1: # This differentiates between weapons that are being swung and those that are thrusted.
if one.frame > 3:
return True
elif one.frame < 3:
return True
else:
return False
elif one.mother.weapon_hand == 'weapons2':
if True in (one.mid_weapon2_melee_rect.colliderect(two.hit_rect), one.weapon2_melee_rect.colliderect(two.hit_rect), one.melee2_rect.colliderect(two.hit_rect)): #checks for either the fist hitting a mob or the center or tip of weapon.
if one.swing_weapon2:
if one.frame > 3:
return True
elif one.frame < 3:
return True
return False
def breakable_melee_hit_rect(one, two):
if one.mother.weapon_hand == 'weapons':
if True in (one.mid_weapon_melee_rect.colliderect(two.trunk.hit_rect), one.weapon_melee_rect.colliderect(two.trunk.hit_rect), one.melee_rect.colliderect(two.trunk.hit_rect)):
if one.swing_weapon1: # This differentiates between weapons that are being swung and those that are thrusted.
if one.frame > 6:
return True
elif one.frame < 6:
return True
else:
return False
elif one.mother.weapon_hand == 'weapons2':
if True in (one.mid_weapon2_melee_rect.colliderect(two.trunk.hit_rect), one.weapon2_melee_rect.colliderect(two.trunk.hit_rect), one.melee2_rect.colliderect(two.trunk.hit_rect)):
if one.swing_weapon2:
if one.frame > 6:
return True
elif one.frame < 6:
return True
return False
# Used to define fireball hits
def fire_collide(one, two):
if one.hit_rect.colliderect(two.hit_rect):
return True
else:
return False
def entryway_collide(one, two):
if one.rect.colliderect(two.hit_rect):
return True
else:
return False
class Game:
def __init__(self):
self.window_ratio = .97
self.screen_width = WIDTH
self.screen_height = int(HEIGHT * self.window_ratio)
self.flags = pg.NOFRAME
#self.screen = pg.display.set_mode((self.screen_width, HEIGHT), pg.FULLSCREEN)
icon_image = pg.image.load(path.join(img_folder, ICON_IMG))
pg.display.set_icon(icon_image)
self.screen = pg.display.set_mode((self.screen_width, self.screen_height), self.flags)
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.dt = 0.0001
# Loads Mutant Python Logo Faid in/out.
mpy_logo_image = pg.image.load(path.join(img_folder, LOGO_IMAGE)).convert_alpha()
mpy_logo_image = pg.transform.scale(mpy_logo_image, (int(self.screen_height/4), int(self.screen_height/4)))
logo_width = mpy_logo_image.get_width()
logo_placement = ((self.screen_width - logo_width)/2, (self.screen_height - logo_width)/2)
mpy_words_image = pg.image.load(path.join(img_folder, MPY_WORDS)).convert_alpha()
mpy_words_image = pg.transform.scale(mpy_words_image, (int(self.screen_width/4), int(self.screen_height/8)))
words_height = mpy_words_image.get_height()
words_width = mpy_words_image.get_width()
words_placement = ((self.screen_width - words_width)/2, (self.screen_height - words_height)/2)
for i in range(0, 256):
#self.clock.tick(120)
self.screen.fill(BLACK)
mpy_logo_image.set_alpha(i)
self.screen.blit(mpy_logo_image, logo_placement)
pg.display.flip()
for i in range(255, 0, -1):
#self.clock.tick(120)
self.screen.fill(BLACK)
mpy_logo_image.set_alpha(i)
self.screen.blit(mpy_logo_image, logo_placement)
pg.display.flip()
for i in range(0, 256):
#self.clock.tick(120)
self.screen.fill(BLACK)
mpy_words_image.set_alpha(i)
self.screen.blit(mpy_words_image, words_placement)
pg.display.flip()
self.load_data()
for i in range(255, 0, -1):
#self.clock.tick(120)
self.screen.fill(BLACK)
mpy_words_image.set_alpha(i)
self.screen.blit(mpy_words_image, words_placement)
pg.display.flip()
self.channel3 = pg.mixer.Channel(2)
self.channel4 = pg.mixer.Channel(3) # Fire, water fall
self.channel5 = pg.mixer.Channel(4) # breaking sounds and other effects
self.channel6 = pg.mixer.Channel(5) # vehicle sounds
self.channel7 = pg.mixer.Channel(6) # explosions
def on_screen(self, sprite, threshold = 50):
rect = self.camera.apply(sprite)
if rect.right < -threshold or rect.bottom < -threshold or rect.left > self.screen_width + threshold or rect.top > self.screen_height + threshold:
return False
else:
return True
def on_screen_no_edge(self, sprite): #no threashold for slightly faster draw.
rect = self.camera.apply(sprite)
if rect.right < 0 or rect.bottom < 0 or rect.left > self.screen_width or rect.top > self.screen_height:
return False
else:
return True
def is_living(self, npc_kind):
if 'dead' in self.people[npc_kind]:
if self.people[npc_kind]['dead']:
return False
else:
return True
else:
return True
def format_date(self):
directive = "%m-%d-%Y_%H-%M-%S"
return datetime.datetime.now().strftime(directive)
def save_sprite_locs(self):
# This block stores all sprite locations and their health/inventories in the map_sprite_data_list so the game remembers where everything is.
npc_list = []
animal_list = []
item_list = []
vehicle_list = []
breakable_list = []
if not self.underworld:
for npc in self.npcs:
if npc not in self.companions:
npc_list.append({'name': npc.species, 'location': npc.pos, 'health': npc.stats['health'], 'inventory': npc.inventory, 'colors': npc.colors})
self.map_sprite_data_list[int(self.world_location.x)][int(self.world_location.y)].npcs = npc_list
for animal in self.animals:
if animal not in self.companions:
if animal != self.player.vehicle:
animal_list.append({'name': animal.species, 'location': animal.pos, 'health': animal.stats['health']})
self.map_sprite_data_list[int(self.world_location.x)][int(self.world_location.y)].animals = animal_list
for item in self.dropped_items:
item_list.append({'name': item.name, 'location': item.pos, 'rotation': item.rot})
self.map_sprite_data_list[int(self.world_location.x)][int(self.world_location.y)].items = item_list
for vehicle in self.vehicles:
if vehicle.driver != self.player:
vehicle_list.append({'name': vehicle.species, 'location': vehicle.pos, 'health': vehicle.stats['health']})
self.map_sprite_data_list[int(self.world_location.x)][int(self.world_location.y)].vehicles = vehicle_list
for breakable in self.breakable:
breakable_list.append({'name': breakable.name, 'location': breakable.center, 'w': breakable.w, 'h': breakable.h, 'rotation': breakable.rot})
self.map_sprite_data_list[int(self.world_location.x)][int(self.world_location.y)].breakable = breakable_list
else:
for npc in self.npcs:
if npc not in self.companions:
npc_list.append({'name': npc.species, 'location': npc.pos, 'health': npc.stats['health'], 'inventory': npc.inventory, 'colors': npc.colors})
self.underworld_sprite_data_dict[self.previous_map].npcs = npc_list
for animal in self.animals:
if animal not in self.companions:
if animal != self.player.vehicle:
animal_list.append({'name': animal.species, 'location': animal.pos, 'health': animal.stats['health']})
self.underworld_sprite_data_dict[self.previous_map].animals = animal_list
for item in self.dropped_items:
item_list.append({'name': item.name, 'location': item.pos, 'rotation': item.rot})
self.underworld_sprite_data_dict[self.previous_map].items = item_list
for vehicle in self.vehicles:
vehicle_list.append({'name': vehicle.species, 'location': vehicle.pos, 'health': vehicle.stats['health']})
self.underworld_sprite_data_dict[self.previous_map].vehicles = vehicle_list
for breakable in self.breakable:
breakable_list.append({'name': breakable.name, 'location': breakable.center, 'w': breakable.w, 'h': breakable.h, 'rotation': breakable.rot})
self.underworld_sprite_data_dict[self.previous_map].breakable = breakable_list
def save(self):
self.screen.fill(BLACK)
self.save_sprite_locs()
possessing = self.player.possessing
if self.player.possessing:
self.player.possessing.depossess()
self.player.dragon = False
if 'dragon' in self.player.equipped['race']: # Makes it so you aren't a dragon when you load a game.
self.player.equipped['race'] = self.player.equipped['race'].replace('dragon', '')
self.player.body.update_animations()
self.draw_text('Saving....', self.script_font, 50, WHITE, self.screen_width / 2, self.screen_height / 2, align="topright")
pg.display.flip()
sleep(0.5)
companion_list = []
for companion in self.companions:
companion_list.append(companion.species)
vehicle_name = None
if self.player.in_vehicle:
vehicle_name = self.player.vehicle.species
updated_equipment = [UPGRADED_WEAPONS, UPGRADED_HATS, UPGRADED_TOPS, UPGRADED_GLOVES, UPGRADED_BOTTOMS, UPGRADED_SHOES, UPGRADED_ITEMS]
save_list = [self.player.inventory, self.player.equipped, self.player.stats, [self.player.pos.x, self.player.pos.y], self.previous_map, [self.world_location.x, self.world_location.y], self.chests, self.overworld_map, updated_equipment, self.people, self.quests, self.player.colors, vehicle_name, companion_list, self.map_sprite_data_list, self.underworld_sprite_data_dict, self.key_map]
if not path.isdir(saves_folder): makedirs(saves_folder)
with open(path.join(saves_folder, self.player.race + "_" + self.format_date() + ".sav"), "wb", -1) as FILE:
pickle.dump(save_list, FILE)
if possessing:
possessing.possess(self.player)
def load_save(self, file_name):
load_file = []
with open(file_name, "rb", -1) as FILE:
load_file = pickle.load(FILE)
# Loads saved upgraded equipment:
self.people = load_file[9] # Updates NPCs
self.quests = load_file[10] # Updates Quests from save
self.saved_vehicle = load_file[12]
self.chests = load_file[6] # Updates chests from dave
self.saved_companions = load_file[13]
self.map_sprite_data_list = load_file[14]
self.underworld_sprite_data_dict = load_file[15]
self.key_map = load_file[16]
updated_equipment = load_file[8]
UPGRADED_WEAPONS.update(updated_equipment[0])
UPGRADED_HATS.update(updated_equipment[1])
UPGRADED_TOPS.update(updated_equipment[2])
UPGRADED_GLOVES.update(updated_equipment[3])
UPGRADED_BOTTOMS.update(updated_equipment[4])
UPGRADED_SHOES.update(updated_equipment[5])
UPGRADED_ITEMS.update(updated_equipment[6])
WEAPONS.update(UPGRADED_WEAPONS)
HATS.update(UPGRADED_HATS)
TOPS.update(UPGRADED_TOPS)
BOTTOMS.update(UPGRADED_BOTTOMS)
GLOVES.update(UPGRADED_GLOVES)
SHOES.update(UPGRADED_SHOES)
ITEMS.update(UPGRADED_ITEMS)
self.player.inventory = load_file[0]
self.player.equipped = load_file[1]
self.player.race = self.player.equipped['race']
self.player.stats = load_file[2]
self.player.colors = load_file[11]
self.previous_map = load_file[4]
self.world_location = vec(load_file[5])
self.load_map(self.previous_map)
self.player.pos = vec(load_file[3])
self.player.human_body.update_animations()
self.player.dragon_body.update_animations()
self.player.calculate_fire_power()
self.player.calculate_perks()
self.overworld_map = load_file[7]
#Update hud stats
self.hud_health_stats = self.player.stats
self.hud_health = self.hud_health_stats['health'] / self.hud_health_stats['max health']
self.hud_stamina = self.hud_health_stats['stamina'] / self.hud_health_stats['max stamina']
self.hud_magica = self.hud_health_stats['magica'] / self.hud_health_stats['max magica']
self.hud_hunger = self.hud_health_stats['hunger'] / self.hud_health_stats['max hunger']
# Loads saved companions
for companion in self.saved_companions:
for npc_type in NPC_TYPE_LIST:
if companion in eval(npc_type.upper()):
rand_angle = randrange(0, 360)
random_vec = vec(170, 0).rotate(-rand_angle)
follower_center = vec(self.player.pos + random_vec)
if npc_type == 'animals':
if companion != self.saved_vehicle: #Makes it so it doesn't double load companions you are riding.
follower = Animal(self, follower_center.x, follower_center.y, self.map, companion)
follower.offensive = False
follower.make_companion()
else:
follower = Npc(self, follower_center.x, follower_center.y, self.map, companion)
follower.offensive = False
follower.make_companion()
self.saved_companions = []
# Enters vehicle if you saved it inside a vehicle
for vehicle in self.vehicles:
if vehicle.kind == self.saved_vehicle:
vehicle.enter_vehicle(self.player)
for vehicle in self.flying_vehicles:
if vehicle.kind == self.saved_vehicle:
vehicle.enter_vehicle(self.player)
if self.saved_vehicle in ANIMALS:
mount = Animal(self, self.player.pos.x, self.player.pos.y, self.map, self.saved_vehicle)
mount.mount(self.player)
self.load_over_map(self.overworld_map)
def update_old_save(self, file_name):
load_file = []
with open(file_name, "rb", -1) as FILE:
load_file = pickle.load(FILE)
# Loads saved upgraded equipment:
self.people = PEOPLE # Updates NPCs
self.quests = QUESTS # Updates Quests from save
self.chests = CHESTS # Updates chests from dave
self.key_map = KEY_MAP
updated_equipment = load_file[8]
UPGRADED_WEAPONS.update(updated_equipment[0])
UPGRADED_HATS.update(updated_equipment[1])
UPGRADED_TOPS.update(updated_equipment[2])
UPGRADED_GLOVES.update(updated_equipment[3])
UPGRADED_BOTTOMS.update(updated_equipment[4])
UPGRADED_SHOES.update(updated_equipment[5])
UPGRADED_ITEMS.update(updated_equipment[6])
WEAPONS.update(UPGRADED_WEAPONS)
HATS.update(UPGRADED_HATS)
TOPS.update(UPGRADED_TOPS)
BOTTOMS.update(UPGRADED_BOTTOMS)
GLOVES.update(UPGRADED_GLOVES)
SHOES.update(UPGRADED_SHOES)
ITEMS.update(UPGRADED_ITEMS)
self.player.inventory = load_file[0]
self.player.equipped = load_file[1]
self.player.race = self.player.equipped['race']
self.player.stats = load_file[2]
self.previous_map = load_file[4]
self.world_location = vec(load_file[5])
self.load_map(self.previous_map)
self.player.pos = vec(load_file[3])
self.player.human_body.update_animations()
self.player.dragon_body.update_animations()
self.player.calculate_fire_power()
self.player.calculate_perks()
self.overworld_map = load_file[7]
self.load_over_map(self.overworld_map)
def draw_text(self, text, font_name, size, color, x, y, align="topleft"):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(**{align: (x, y)})
self.screen.blit(text_surface, text_rect)
def load_data(self):
self.wall_tiles = []
self.ore_tiles = []
self.empty_tiles = []
self.water_tiles = []
self.shallows_tiles = []
self.lava_tiles = []
self.long_grass_tiles = []
self.wall_tile_layer = 0
self.water_tile_layer = 0
self.long_grass_layer = 0
self.lava_layer = 0
self.title_font = HEADING_FONT
self.hud_font = HUD_FONT
self.script_font = SCRIPT_FONT
self.dim_screen = pg.Surface(self.screen.get_size()).convert_alpha()
self.dim_screen.fill((SHADOW))
self.body_surface = pg.Surface((64, 64)).convert()
self.body_surface.set_colorkey(BLACK)
self.open_book_image = pg.image.load(path.join(img_folder, 'open_book.png')).convert()
self.open_book_image = pg.transform.scale(self.open_book_image, (self.screen_width, self.screen_height - 30))
self.open_letter_image = pg.image.load(path.join(img_folder, 'open_letter.png')).convert()
self.open_letter_image = pg.transform.scale(self.open_letter_image, (self.screen_width, self.screen_height - 30))
self.over_minimap_image = pg.image.load(path.join(img_folder, OVERWORLD_MAP_IMAGE)).convert()
self.over_minimap_image = pg.transform.scale(self.over_minimap_image, (self.screen_width, self.screen_height))
self.compass_image = pg.image.load(path.join(img_folder, 'compass.png')).convert_alpha()
self.player_tur = pg.image.load(path.join(img_folder, PLAYER_TUR)).convert_alpha()
#self.player_tank = pg.image.load(path.join(img_folder, PLAYER_TANK)).convert_alpha()
#self.tank_in_water = pg.image.load(path.join(img_folder, TANK_IN_WATER)).convert_alpha()
#self.sunken_tank = pg.image.load(path.join(img_folder, SUNKEN_TANK)).convert_alpha()
self.lock_image = pg.image.load(path.join(img_folder, 'lock.png')).convert_alpha()
self.lock_keyway_image = pg.image.load(path.join(img_folder, 'lock_keyway.png')).convert_alpha()
self.keyed_keyway_image = pg.image.load(path.join(img_folder, 'keyed_keyway.png')).convert_alpha()
self.lock_pick_image = pg.image.load(path.join(img_folder, 'lock_pick.png')).convert_alpha()
self.swim_shadow_image = pg.image.load(path.join(img_folder, 'swim_shadow.png')).convert_alpha()
self.mech_back_image = pg.image.load(path.join(img_folder, 'mech_back_lights.png')).convert_alpha()
#self.rock_shadow_image = pg.image.load(path.join(img_folder, 'rock_shadow.png')).convert_alpha()
self.invisible_image = pg.image.load(path.join(img_folder, 'invisible.png')).convert_alpha()
# creates a dictionary of animal images. This is not in the settings file like the others because of the order it needs to import info.
ANIMAL_IMAGES = {}
for animal in ANIMAL_ANIMATIONS:
temp_list = []
number_of_files = len([name for name in os.listdir(animals_folder) if animal in name if os.path.isfile(os.path.join(animals_folder, name))])
for i in range(1, number_of_files + 1):
filename = animal + '{}.png'.format(i)
temp_list.append(filename)
ANIMAL_IMAGES[animal] = temp_list
# Loads animal images
self.animal_images = {}
for kind in ANIMAL_IMAGES:
temp_list = []
for i, picture in enumerate(ANIMAL_IMAGES[kind]):
img = pg.image.load(path.join(animals_folder, ANIMAL_IMAGES[kind][i])).convert_alpha()
temp_list.append(img)
self.animal_images[kind] = temp_list
# associates the animation frames with the animal images
self.animal_animations = {}
for kind in ANIMAL_ANIMATIONS:
temp_dict = {}
for animation in ANIMAL_ANIMATIONS[kind]:
temp_list = []
for frame in ANIMAL_ANIMATIONS[kind][animation]:
temp_list.append(self.animal_images[kind][frame - 1])
temp_dict[animation] = temp_list
self.animal_animations[kind] = temp_dict
self.bullet_images = {}
for x, size in enumerate(BULLET_SIZES):
for i, item in enumerate(BULLET_IMAGES):
bullet_img = pg.image.load(path.join(bullets_folder, BULLET_IMAGES[i])).convert_alpha()
if size != 'ar':
if i != 0:
img = pg.transform.scale(bullet_img, (6*(x + 1), 4*(x + 1)))
else:
img = pg.transform.scale(bullet_img, (12 * (x + 1), 4 * (x + 1)))
else:
img = pg.transform.scale(bullet_img, (80, 10))
bullet_name = size + str(i)
self.bullet_images[bullet_name] = img
self.door_images = []
for i, item in enumerate(DOOR_IMAGES):
img = pg.image.load(path.join(doors_folder, DOOR_IMAGES[i])).convert_alpha()
self.door_images.append(img)
self.door_break_images = []
for i, item in enumerate(DOOR_BREAK_IMAGES):
img = pg.image.load(path.join(door_break_folder, DOOR_BREAK_IMAGES[i])).convert_alpha()
self.door_break_images.append(img)
self.item_images = []
for i, item in enumerate(ITEM_IMAGES):
img = pg.image.load(path.join(items_folder, ITEM_IMAGES[i])).convert_alpha()
self.item_images.append(img)
self.enchantment_images = []
for i, item in enumerate(ENCHANTMENT_IMAGES):
img = pg.image.load(path.join(enchantments_folder, ENCHANTMENT_IMAGES[i])).convert_alpha()
self.enchantment_images.append(img)
self.weapon_images = []
for i, weapon in enumerate(WEAPON_IMAGES):
img = pg.image.load(path.join(weapons_folder, WEAPON_IMAGES[i])).convert_alpha()
self.weapon_images.append(img)
self.hat_images = []
for i, hat in enumerate(HAT_IMAGES):
img = pg.image.load(path.join(hats_folder, HAT_IMAGES[i])).convert_alpha()
self.hat_images.append(img)
self.hair_images = []
for i, hair in enumerate(HAIR_IMAGES):
img = pg.image.load(path.join(hair_folder, HAIR_IMAGES[i])).convert_alpha()
self.hair_images.append(img)
self.top_images = []
for i, top in enumerate(TOP_IMAGES):
img = pg.image.load(path.join(tops_folder, TOP_IMAGES[i])).convert_alpha()
self.top_images.append(img)
self.bottom_images = []
for i, bottom in enumerate(BOTTOM_IMAGES):
img = pg.image.load(path.join(bottoms_folder, BOTTOM_IMAGES[i])).convert_alpha()
self.bottom_images.append(img)
self.shoe_images = []
for i, shoe in enumerate(SHOE_IMAGES):
img = pg.image.load(path.join(shoes_folder, SHOE_IMAGES[i])).convert_alpha()
self.shoe_images.append(img)
self.glove_images = []
for i, glove in enumerate(GLOVE_IMAGES):
img = pg.image.load(path.join(gloves_folder, GLOVE_IMAGES[i])).convert_alpha()
self.glove_images.append(img)
self.magic_images = []
for i, magic in enumerate(MAGIC_IMAGES):
img = pg.image.load(path.join(magic_folder, MAGIC_IMAGES[i])).convert_alpha()
self.magic_images.append(img)
self.light_mask_images = []
for i, val in enumerate(LIGHT_MASK_IMAGES):
img = pg.image.load(path.join(light_masks_folder, LIGHT_MASK_IMAGES[i])).convert_alpha()
self.light_mask_images.append(img)
self.flashlight_masks = []
temp_img = pg.transform.scale(self.light_mask_images[3], (int(600 * 2.8), 600))
for rot in range(0, 120):
new_image = pg.transform.rotate(temp_img, rot*3)
self.flashlight_masks.append(new_image)
self.magic_animation_images = []
for image in self.magic_images:
image_list = []
# enlarge image animation
for i in range(0, 5):
new_image = pg.transform.scale(image, (25*i, 25*i))
image_list.append(new_image)
# shrink animation
for i in range(1, 10):
new_image = pg.transform.scale(image, (int(140/i), int(140/i)))
image_list.append(new_image)
self.magic_animation_images.append(image_list)
self.gender_images = []
for i, gender in enumerate(GENDER_IMAGES):
img = pg.image.load(path.join(gender_folder, GENDER_IMAGES[i])).convert_alpha()
self.gender_images.append(img)
self.corpse_images = []
for i, corpse in enumerate(CORPSE_IMAGES):
img = pg.image.load(path.join(corpse_folder, CORPSE_IMAGES[i])).convert_alpha()
self.corpse_images.append(img)
self.vehicle_images = []
for i, x in enumerate(VEHICLES_IMAGES):
img = pg.image.load(path.join(vehicles_folder, VEHICLES_IMAGES[i])).convert_alpha()
self.vehicle_images.append(img)
self.color_swatch_images = []
for i, x in enumerate(COLOR_SWATCH_IMAGES):
img = pg.image.load(path.join(color_swatches_folder, COLOR_SWATCH_IMAGES[i])).convert()
self.color_swatch_images.append(img)
self.race_images = []
for i, race in enumerate(RACE_IMAGES):
img = pg.image.load(path.join(race_folder, RACE_IMAGES[i])).convert_alpha()
self.race_images.append(img)
self.fire_images = []
for i, x in enumerate(FIRE_IMAGES):
img = pg.image.load(path.join(fire_folder, FIRE_IMAGES[i])).convert_alpha()
self.fire_images.append(img)
self.shock_images = []
for i, x in enumerate(SHOCK_IMAGES):
img = pg.image.load(path.join(shock_folder, SHOCK_IMAGES[i])).convert_alpha()
self.shock_images.append(img)
self.electric_door_images = []
for i, x in enumerate(ELECTRIC_DOOR_IMAGES):
img = pg.image.load(path.join(electric_door_folder, ELECTRIC_DOOR_IMAGES[i])).convert_alpha()
self.electric_door_images.append(img)
self.loading_screen_images = []
for i, screen in enumerate(LOADING_SCREEN_IMAGES):
img = pg.image.load(path.join(loading_screen_folder, LOADING_SCREEN_IMAGES[i])).convert()
self.loading_screen_images.append(img)
self.tree_images = {}
self.breakable_images = {}
for kind in BREAKABLE_IMAGES:
if 'tree' not in kind:
temp_list = []
for i, picture in enumerate(BREAKABLE_IMAGES[kind]):
img = pg.image.load(path.join(breakable_folder, BREAKABLE_IMAGES[kind][i])).convert_alpha()
temp_list.append(img)
self.breakable_images[kind] = temp_list
else:
temp_list = []
temp_list2 = []
temp_list3 = []
for i, picture in enumerate(BREAKABLE_IMAGES[kind]):
img = pg.image.load(path.join(breakable_folder, BREAKABLE_IMAGES[kind][i])).convert_alpha()
scaled_image = pg.transform.scale(img, (TREE_SIZES['sm'], TREE_SIZES['sm']))
temp_list.append(scaled_image)
scaled_image = pg.transform.scale(img, (TREE_SIZES['md'], TREE_SIZES['md']))
temp_list2.append(scaled_image)
scaled_image = pg.transform.scale(img, (TREE_SIZES['lg'], TREE_SIZES['lg']))
temp_list3.append(scaled_image)
self.tree_images['sm' + kind] = temp_list
self.tree_images['md' + kind] = temp_list2
self.tree_images['lg' + kind] = temp_list3
self.portal_sheet = pg.image.load(PORTAL_SHEET).convert_alpha()
self.portal_images = load_spritesheet(self.portal_sheet, 256)
self.fireball_images = []
for i, x in enumerate(FIREBALL_IMAGES):
img = pg.image.load(path.join(fireball_folder, FIREBALL_IMAGES[i])).convert_alpha()
self.fireball_images.append(img)
self.explosion_images = []
for i, x in enumerate(EXPLOSION_IMAGES):
img = pg.image.load(path.join(explosion_folder, EXPLOSION_IMAGES[i])).convert_alpha()
self.explosion_images.append(img)
self.humanoid_images = {}
for kind in HUMANOID_IMAGES:
temp_list = []
for i, picture in enumerate(HUMANOID_IMAGES[kind]):
temp_folder = kind.replace('images', 'parts_folder')
img = pg.image.load(path.join(eval(temp_folder), HUMANOID_IMAGES[kind][i])).convert_alpha()
temp_list.append(img)
self.humanoid_images[kind] = temp_list
self.gun_flashes = []
for img in MUZZLE_FLASHES:
self.gun_flashes.append(pg.image.load(path.join(img_folder, img)).convert_alpha())
# lighting effect
self.fog = pg.Surface((self.screen_width, self.screen_height))
self.fog.fill(NIGHT_COLOR)
# Sound loading
self.effects_sounds = {}
for key in EFFECTS_SOUNDS:
self.effects_sounds[key] = pg.mixer.Sound(path.join(snd_folder, EFFECTS_SOUNDS[key]))
self.weapon_sounds = {}
for weapon in WEAPON_SOUNDS:
self.weapon_sounds[weapon] = []
for snd in WEAPON_SOUNDS[weapon]:
s = pg.mixer.Sound(path.join(snd_folder, snd))
s.set_volume(0.3)
self.weapon_sounds[weapon].append(s)
self.weapon_hit_sounds = {}
for weapon in WEAPON_SOUNDS:
self.weapon_hit_sounds[weapon] = []
for snd in WEAPON_HIT_SOUNDS[weapon]:
s = pg.mixer.Sound(path.join(snd_folder, snd))
s.set_volume(0.3)
self.weapon_hit_sounds[weapon].append(s)
self.zombie_moan_sounds = []
for snd in ZOMBIE_MOAN_SOUNDS:
s = pg.mixer.Sound(path.join(snd_folder, snd))
s.set_volume(0.2)
self.zombie_moan_sounds.append(s)
self.wraith_sounds = []
for snd in WRAITH_SOUNDS:
s = pg.mixer.Sound(path.join(snd_folder, snd))
s.set_volume(0.2)
self.wraith_sounds.append(s)
self.punch_sounds = []
for snd in PUNCH_SOUNDS:
s = pg.mixer.Sound(path.join(snd_folder, snd))
s.set_volume(0.2)
self.punch_sounds.append(s)
self.male_player_hit_sounds = []
for snd in MALE_PLAYER_HIT_SOUNDS:
self.male_player_hit_sounds.append(pg.mixer.Sound(path.join(snd_folder, snd)))
self.female_player_hit_sounds = []
for snd in FEMALE_PLAYER_HIT_SOUNDS:
self.female_player_hit_sounds.append(pg.mixer.Sound(path.join(snd_folder, snd)))
self.female_player_voice = {}
for key in FEMALE_PLAYER_VOICE:
temp_list = []
for i, snd in enumerate(FEMALE_PLAYER_VOICE[key]):
temp_list.append(pg.mixer.Sound(path.join(female_player_sound_folder, FEMALE_PLAYER_VOICE[key][i])))
self.female_player_voice[key] = temp_list
self.male_player_voice = {}
for key in MALE_PLAYER_VOICE:
temp_list = []
for i, snd in enumerate(MALE_PLAYER_VOICE[key]):
temp_list.append(pg.mixer.Sound(path.join(female_player_sound_folder, MALE_PLAYER_VOICE[key][i])))
self.male_player_voice[key] = temp_list
self.zombie_hit_sounds = []
for snd in ZOMBIE_HIT_SOUNDS:
self.zombie_hit_sounds.append(pg.mixer.Sound(path.join(snd_folder, snd)))
self.lock_picking_sounds = []
for snd in LOCK_PICKING_SOUNDS:
self.lock_picking_sounds.append(pg.mixer.Sound(path.join(snd_folder, snd)))
def new(self):
pg.mixer.music.load(path.join(music_folder, TITLE_MUSIC))
pg.mixer.music.play(loops=-1)
title_image = pg.image.load(path.join(img_folder, TITLE_IMAGE)).convert()
title_image = pg.transform.scale(title_image, (self.screen_width, self.screen_height))
self.map = None
self.continued_game = False
self.in_load_menu = False
self.in_npc_menu = False
self.in_settings_menu = False
waiting = True
i = 0
while waiting:
self.clock.tick(FPS)
self.screen.fill(BLACK)
title_image.set_alpha(i)
self.screen.blit(title_image, (0, 0))
if i > 240:
self.draw_text('Press any key to begin or C to continue', self.script_font, 30, WHITE, self.screen_width / 2, self.screen_height - 120,
align="center")
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
waiting = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_c:
waiting = False
self.in_load_menu = True
self.continued_game = True
elif event.key == pg.K_n: # Enters the NPC creation tool
if event.mod & pg.KMOD_CTRL:
waiting = False
self.in_npc_menu = True
else:
waiting = False
elif event.key != pg.K_n:
if not event.mod & pg.KMOD_CTRL:
waiting = False
pg.display.flip()
i += 1
if i > 255:
i = 255
# initialize all variables and do all the setup for a new game
# Used for controlling day and night
self.darkness = 0
self.dark_color = (255, 255, 255)
self.time_of_day = 0
self.nightfall = False
self.sunrise = False
self.night = False
self.last_darkness_change = 0
self.day_start_time = pg.time.get_ticks()
self.map_sprite_data_list = []
self._player_inside = False
self.compass_rot = 0
self.people = PEOPLE
self.saved_vehicle = []
self.saved_companions = []
self.underworld = False
self.quests = QUESTS
self.chests = CHESTS
self.key_map = KEY_MAP
self.bg_music = BG_MUSIC
self.previous_music = TITLE_MUSIC
self.portal_location = vec(0, 0)
self.portal_combo = ''
self.load_menu = Load_Menu(self)
self.settings_menu = Settings_Menu(self)
self.guard_alerted = False
self.hud_map = False
self.hud_overmap = False
self.message_text = True
self.message = ''
self.map_type = None
self.group = PyscrollGroup()
self.all_sprites = pg.sprite.LayeredUpdates() # Used for all non_static sprites
self.all_static_sprites = pg.sprite.Group() # used for all static sprites
self.sprites_on_screen = pg.sprite.Group()
self.moving_targets = pg.sprite.Group() # Used for all moving things bullets interact with
self.moving_targets_on_screen = pg.sprite.Group()
self.mobs = pg.sprite.Group()
self.mobs_on_screen = pg.sprite.Group()
self.npc_bodies = pg.sprite.Group()
self.npc_bodies_on_screen = pg.sprite.Group()
self.npcs = pg.sprite.Group()
self.npcs_on_screen = pg.sprite.Group()
self.animals = pg.sprite.Group()
self.animals_on_screen = pg.sprite.Group()
self.fires = pg.sprite.Group()
self.fires_on_screen = pg.sprite.Group()
self.electric_doors = pg.sprite.Group()
self.electric_doors_on_screen = pg.sprite.Group()
self.entryways = pg.sprite.Group()
self.entryways_on_screen = pg.sprite.Group()
self.breakable = pg.sprite.Group()
self.breakable_on_screen = pg.sprite.Group()
self.corpses = pg.sprite.Group()
self.corpses_on_screen = pg.sprite.Group()
self.dropped_items = pg.sprite.Group()
self.dropped_items_on_screen = pg.sprite.Group()
self.beds = pg.sprite.Group()
self.beds_on_screen = pg.sprite.Group()
self.obstacles = pg.sprite.Group()
self.obstacles_on_screen = pg.sprite.Group()
self.walls = pg.sprite.Group()
self.walls_on_screen = pg.sprite.Group()
self.barriers = pg.sprite.Group()
self.barriers_on_screen = pg.sprite.Group()
self.elevations = pg.sprite.Group()
self.elevations_on_screen = pg.sprite.Group()
self.water = pg.sprite.Group()
self.water_on_screen = pg.sprite.Group()
self.shallows = pg.sprite.Group()
self.long_grass = pg.sprite.Group()
self.long_grass_on_screen = pg.sprite.Group()
self.shallows_on_screen = pg.sprite.Group()
self.lava = pg.sprite.Group()
self.lava_on_screen = pg.sprite.Group()
self.inside = pg.sprite.Group()
self.inside_on_screen = pg.sprite.Group()
self.climbs = pg.sprite.Group()
self.climbs_on_screen = pg.sprite.Group()
self.vehicles = pg.sprite.Group()
self.vehicles_on_screen = pg.sprite.Group()
self.aipaths = pg.sprite.Group()
self.lights = pg.sprite.Group()
self.firepots = pg.sprite.Group()
self.arrows = pg.sprite.Group()
self.chargers = pg.sprite.Group()
self.mechsuits = pg.sprite.Group()
self.detectors = pg.sprite.Group()
self.detectables = pg.sprite.Group()
self.portals = pg.sprite.Group()
self.door_walls = pg.sprite.Group()
self.nospawn = pg.sprite.Group()
self.doors = pg.sprite.Group()
self.toilets = pg.sprite.Group()
self.player_group = pg.sprite.Group()
self.players = pg.sprite.Group()
self.grabable_animals = pg.sprite.Group()
self.explosions = pg.sprite.Group()
self.shocks = pg.sprite.Group()
self.fireballs = pg.sprite.Group()
self.firepits = pg.sprite.Group()
self.containers = pg.sprite.Group()
self.chest_containers = pg.sprite.Group()
self.bullets = pg.sprite.Group()
self.enemy_bullets = pg.sprite.Group()
self.enemy_fireballs = pg.sprite.Group()
self.work_stations = pg.sprite.Group()
self.climbables_and_jumpables = pg.sprite.Group()
self.all_vehicles = pg.sprite.Group()
self.companions = pg.sprite.Group()
self.companion_bodies = pg.sprite.Group()
self.boats = pg.sprite.Group()
self.amphibious_vehicles = pg.sprite.Group()
self.flying_vehicles = pg.sprite.Group()
self.land_vehicles = pg.sprite.Group()
self.turrets = pg.sprite.Group()
self.occupied_vehicles = pg.sprite.Group()
self.random_targets = pg.sprite.Group()
self.target_list = [self.random_targets, self.entryways, self.work_stations, self.moving_targets, self.aipaths]
self.new_game = True
self.respawn = False
self.previous_map = "1.tmx"
self.world_location = vec(1, 1)
self.underworld_sprite_data_dict = {}
self.player = Player(self) # Creates initial player object
if self.new_game: # Why do I have to variables: new_game and conitnued_game
self.in_character_menu = True
self.character_menu = Character_Design_Menu(self)
self.generic_npc = Npc(self, 0, 0, map, 'generic') # Spawns a generic villager npc to be modified
self.npc_menu = Npc_Design_Menu(self, self.generic_npc)
if self.in_npc_menu:
self.npc_menu.update()
self.generic_npc.kill()
if not self.continued_game:
self.character_menu.update()
self.in_character_menu = False
self.overworld_map = START_WORLD
if not self.continued_game:
self.load_over_map(self.overworld_map) # Loads world map for first world. This will allow me to load other world maps later.
if not self.continued_game:
self.change_map(None, RACE[self.player.race]['start map'], RACE[self.player.race]['start pos'])
self.menu = Inventory_Menu(self)
self.stats_menu = Stats_Menu(self)
self.quest_menu = None
self.fly_menu = None
self.in_menu = False
self.in_inventory_menu = False
self.store_menu = None
self.in_store_menu = False
self.in_stats_menu = False
self.in_loot_menu = False
self.in_lock_menu = False
self.in_station_menu = False
self.in_quest_menu = False
self.in_dialogue_menu = False
self.dialogue_menu = None
self.dialogue_menu_npc = None
self.last_hud_update = 0
self.last_fire = 0
self.last_dialogue = 0
self.hud_health_stats = self.player.stats
self.hud_health = self.hud_health_stats['health'] / self.hud_health_stats['max health']
self.hud_stamina = self.hud_health_stats['stamina'] / self.hud_health_stats['max stamina']
self.hud_magica = self.hud_health_stats['magica'] / self.hud_health_stats['max magica']
self.hud_mobhp = 0
self.show_mobhp = False
self.last_mobhp_update = 0
self.hud_hunger = 1
self.hud_ammo1 = ''
self.hud_ammo2 = ''
self.e_down = False
self.draw_debug = False