-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.py
3050 lines (2037 loc) · 90.7 KB
/
sampler.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
# modules
import tcod as libtcod
import pygame
import math
import pickle
import gzip
import random
import datetime
# game files
import constants
import pyglet
import numpy as np
# ____ _ _
# / ___|| |_ _ __ _ _ ___| |_
# \___ \| __| '__| | | |/ __| __|
# ___) | |_| | | |_| | (__| |_
# |____/ \__|_| \__,_|\___|\__|
class struc_Tile:
'''This class functions as a struct that tracks the data for each tile
within a map.
Attributes:
block_path (arg, bool) : True if tile prevents actors from moving
through it under normal circumstances.
explored (bool): Initializes to FALSE, set to true if player
has seen it before.
'''
def __init__(self, block_path):
self.block_path = block_path
self.explored = False
class struc_Assets:
'''This class is a struct that holds all the assets used in the game. This
includes sprites, sound effects, and music.
'''
def __init__(self):
self.load_assests()
self.adjust_volume()
def load_assests(self):
## SPRITESHEETS ##
self.reptile = obj_Spritesheet("data/Reptiles.png")
self.aquatic = obj_Spritesheet("data/Aquatic.png")
self.wall = obj_Spritesheet("data/Wall.png")
self.floor = obj_Spritesheet("data/Floor.png")
self.tile = obj_Spritesheet("data/Tile.png")
self.shield = obj_Spritesheet("data/Shield.png")
self.medwep = obj_Spritesheet("data/MedWep.png")
self.scroll = obj_Spritesheet("data/Scroll.png")
self.flesh = obj_Spritesheet("data/Flesh.png")
self.misc = obj_Spritesheet("data/Light2.png")
self.doors = obj_Spritesheet("data/Door.png")
## ANIMATIONS ##
self.A_PLAYER = self.reptile.get_animation('g', 8, 16, 16, 2, (32, 32))
self.A_SNAKE_01 = self.reptile.get_animation('e', 5, 16, 16, 2, (32, 32))
self.A_SNAKE_02 = self.reptile.get_animation('k', 5, 16, 16, 2, (32, 32))
self.A_MOUSE = self.reptile.get_animation('a', 13, 16, 16, 2, (32, 32))
self.S_MAGIC_LAMP = self.misc.get_animation('b', 1, 32, 48, 4, (32, 32))
## SPRITES ##
self.S_WALL = self.wall.get_image('d', 7, 16, 16, (32, 32))[0]
self.S_WALLEXPLORED = self.wall.get_image('d', 13, 16, 16, (32, 32))[0]
self.S_FLOOR = self.floor.get_image('b', 8, 16, 16, (32, 32))[0]
self.S_FLOOREXPLORED = self.floor.get_image('b', 14, 16, 16, (32, 32))[0]
## ITEMS ##
self.S_SWORD = self.medwep.get_image('a', 1 ,16,16,(32,32))
self.S_SHIELD = self.shield.get_image('a', 1 ,16,16,(32,32))
self.S_SCROLL_01 = self.scroll.get_image('h', 2, 16,16, (32,32))
self.S_SCROLL_02 = self.scroll.get_image('d', 2, 16,16, (32,32))
self.S_SCROLL_03 = self.scroll.get_image('b', 2, 16,16, (32,32))
self.S_FLESH_01 = self.flesh.get_image('b', 4, 16,16, (32,32))
self.S_FLESH_02 = self.flesh.get_image('a', 4, 16,16, (32,32))
##SPECIAL
self.S_STAIRS_DOWN = self.tile.get_image('f',4, 16,16, (32,32))
self.S_STAIRS_UP = self.tile.get_image('e',4, 16,16, (32,32))
self.MAIN_MENU_BG = pygame.image.load("data/fawkes.jpg")
self.MAIN_MENU_BG = pygame.transform.scale(self.MAIN_MENU_BG, (constants.CAMERA_WIDTH, constants.CAMERA_HEIGHT))
self.ENDGAME = pygame.image.load("data/endgame.jpg")
self.ENDGAME = pygame.transform.scale(self.ENDGAME, (constants.CAMERA_WIDTH , constants.CAMERA_HEIGHT))
self.ENDGAME2 = pygame.image.load("data/endgame.png")
self.ENDGAME2 = pygame.transform.scale(self.ENDGAME2, (constants.CAMERA_WIDTH , constants.CAMERA_HEIGHT))
self.ENDGAME3 = pygame.image.load("data/endgame3.jpg")
self.ENDGAME3 = pygame.transform.scale(self.ENDGAME3, (constants.CAMERA_WIDTH , constants.CAMERA_HEIGHT))
self.ENDGAME4 = pygame.image.load("data/endgame4.jpg")
self.ENDGAME4 = pygame.transform.scale(self.ENDGAME4, (constants.CAMERA_WIDTH , constants.CAMERA_HEIGHT))
self.ENDGAME5 = pygame.image.load("data/endgame5.png")
self.ENDGAME5 = pygame.transform.scale(self.ENDGAME5, (constants.CAMERA_WIDTH , constants.CAMERA_HEIGHT))
self.S_PORTAL_CLOSED = self.doors.get_image('e',6, 16,16, (32,32))
self.S_PORTAL_OPEN = self.doors.get_animation('b', 6, 16,16, 2,(32,32))
self.animation_dict = {
"A_PLAYER" : self.A_PLAYER,
"A_SNAKE_01" : self.A_SNAKE_01,
"A_SNAKE_02" : self.A_SNAKE_02,
"A_MOUSE" : self.A_MOUSE,
"S_MAGIC_LAMP" : self.S_MAGIC_LAMP,
"S_SWORD" : self.S_SWORD,
"S_SHIELD" : self.S_SHIELD,
"S_SCROLL_01" : self.S_SCROLL_01,
"S_SCROLL_02" : self.S_SCROLL_02,
"S_SCROLL_03" : self.S_SCROLL_03,
"S_FLESH_01" : self.S_FLESH_01,
"S_FLESH_02" : self.S_FLESH_02,
"S_STAIRS_DOWN" : self.S_STAIRS_DOWN,
"S_STAIRS_UP" : self.S_STAIRS_UP,
"S_PORTAL_CLOSED" : self.S_PORTAL_CLOSED,
"S_PORTAL_OPEN" : self.S_PORTAL_OPEN
}
##AUDIO##
self.snd_list = []
self.music_bakcground = "data/audio/audio_game.mp3"
self.snd_hit_1 = self.add_sound("data/audio/hit_1.wav")
self.snd_hit_2 = self.add_sound("data/audio/hit_2.wav")
self.snd_hit_3 = self.add_sound("data/audio/hit_3.wav")
self.snd_hit_4 = self.add_sound("data/audio/hit_4.wav")
self.endgame = "data/audio/endgame.mp3"
self.snd_list_hit = [self.snd_hit_1, self.snd_hit_2, self.snd_hit_3, self.snd_hit_4]
def add_sound(self, file_address):
new_sound = pygame.mixer.Sound(file_address)
self.snd_list.append(new_sound)
return new_sound
def adjust_volume(self):
for sound in self.snd_list:
sound.set_volume(PREFERENCES.vol_sound)
pygame.mixer.music.set_volume(PREFERENCES.vol_music)
class struc_Preferences:
def __init__(self):
self.vol_sound = .5
self.vol_music = .5
# ___ _ _ __
# / _ \| |__ (_) ___ ___| |_ ___
# | | | | '_ \| |/ _ \/ __| __/ __|
# | |_| | |_) | | __/ (__| |_\__ \
# \___/|_.__// |\___|\___|\__|___/
# |__/
class obj_Actor:
'''The actor object represents every entity in the game.
This object is anything that can appear or act within the game. Each entity
is made up of components that control how these objects work.
Attributes:
x (arg, int): position on the x axis
y (arg, int): position on the y axis
name_object (arg, str) : name of the object type, "chair" or
"goblin" for example.
animation (arg, list): sequence of images that make up the object's
spritesheet. Created within the struc_Assets class.
animation_speed (arg, float): time in seconds it takes to loop through
the object animation.
Components:
creature: any object that has health, and generally can fight.
ai: set of instructions an obj_Actor can follow.
container: objects that can hold an inventory.
item: items are items that are able to be picked up and (usually)
usable.
'''
def __init__(self, x, y,
name_object,
animation_key,
animation_speed = .5,
depth = 0,
state = None,
# Components
creature = None,
ai = None,
container = None,
item = None,
equipment = None,
stairs = None,
exitportal = None):
self.x, self.y = x, y
self.name_object = name_object
self.animation_key = animation_key
self.animation = ASSETS.animation_dict[animation_key]
self.depth = depth
self.state = state
# divide by 1.0 to convert ints to floats
self.animation_speed = animation_speed / 1.0
self.creature = creature
if self.creature:
self.creature.owner = self
self.ai = ai
if self.ai:
self.ai.owner = self
self.container = container
if self.container:
self.container.owner = self
self.item = item
if self.item:
self.item.owner = self
self.equipment = equipment
if self.equipment:
self.equipment.owner = self
self.item = com_Item()
self.item.owner = self
self.stairs = stairs
if self.stairs:
self.stairs.owner = self
self.exitportal = exitportal
if self.exitportal:
self.exitportal.owner = self
## PRIVATE ##
# speed -> frames conversion
self._flickerspeed = self.animation_speed / len(self.animation)
# timer for deciding when to flip the image
self._flickertimer = 0.0
# currently viewed sprite
self._spriteimage = 0
@property
def display_name(self):
if self.creature:
return (self.creature.name_instance + " the " + self.name_object)
if self.item:
if self.equipment and self.equipment.equipped:
return (self.name_object + " (equipped)")
else:
return self.name_object
def draw(self):
'''Draws the object to the screen.
This function draws the object to the screen if it appears within the
PLAYER fov. It also keeps track of the timing for animations to trigger
a transition to the next sprite in the animation.
'''
is_visible = libtcod.map_is_in_fov(FOV_MAP, self.x, self.y)
if is_visible: # if visible, check to see if animation has > 1 image
if len(self.animation) == 1:
# if no, just blit the image
SURFACE_MAP.blit(self.animation[0], (self.x * constants.CELL_WIDTH,
self.y * constants.CELL_HEIGHT))
# does this object have multiple sprites?
elif len(self.animation) > 1:
# only update animation timer if we can calculate how quickly
# the game is running.
if CLOCK.get_fps() > 0.0:
self._flickertimer += 1 / CLOCK.get_fps()
# if the timer has reached the speed
if self._flickertimer >= self._flickerspeed:
self._flickertimer = 0.0 # reset the timer
# is this sprite the final item in the list?
if self._spriteimage >= len(self.animation) - 1:
self._spriteimage = 0 # reset sprite to top of list
else:
self._spriteimage += 1 # advance to next sprite
# draw the result
SURFACE_MAP.blit(self.animation[self._spriteimage],
(self.x * constants.CELL_WIDTH,
self.y * constants.CELL_HEIGHT))
def distance_to(self, other):
dx = other.x - self.x
dy = other.y - self.y
return math.sqrt(dx ** 2 + dy ** 2)
def move_towards(self, other):
dx = other.x - self.x
dy = other.y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2)
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.creature.move(dx, dy)
def move_away(self, other):
dx = other.x - self.x
dy = other.y - self.y
distance = math.sqrt(dx ** 2 + dy ** 2)
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.creature.move(-dx, -dy)
def animation_destroy(self):
self.animation = None
def animation_init(self):
self.animation = ASSETS.animation_dict[self.animation_key]
class obj_Game:
'''The obj_Game tracks game progress
This is an object that stores all the information used by the game to 'keep
track' of progress. It tracks maps, objects, and game history or record of
messages.
Attributes:
current_map (obj): whatever map is currently loaded.
current_objects (list): list of objects for the current map.
message_history (list): list of messages that have been pushed
to the player over the course of a game.'''
def __init__(self):
self.current_objects = []
self.message_history = []
self.maps_previous = []
self.maps_next = []
self.current_map, self.current_rooms = map_create()
def transition_next(self):
global FOV_CALCULATE
FOV_CALCULATE = True
for obj in self.current_objects:
obj.animation_destroy()
self.maps_previous.append((PLAYER.x, PLAYER.y, self.current_map, self.current_rooms,
self.current_objects))
if len(self.maps_next) == 0:
self.current_objects = [PLAYER]
PLAYER.animation_init()
self.current_map, self.current_rooms = map_create()
map_place_objects(self.current_rooms)
else:
(PLAYER.x, PLAYER.y, self.current_map, self.current_rooms,
self.current_objects) = self.maps_next[-1]
for obj in self.current_objects:
obj.animation_init()
map_make_fov(self.current_map)
FOV_CALCULATE = True
del self.maps_next[-1]
def transition_previous(self):
global FOV_CALCULATE
if len(self.maps_previous) !=0:
for obj in self.current_objects:
obj.animation_destroy()
self.maps_next.append((PLAYER.x, PLAYER.y, self.current_map, self.current_rooms,
self.current_objects))
(PLAYER.x, PLAYER.y, self.current_map, self.current_rooms,
self.current_objects) = self.maps_previous[-1]
for obj in self.current_objects:
obj.animation_init()
map_make_fov(self.current_map)
del self.maps_previous[-1]
FOV_CALCULATE = True
class obj_Spritesheet:
'''Class used to grab images out of a sprite sheet. As a class, it allows
you to access and subdivide portions of the sprite_sheet.
Attributes:
file_name (arg, str): String which contains the directory/filename of
the image for use as a spritesheet.
sprite_sheet (pygame.surface): The loaded spritesheet accessed through
the file_name argument.
'''
def __init__(self, file_name):
self.sprite_sheet = pygame.image.load(file_name).convert()
self.tiledict = {'a': 1, 'b': 2, 'c': 3, 'd': 4,
'e': 5, 'f': 6, 'g': 7, 'h': 8,
'i': 9, 'j': 10, 'k': 11, 'l': 12,
'm': 13, 'n': 14, 'o': 15, 'p': 16}
def get_image(self, column, row, width = constants.CELL_WIDTH,
height = constants.CELL_HEIGHT, scale = None):
'''This method returns a single sprite.
Args:
column (str): Letter which gets converted into an integer, column in
the spritesheet to be loaded.
row (int): row in the spritesheet to be loaded.
width (int): individual sprite width in pixels
height (int): individual sprite height in pixels
scale ((width, height)) = If included, scales the sprites to a new
size.
Returns:
image_list (list): This method returns a single sprite contained
within a list loaded from the spritesheet property.
'''
image_list = []
image = pygame.Surface([width, height]).convert()
image.blit(self.sprite_sheet, (0, 0),
(self.tiledict[column] * width,
row * height,
width, height))
image.set_colorkey(constants.COLOUR_BLACK)
if scale:
(new_w, new_h) = scale
image = pygame.transform.scale(image, (new_w, new_h))
image_list.append(image)
return image_list
def get_animation(self, column, row, width = constants.CELL_WIDTH,
height = constants.CELL_HEIGHT, num_sprites = 1,
scale = None):
'''This method returns a sequence of sprites.
Args:
column (str): Letter which gets converted into an integer, column in
the spritesheet to be loaded.
row (int): row in the spritesheet to be loaded.
width (int): individual sprite width in pixels
height (int): individual sprite height in pixels
num_sprites (int): number of sprites to be loaded in sequence.
scale ((width, height)) = If included, scales the sprites to a new
size.
Returns:
image_list (list): This method returns a sequence of sprites
contained within a list loaded from the spritesheet property.
'''
image_list = []
for i in range(num_sprites):
# create blank image
image = pygame.Surface([width, height]).convert()
# copy image from sheet onto blank
image.blit(self.sprite_sheet,
(0, 0),
(self.tiledict[column] * width + (width * i),
row * height, width, height))
# set transparency key to black
image.set_colorkey(constants.COLOUR_BLACK)
if scale:
(new_w, new_h) = scale
image = pygame.transform.scale(image, (new_w, new_h))
image_list.append(image)
return image_list
class obj_Room:
''' This is a rectangle that lives on the map '''
def __init__(self, coords, size):
self.x1, self.y1 = coords
self.w, self.h = size
self.x2 = self.x1 + self.w
self.y2 = self.y1 + self.h
@property
def center(self):
center_x = int((self.x1 + self.x2) / 2)
center_y = int((self.y1 + self.y2) / 2)
return (center_x, center_y)
def intersect(self, other):
# return True if other obj intersects with this one
objects_intersect = (self.x1 <= other.x2 and self.x2 >= other.x1 and
self.y1 <= other.y2 and self.y2 >= other.y1)
return objects_intersect
class obj_Camera:
def __init__(self):
self.width = constants.CAMERA_WIDTH
self.height = constants.CAMERA_HEIGHT
self.x, self.y = (0,0)
@property
def rectangle(self):
pos_rect = pygame.Rect((0,0), (constants.CAMERA_WIDTH, constants.CAMERA_HEIGHT))
pos_rect.center = (self.x, self.y)
return pos_rect
@property
def map_address(self):
map_x = int(self.x / constants.CELL_WIDTH)
map_y = int(self.y / constants.CELL_HEIGHT)
return (map_x,map_y)
def update(self):
target_x = PLAYER.x * constants.CELL_WIDTH + int((constants.CELL_WIDTH/2))
target_y = PLAYER.y * constants.CELL_HEIGHT+ int((constants.CELL_HEIGHT/2))
distance_x , distance_y = self.map_dist((target_x, target_y))
self.x += int(distance_x * .2)
self.y += int(distance_y * .2)
def win_to_map(self, coords):
tar_x, tar_y = coords
# convert window coords to distance from cam
cam_d_x, cam_d_y = self.cam_dist((tar_x, tar_y))
# dist from cam to amap coord
map_p_x = self.x + cam_d_x
map_p_y = self.y + cam_d_y
return (map_p_x, map_p_y)
def map_dist(self, coords):
new_x, new_y = coords
dist_x = new_x - self.x
dist_y = new_y - self.y
return (dist_x, dist_y)
def cam_dist(self, coords):
win_x, win_y = coords
dist_x = win_x - int(self.width/2)
dist_y = win_y - int(self.height/2)
return (dist_x, dist_y)
# ____ ___ __ __ ____ ___ _ _ _____ _ _ _____ ____
# / ___/ _ \| \/ | _ \ / _ \| \ | | ____| \ | |_ _/ ___|
# | | | | | | |\/| | |_) | | | | \| | _| | \| | | | \___ \
# | |__| |_| | | | | __/| |_| | |\ | |___| |\ | | | ___) |
# \____\___/|_| |_|_| \___/|_| \_|_____|_| \_| |_| |____/
class com_Creature:
'''Creatures are actors that have health and can fight.
Attributes:
name_instance (arg, str): name of instance. "Bob" for example.
max_hp (arg, int): max health of the creature.
death_function (arg, function): function to be executed when hp reaches 0.
current_hp (int): current health of the creature.
'''
def __init__(self, name_instance, base_atk = 2, base_def = 0, max_hp = 10,
death_function = None):
self.name_instance = name_instance
self.base_atk = base_atk
self.base_def = base_def
self.max_hp = max_hp
self.death_function = death_function
self.current_hp = max_hp
def move(self, dx, dy):
'''Moves the object
Args:
dx (int): distance to move actor along x axis
dy (int): distance to move actor along y axis
'''
tile_is_wall = (GAME.current_map[self.owner.x + dx]
[self.owner.y + dy].block_path == True)
target = map_check_for_creature(self.owner.x + dx,
self.owner.y + dy,
self.owner)
if target:
self.attack(target)
if not tile_is_wall and target is None:
self.owner.x += dx
self.owner.y += dy
def attack(self, target):
'''Creature makes an attack against another obj_Actor
Args:
target (obj_Actor): target to be attacked, must have creature
component.
damage (int): amount of damage to be done to target
'''
damage_dealt = self.power - target.creature.defense
if damage_dealt>0 and self.owner is PLAYER:
game_message(self.name_instance + " attacks " +
target.creature.name_instance + " for " + str(damage_dealt) +
" damage!", constants.COLOUR_RED)
target.creature.take_damage(damage_dealt)
if damage_dealt>0 and self.owner is PLAYER:
pygame.mixer.Sound.play(RANDOM_ENGINE.choice(ASSETS.snd_list_hit))
def take_damage(self, damage):
"""Applies damage received to self.health
This function applies damage to the obj_Actor with the creature
component. If the current health level falls below 1, executes the
death_function.
Args:
damage (int): amount of damage to be applied to self.
"""
# subtract health
self.current_hp -= damage
# print message
game_message(self.name_instance +
"'s health is " +
str(self.current_hp) +
"/" +
str(self.max_hp),
constants.COLOUR_RED)
# if health now equals < 1, execute death function
if self.current_hp <= 0:
if self.death_function is not None:
self.death_function(self.owner)
def heal(self, value):
self.current_hp += value
if self.current_hp > self.max_hp:
self.current_hp = self.max_hp
@property
def power(self):
total_power = self.base_atk
if self.owner.container:
object_bonuses = [obj.equipment.attack_bonus
for obj in self.owner.container.equipped_items]
for bonus in object_bonuses:
if bonus:
total_power += bonus
return total_power
@property
def defense(self):
total_defense = self.base_def
if self.owner.container:
object_bonuses = [obj.equipment.defense_bonus
for obj in self.owner.container.equipped_items]
for bonus in object_bonuses:
if bonus:
total_defense += bonus
return total_defense
class com_Container:
def __init__(self, volume = 10.0, inventory = None):
if inventory:
self.inventory = inventory
else:
self.inventory = []
self.max_volume = volume
## TODO Get Names of everything in inventory
## TODO Get volume within container
@property
def volume(self):
return 0.0
@property
def equipped_items(self):
list_of_equipped_items = [obj for obj in self.inventory
if obj.equipment and obj.equipment.equipped]
return list_of_equipped_items
## TODO Get weight of everything in inventory
class com_Item:
'''Items are components that can be picked up and used.
Attributes:
weight (arg, float): how much does the item weigh
volume (arg, float): how much space does the item take up
'''
def __init__(self, weight = 0.0, volume = 0.0, use_function = None,
value = None):
self.weight = weight
self.volume = volume
self.value = value
self.use_function = use_function
def pick_up(self, actor):
'''The item is picked up and placed into an object's inventory.
When called, this method seeks to place the item into an object's
inventory if there is room. It then removes the item from a Game's
current_objects list.
Args:
actor (obj_Actor): the object that is picking up the item.
'''
if actor.container: # first, checks for container component
# does the container have room for this object?
if actor.container.volume + self.volume > actor.container.max_volume:
# if no, print error message
game_message("Not enough room to pick up")
else:
# otherwise, pick the item up, remove from GAME.current_objects
# message the player
game_message('Picking up')
# add to actor inventory
actor.container.inventory.append(self.owner)
self.owner.animation_destroy()
# remove from game active list
GAME.current_objects.remove(self.owner)
# tell item what container holds it
self.current_container = actor.container
def drop(self, new_x, new_y):
'''Drops the item onto the ground.
This method removes the item from the actor.container inventory and
places it into the GAME.current_objects list. Drops the item at the
location defined in the args.
Args:
new_x (int): x coord on the map to drop item
new_y (int): y coord on the map to drop item
'''
# add this item to tracked objects
GAME.current_objects.append(self.owner)
self.owner.animation_init()
# remove from the inventory of whatever actor holds it
self.current_container.inventory.remove(self.owner)
# set item location to as defined in the args
self.owner.x = new_x
self.owner.y = new_y
# confirm successful placement with game message
game_message("Item Dropped!")
def use(self):
'''Use the item by producing an effect and removing it.
'''
if self.owner.equipment:
self.owner.equipment.toggle_equip()
return
if self.use_function:
result = self.use_function(self.current_container.owner, self.value)
if result is not None:
print("use_function failed")
else:
self.current_container.inventory.remove(self.owner)
class com_Equipment:
def __init__(self, attack_bonus = None, defense_bonus = None, slot = None):
self.attack_bonus = attack_bonus
self.defense_bonus = defense_bonus
self.slot = slot
self.equipped = False
def toggle_equip(self):
if self.equipped:
self.unequip()
else:
self.equip()
def equip(self):
#check for equipment in slot
all_equipped_items = self.owner.item.current_container.equipped_items
for item in all_equipped_items:
if item.equipment.slot and (item.equipment.slot == self.slot):
game_message("equipment slot is occupied", constants.COLOUR_RED)
return
self.equipped = True
game_message("item equipped")
def unequip(self):
#toggle self.equipped
self.equipped = False
game_message("item unequipped")
class com_Stairs:
def __init__(self, downwards = True):
self.downwards = downwards
def use(self):
if self.downwards:
GAME.transition_next()
else:
GAME.transition_previous()
class com_ExitPortal:
def __init__(self):
self.OPENANIMATION = "S_PORTAL_OPEN"
self.CLOSEANIMATION = "S_PORTAL_CLOSED"
self.open = False
def update(self):
found_lamp = False
# check conditions
portal_open = self.owner.state == "OPEN"
for obj in PLAYER.container.inventory:
if obj.name_object == "THE LAMP" :
found_lamp = True
if found_lamp and not portal_open:
self.owner.state = "OPEN"
self.owner.animation_key = self.OPENANIMATION
self.owner.animation_init()
if not found_lamp and portal_open:
self.owner.state = "CLOSED"
self.owner.animation_key = self.CLOSEANIMATION
self.owner.animation_init()
def use(self):