-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComp-Sci-Project-GitHub.py
1736 lines (1474 loc) · 74.1 KB
/
Comp-Sci-Project-GitHub.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
# import modules/ scripts
import pygame
import random
import math
import grid_class
# defining a few colours, using their RGB value
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
SEA_BLUE = (0, 191, 255) # colour for sea
# initialise window height and width
WIDTH = 700
HEIGHT = 500
# initialising the pygame engine
pygame.init()
# setting the borderless window
size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size) # creates display window
pygame.display.set_caption("Pyrate!") # sets the window title
# define player class
class Player(pygame.sprite.Sprite):
def __init__(self, start_x, start_y):
super().__init__()
self.change_x = 0 # player speed left and right, starts at 0
self.change_y = 0 # player speed up and down, starts at 0
self.speed = 3 # player speed variable
self.size = 30 # player rectangle longest length size
self.height = 30
self.width = 18
self.ship_size = 30 # integer for size of ship rectangle
# set up player pirate sprites
self.down_image1 = pygame.image.load("Walking_player_spr.png").convert()
self.down_image1.set_colorkey(WHITE)
self.down_image2 = pygame.image.load("Walking_player_spr_2.png").convert()
self.down_image2.set_colorkey(WHITE)
self.up_image1 = pygame.image.load("Walking_back_player_spr.png").convert()
self.up_image1.set_colorkey(WHITE)
self.up_image2 = pygame.image.load("Walking_back_player_spr_2.png").convert()
self.up_image2.set_colorkey(WHITE)
self.left_image1 = pygame.image.load("Walking_left_player_spr.png").convert()
self.left_image1.set_colorkey(WHITE)
self.left_image2 = pygame.image.load("Walking_left_player_spr_2.png").convert()
self.left_image2.set_colorkey(WHITE)
self.right_image1 = pygame.image.load("Walking_right_player_spr.png").convert()
self.right_image1.set_colorkey(WHITE)
self.right_image2 = pygame.image.load("Walking_right_player_spr_2.png").convert()
self.right_image2.set_colorkey(WHITE)
self.curr_image = self.down_image1
# set up player ship sprites
self.ship_up_spr = pygame.image.load("Ship_up_spr.png").convert()
self.ship_up_spr.set_colorkey(WHITE)
self.ship_down_spr = pygame.image.load("Ship_down_spr.png").convert()
self.ship_down_spr.set_colorkey(WHITE)
self.ship_left_spr = pygame.image.load("Ship_left_spr.png").convert()
self.ship_left_spr.set_colorkey(WHITE)
self.ship_right_spr = pygame.image.load("Ship_right_spr.png").convert()
self.ship_right_spr.set_colorkey(WHITE)
self.image_timer = 0
self.rect = self.curr_image.get_rect()
self.ship_rect = self.ship_up_spr.get_rect()
self.health_image = pygame.image.load("Health.png").convert()
self.health_image.set_colorkey(WHITE)
self.health_rect = self.health_image.get_rect()
self.rect.x = start_x # player x position
self.rect.y = start_y # player y position
self.last_x = 0 # most recent x direction of player
self.last_y = -1 # most recent y direction of player
self.health = 5 # integer for player health
self.max_health = 5 # integer for maximum player health
self.invulnerable = False # boolean for if player is invulnerable or not
self.invulnerable_timer = pygame.time.get_ticks() # create reference timer for invulnerability period
self.health_flicker_timer = pygame.time.get_ticks() # create reference timer for health flicker
self.inventory = [] # empty list for use as inventory
self.draw_health = True # boolean to check if health should be drawn
self.banner = pygame.image.load("Banner.png").convert() # sprite for player message banner
self.banner.set_colorkey(WHITE)
self.banner_rect = self.banner.get_rect()
self.pause_timer = 0 # timer for keeping the other timers ticking during the pause period
self.on_island = False # boolean for tracking if the player landed on an island
# method for updating player movement on the map screem
def move_map(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
# method for updating player movement on islands and in dungeons
def move_close(self, location_rect):
self.rect.clamp_ip(location_rect) # keep player on island
self.rect.x += self.change_x
self.rect.y += self.change_y
# method to draw player pirate sprites to screen
def draw(self, screen):
# code to ensure correct image is drawn
if pygame.time.get_ticks() - self.image_timer > 500:
if self.curr_image == self.up_image1:
self.curr_image = self.up_image2
elif self.curr_image == self.up_image2:
self.curr_image = self.up_image1
elif self.curr_image == self.left_image1:
self.curr_image = self.left_image2
elif self.curr_image == self.left_image2:
self.curr_image = self.left_image1
elif self.curr_image == self.right_image1:
self.curr_image = self.right_image2
elif self.curr_image == self.right_image2:
self.curr_image = self.right_image1
elif self.curr_image == self.down_image1:
self.curr_image = self.down_image2
elif self.curr_image == self.down_image2:
self.curr_image = self.down_image1
self.image_timer = pygame.time.get_ticks() # reset image timer
screen.blit(self.curr_image, [self.rect.x, self.rect.y]) # blit current player image to screen
# method to draw player ship sprite to screen
def draw_map(self, screen):
screen.blit(self.curr_image, [self.rect.x, self.rect.y])
# method to take damage from player health
def take_damage(self, damage):
self.health -= damage # take away enemy damage from player health
# method to output a given message to the screen on a banner sprite
def message(self, text):
output_text = font.render(text, True, BLACK)
screen.blit(self.banner, [0, 0])
screen.blit(output_text, [20, 10])
# method to stop player movement
def halt_speed(self):
player_obj.change_y = 0
player_obj.change_x = 0
# method to draw player health bar
def draw_player_health(self, screen):
if self.draw_health: # if player has health
for hp in range(self.health):
screen.blit(self.health_image, [WIDTH - 40, (20 + hp * 35)])
# method to make player's health bar flicker
def health_invulnerable_flicker(self, screen):
# code to make health timer flicker
if self.invulnerable:
if (pygame.time.get_ticks() - self.health_flicker_timer) > 300:
if self.draw_health:
self.draw_health = False
self.health_flicker_timer = pygame.time.get_ticks()
else:
self.draw_health = True
self.health_flicker_timer = pygame.time.get_ticks()
else:
self.draw_health = True
# initialise player object
player_obj = Player(350, 250)
# initialise island class
class Island(pygame.sprite.Sprite):
def __init__(self, height, width, position_x, position_y, type):
super().__init__()
self.height = height
self.width = width
self.width_map = width / 4
self.height_map = height / 4
self.position_x_close = (WIDTH / 2) - (self.width / 2) # x position of island sprite on overview screen
self.position_y_close = (HEIGHT / 2) - (self.height / 2) # y position of island sprite on overview screen
if type == "centre": # if centre island object, give specific centre island appearance
self.image = pygame.image.load("CentreIslandClose.png").convert()
self.image.set_colorkey(WHITE)
self.image_map = pygame.image.load("CentreIslandMap.png").convert()
self.image_map.set_colorkey(WHITE)
self.door = DungeonDoor((WIDTH / 2) - 15, self.position_y_close + 40)
else: # if not centre island object, give random island appearance
self.appearance = random.choice(["Sand", "Grass", "Rock"])
if self.appearance == "Sand": # set sandy island sprites
self.image = pygame.image.load("SandIslandClose.png").convert()
self.image.set_colorkey(WHITE)
self.image_map = pygame.image.load("SandIslandMap.png").convert()
self.image_map.set_colorkey(WHITE)
elif self.appearance == "Grass": # set grassy island sprites
self.image = pygame.image.load("Grass_island_spr.png").convert()
self.image.set_colorkey(WHITE)
self.image_map = pygame.image.load("Grass_map_spr.png").convert()
self.image_map.set_colorkey(WHITE)
elif self.appearance == "Rock": # set rocky island sprites
self.image = pygame.image.load("Rock_island_spr.png").convert()
self.image.set_colorkey(WHITE)
self.image_map = pygame.image.load("Rock_island_map_spr.png").convert()
self.image_map.set_colorkey(WHITE)
# rect attributes for island sprite while on map screen
self.rect = self.image_map.get_rect()
self.rect.x = position_x
self.rect.y = position_y
# rect attributes for island sprite while on overview screen
self.rect_close = self.image.get_rect()
self.rect_close.x = self.position_x_close
self.rect_close.y = self.position_y_close
self.boundary_rect = [self.position_x_close + 5, self.position_y_close + 5, self.height - 10,
self.width - 10] # rectangle for keeping player in island
self.overview = False # boolean to tell if island overview screen should be displayed or not
self.off = False # boolean to check if player has left island
self.chest_open = False # boolean to check if island chest is open
self.island_location = False # boolean to determine if island spawn location is all good (no collisions)
self.breakables = pygame.sprite.Group() # create list of breakable items per location
self.enemies = pygame.sprite.Group() # create list of enemies per location
# create graph with grid_class script
self.graph = grid_class.Grid(self.height, self.width)
self.chest = TreasureChest(40, self.position_x_close + (self.width / 2) - 20, self.position_y_close + 40 - 20,
"Example Treasure")
chests.add(self.chest) # add chest to chest sprite group
def draw_close(self, screen): # island drawing code for when game is on island
screen.blit(self.image, [self.rect_close.x, self.rect_close.y])
def draw_map(self, screen): # island drawing code for when game is on map screen
screen.blit(self.image_map, [self.rect.x, self.rect.y])
def place_in_graph(self, item, item_pos): # method to place item in centre of graph
self.graph.place_in_position(item_pos, item, self)
# method for what to do if player has collided with the island on the map screen
def island_collision(self):
# changes location overview
map.overview = False
self.overview = True
player_obj.on_island = True
# code to determine player position
player_obj.rect.y = self.position_y_close + (self.height * 0.8)
player_obj.rect.x = self.position_x_close + (self.width / 2) - (player_obj.size / 2)
player_obj.halt_speed() # stop player position
player_obj.invulnerable_timer = pygame.time.get_ticks() # make player invulnerable for length of timer
# code to set player sprite to pirate
player_obj.curr_image = player_obj.up_image1
# method to check if player is in a position to leave the island or not
def check_leave_island(self):
# code to check if island is being left
if player_obj.rect.y + player_obj.size >= self.position_y_close + self.height - player_obj.speed:
# change game overview screen
self.overview = False
map.overview = True
self.off = True
def leave_island(self):
# code to determine player position once left island
player_obj.rect.y = self.rect.y + self.height_map + 5
player_obj.rect.x = self.rect.x + (self.width_map / 2) - (player_obj.size / 2)
player_obj.halt_speed()
self.off = False
self.chest.treasure_message_display = False # stop displaying treasure message
bullets.empty() # remove all bullets from group
# code to set player ship sprite
player_obj.curr_image = player_obj.ship_down_spr
# create location overview method
def location_loop(self):
# access global variables
global done
# ensure player speed does not carry over
player_obj.halt_speed()
# reset map ocean timer
map.ocean_switch_timer = 0
while self.overview:
# code for key presses + movement
location_movement(self, self.enemies)
# fill screen with background colour
screen.fill(SEA_BLUE)
# draw ocean to screen
map.draw_ocean_island()
# draw the island up close
self.draw_close(screen)
# what happens when player spawns on island
if player_obj.on_island:
# land_on_island(self)
enemy_delay(self.enemies)
# code to remove enemies from island_obj.enemies list, draw them to screen, and have them attack
enemy_draw_move(self, self.enemies, self.rect_close)
# code to do things if not on centre island
if self != centre_island_obj:
# code to spawn chest on island
if not self.enemies:
self.chest.draw(screen)
# code to handle collision and treasure message
self.chest.check_collision(self.enemies)
# make player leave if exit bottom of island
self.check_leave_island()
# have player attack (on island)
player_obj.move_close(self.boundary_rect)
# code to check if enemies are dead or not
enemy_health_check()
# code to check if player can be hit
check_player_invulnerable()
# code to check collision between player and enemy
check_player_enemy_collision(self.enemies)
# code to check collision between bullet and sword
if sword_obj.will_draw:
# check sword-bullet collision
check_sword_bullet_collision()
# code to check collision between player and bullet
check_player_bullet_collision()
# check if breakable object is broken
sword_obj.check_break(self, self.breakables)
# check if on centre island
if self == centre_island_obj:
# draw the door on the island
self.door.draw(screen)
# check collision with door on island
if pygame.sprite.collide_rect(player_obj, self.door):
check_player_door_collision(self, dungeon_entrance_obj, dungeon_entrance_obj.enemies)
# display tutorial messages
for tutorial in tutorials:
tutorial.show_tutorial(self)
# remove tutorial messages
if centre_pot_obj.broken:
centre_sword_tutorial.end_tutorial()
self.check_leave_island()
if player_obj.change_x != 0 or player_obj.change_y != 0:
movement_tutorial.end_tutorial()
if player_obj.rect.y + player_obj.size >= self.position_y_close + self.height - (player_obj.speed * 2):
pause_tutorial.end_tutorial()
# draw breakable objects to screen
for breakable in self.breakables:
breakable.draw(screen)
# procedure to check for player death and draw to screen
player_draw_or_die()
# code to check bullet shooting
if bullets:
draw_bullet()
# draw sword to screen
draw_sword(self.enemies)
# update screen and framerate
screen_update()
# create dungeon door class, for use
class DungeonDoor(pygame.sprite.Sprite):
def __init__(self, position_x, position_y):
super().__init__()
self.height = 40 # attribute for door height
self.width = 30 # attribute for door width
self.image = pygame.image.load("Door.png").convert() # load sprite from file
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect() # generate rect from sprite
self.rect.x = position_x # set door x position
self.rect.y = position_y # set door y position
self.can_open = False # boolean to check if door can be opened
# method to draw dungeon door to screen
def draw(self, screen):
screen.blit(self.image, [self.rect.x, self.rect.y])
# method for what to do when a dungeon door is opened
def open_door(self, curr_location, destination, destination_enemies_list):
# change game overview
curr_location.overview = False
destination.overview = True
destination.room_entry = True
# method to check if a dungeon door should be opened or not
def check_open(self, curr_location):
if curr_location == centre_island_obj: # if player on centre island
# only open door if player has collected all treasures and the centre pot is gone
if len(player_obj.inventory) == len(islands) - 1 and centre_pot_obj.broken:
self.can_open = True
elif centre_pot_obj.broken:
player_obj.message("Leave the island to the south. Come back with the treasures.")
elif curr_location == dungeon_entrance_obj: # if player in dungeon entrance
if not dungeon_entrance_obj.enemies:
self.can_open = True
else: # if player in any other location
self.can_open = True
# create dungeon class, for use
class Dungeon(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.overview = False # boolean for if dungeon level is on screen
self.height = 400 # dungeon floor sprite height
self.width = 600 # dungeon floor sprite width
# load dungeon sprite from file
self.image = pygame.image.load("Dungeon_floor_spr.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.wall_image = pygame.image.load("Dungeon_wall_spr.png").convert()
self.wall_image.set_colorkey(WHITE)
# set dungeon floor sprite position
self.rect.x = 50
self.rect.y = 50
self.chest_open = False # boolean for if chest is open
self.breakables = pygame.sprite.Group() # create list of breakable items per location
self.enemies = pygame.sprite.Group() # create list of enemies per location
# create graph with grid_class script
self.graph = grid_class.Grid(self.height, self.width)
self.door = DungeonDoor(self.rect.x + ((self.width / 2) - 15), self.rect.y - 40)
self.room_entry = True # boolean for if the player has entered the room
# method to draw dungeon floor and wall sprites to screen
def draw(self, screen):
screen.blit(self.image, [self.rect.x, self.rect.y])
# method to place item in centre of graph
def place_in_graph(self, item, item_pos):
self.graph.place_in_position(item_pos, item, self)
# method for overview loop
def location_loop(self):
# access global variables
global treasure_message_timer
global done
# ensure player speed does not carry over
player_obj.halt_speed()
# while in dungeon entrance
while self.overview:
# code for key presses + movement
location_movement(self, self.enemies)
# display background image
screen.blit(self.wall_image, [0, 0])
# draw dungeon entrance floor
dungeon_entrance_obj.draw(screen)
# code to check if player has entered room from bottom
if self.room_entry:
enter_room_lower(self, self.door)
enemy_delay(self.enemies)
# code to draw enemies to screen
enemy_draw_move(self, self.enemies, self.rect)
# have player move (within dungeon bounds)
player_obj.move_close(self)
# code to check if enemies are dead or not
enemy_health_check()
# code to check if player can be hit
check_player_invulnerable()
# code to check collision between player and enemy
check_player_enemy_collision(self.enemies)
# code to check collision between bullet and sword
if sword_obj.will_draw:
# check sword-bullet collision
check_sword_bullet_collision()
# code to check collision between player and bullet
check_player_bullet_collision()
# check if door can spawn
if not self.enemies:
if self == dungeon_entrance_obj:
self.door.draw(screen)
# check player-door collision
if pygame.sprite.collide_rect(player_obj, self.door):
check_player_door_collision(self, dungeon_second_room_obj, dungeon_second_room_obj.enemies)
elif self == dungeon_second_room_obj:
end_chest.draw(screen)
end_chest.check_end_game(self)
# display player movements to screen
player_draw_or_die()
# code to check bullet shooting
if bullets:
draw_bullet()
# draw sword to screen
draw_sword(self.enemies)
# update screen and framerate
screen_update()
# create dungeon objects
dungeon_entrance_obj = Dungeon()
dungeon_second_room_obj = Dungeon()
# create list of dungeon objects
dungeons = pygame.sprite.Group()
dungeons.add(dungeon_entrance_obj, dungeon_second_room_obj)
# create map class, for use
class Map:
def __init__(self):
self.overview = False # boolean for if map level is on screen
# load ocean images from file
self.ocean_image_1 = pygame.image.load("Ocean_1.png").convert()
self.ocean_image_1.set_colorkey(BLACK)
self.ocean_image_2 = pygame.image.load("Ocean_2.png").convert()
self.ocean_image_2.set_colorkey(BLACK)
self.calm_ocean = pygame.image.load("Base_ocean.png").convert()
self.calm_ocean.set_colorkey(BLACK)
self.island_ocean_1 = pygame.image.load("Island_ocean.png").convert()
self.island_ocean_1.set_colorkey(BLACK)
self.island_ocean_2 = pygame.image.load("Island_ocean_2.png").convert()
self.island_ocean_2.set_colorkey(BLACK)
self.ocean_switch = False # boolean to check if ocean image should swap or not
self.curr_image = self.ocean_image_1
self.curr_image_island = self.island_ocean_1
self.ocean_switch_timer = 0 # set ocean image timer to 0
# overview loop method for map screen
def world_map(self):
# ensure player speed does not carry over
player_obj.halt_speed()
# reset ocean display timer
map.ocean_switch_timer = 0
# map loop
while self.overview:
# procedure for key presses and movement
map_movement()
# fill screen with background colour
screen.fill(SEA_BLUE)
# draw map to screen
map.draw_ocean_map()
# draw all islands on map
for island in islands:
island.draw_map(screen)
if island.off:
# leave_island(island)
island.leave_island()
if pygame.sprite.collide_rect(player_obj, island):
island.island_collision()
# have player move (map overview) and draw it to screen
player_obj.move_map()
player_obj.draw(screen)
# if player is in map/sailing screen and they go off the edge, make them reappear on the opposite one
self.wraparound()
# display output and framerate
screen_update()
# procedure to ensure map wrap around to keep player on screen
def wraparound(self):
# if player is in map/sailing screen and they go off the edge, make them reappear on the opposite one
if player_obj.rect.x + player_obj.size < 0:
player_obj.rect.x = WIDTH + (player_obj.speed // 2)
elif player_obj.rect.x > WIDTH:
player_obj.rect.x = (0 - player_obj.size) - (player_obj.speed // 2)
if player_obj.rect.y + player_obj.size < 0:
player_obj.rect.y = HEIGHT + (player_obj.speed // 2)
elif player_obj.rect.y > HEIGHT:
player_obj.rect.y = (0 - player_obj.size) - (player_obj.speed // 2)
# method to draw ocean background on map screen
def draw_ocean_map(self):
if pygame.time.get_ticks() - self.ocean_switch_timer >= 1000:
if self.ocean_switch:
self.curr_image = self.ocean_image_1
self.ocean_switch_timer = pygame.time.get_ticks()
self.ocean_switch = False
else:
self.curr_image = self.ocean_image_2
self.ocean_switch_timer = pygame.time.get_ticks()
self.ocean_switch = True
screen.blit(self.curr_image, [0, 0]) # blit final ocean image to screen
# method to draw ocean background on island screen
def draw_ocean_island(self):
if pygame.time.get_ticks() - self.ocean_switch_timer >= 500:
if self.ocean_switch:
self.curr_image_island = self.island_ocean_1
self.ocean_switch_timer = pygame.time.get_ticks()
self.ocean_switch = False
else:
self.curr_image_island = self.island_ocean_2
self.ocean_switch_timer = pygame.time.get_ticks()
self.ocean_switch = True
screen.blit(self.curr_image_island, [0, 0])
# create map object
map = Map()
# initialise moving enemy class, intended as parent class for future enemies
class Ghost(pygame.sprite.Sprite):
def __init__(self, start_x, start_y):
super().__init__()
self.change_x = 0 # initial x speed
self.change_y = 0 # initial y speed
self.height = 36 # set size
self.width = 28
# load sprites from file
self.image1 = pygame.image.load("Ghost_pirate.png").convert()
self.image1.set_colorkey(WHITE)
self.image2 = pygame.image.load("Ghost_pirate_2.png").convert()
self.image2.set_colorkey(WHITE)
self.curr_image = self.image1
self.image_timer = 0
self.rect = self.curr_image.get_rect()
self.rect.x = start_x # enemy x position
self.rect.y = start_y # enemy y position
self.health = 2 # integer for health value, each hit does damage of 2
self.damage = 1 # boolean for damage enemy does to player health
self.invulnerable = False # boolean for if enemy can take damage or not
self.invulnerable_timer = pygame.time.get_ticks() # sets the current time as reference for invincibility
self.attack_timer = pygame.time.get_ticks() # timer for attack calculation
self.found_location = False # boolean to check if position is correct
self.move = True # boolean to signify moving enemy
self.speed = 1.5 # integer for enemy speed
self.type = "default" # string for enemy type
def move_attack(self, location_rect):
# code to attack enemy towards player aggressively
if self.type == "default":
if pygame.time.get_ticks() - self.attack_timer > 1000:
if abs(player_obj.rect.x - self.rect.x) < abs(player_obj.rect.y - self.rect.y):
if player_obj.rect.y > self.rect.y:
self.change_x = 0
self.change_y = self.speed
self.attack_timer = pygame.time.get_ticks() - random.randrange(5000)
else:
self.change_x = 0
self.change_y = -self.speed
self.attack_timer = pygame.time.get_ticks() - random.randrange(5000)
elif abs(player_obj.rect.x - self.rect.x) > abs(player_obj.rect.y - self.rect.y):
if player_obj.rect.x > self.rect.x:
self.change_x = self.speed
self.change_y = 0
self.attack_timer = pygame.time.get_ticks() - random.randrange(5000)
else:
self.change_x = -self.speed
self.change_y = 0
self.attack_timer = pygame.time.get_ticks() - random.randrange(5000)
elif self.type == "charge":
# code to make enemy charge at player
if pygame.time.get_ticks() - self.attack_timer > 2000:
x_direction = (player_obj.rect.x - self.rect.x) / math.sqrt((player_obj.rect.x - self.rect.x) ** 2 +
(player_obj.rect.y - self.rect.y) ** 2)
y_direction = (player_obj.rect.y - self.rect.y) / math.sqrt((player_obj.rect.x - self.rect.x) ** 2 +
(player_obj.rect.y - self.rect.y) ** 2)
self.change_x = x_direction * 3
self.change_y = y_direction * 3
self.attack_timer = pygame.time.get_ticks() - random.randrange(1000)
elif self.type == "follow":
# code to make enemy go in reverse of last player direction
self.change_x = player_obj.last_x * -1
self.change_y = player_obj.last_y * -1
# apply position changes and keep enemy on island
self.rect.clamp_ip(location_rect)
self.rect.x += self.change_x
self.rect.y += self.change_y
# method to draw enemy sprites to screen
def draw(self, screen):
# code to ensure correct image is drawn
if pygame.time.get_ticks() - self.image_timer > 750:
if self.curr_image == self.image1:
self.curr_image = self.image2
else:
self.curr_image = self.image1
self.image_timer = pygame.time.get_ticks()
screen.blit(self.curr_image, [self.rect.x, self.rect.y]) # draw final sprite to screen
# class for stationary, shooting enemies
class Pirate(pygame.sprite.Sprite):
def __init__(self, start_x, start_y):
super().__init__()
# set sprite size
self.height = 38
self.width = 24
# load sprites from file
self.image1 = pygame.image.load("Pirate_spr.png").convert()
self.image1.set_colorkey(WHITE)
self.image2 = pygame.image.load("Pirate_spr_2.png").convert()
self.image2.set_colorkey(WHITE)
self.curr_image = self.image1
self.image_timer = 0
self.rect = self.curr_image.get_rect()
self.rect.x = start_x # enemy x position
self.rect.y = start_y # enemy y position
self.health = 2 # integer for health value, each hit does damage of 2
self.invulnerable = False # boolean for if enemy can be hit
self.invulnerable_timer = pygame.time.get_ticks() # sets the current time as reference for invincibility
self.attack_timer = pygame.time.get_ticks() # sets the current time as reference for attacking
self.can_attack = False # boolean for if the enemy can attack
self.damage = 0 # integer for how much damage dealt to player health
self.found_location = False # boolean to check if position is correct
self.move = False # boolean to signify that this is not a moving enemy
# method for projectile attacks
def attack(self):
# access global variables
global screen
# check enemy can attack
if pygame.time.get_ticks() - self.attack_timer >= 2000:
self.can_attack = True
if self.can_attack and not pygame.sprite.collide_circle(self, player_obj):
# code to determine bullet direction and speed
bullet_x_direction = (player_obj.rect.x - self.rect.x) / math.sqrt((player_obj.rect.x - self.rect.x) ** 2 +
(player_obj.rect.y - self.rect.y) ** 2)
bullet_y_direction = (player_obj.rect.y - self.rect.y) / math.sqrt((player_obj.rect.x - self.rect.x) ** 2 +
(player_obj.rect.y - self.rect.y) ** 2)
bullet_x_speed = bullet_x_direction * 5
bullet_y_speed = bullet_y_direction * 5
# create bullet object
bullet = Bullet(15, self.rect.x, self.rect.y, bullet_x_speed, bullet_y_speed)
bullets.add(bullet)
bullet.move()
bullet.draw(screen)
self.can_attack = False
self.attack_timer = pygame.time.get_ticks()
# method to draw sprite to screen
def draw(self, screen):
# code to ensure correct image is drawn
if pygame.time.get_ticks() - self.image_timer > 750:
if self.curr_image == self.image1:
self.curr_image = self.image2
else:
self.curr_image = self.image1
self.image_timer = pygame.time.get_ticks()
screen.blit(self.curr_image, [self.rect.x, self.rect.y]) # output final sprite to screen
# initialise bullet class, for enemy attacks
class Bullet(pygame.sprite.Sprite):
def __init__(self, size, start_x, start_y, x_speed, y_speed):
super().__init__()
self.size = size
self.image = pygame.image.load("Bullet.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.x = start_x
self.rect.y = start_y
self.x_speed = x_speed
self.y_speed = y_speed
self.damage = 1 # integer for damage dealt to player health
# method to draw bullet sprite to screen
def draw(self, screen):
screen.blit(self.image, [self.rect.x, self.rect.y])
# method to move bullet sprite
def move(self):
self.rect.x += self.x_speed
self.rect.y += self.y_speed
# create class of breakable objects
class BreakObject(pygame.sprite.Sprite):
def __init__(self, x_position, y_position):
super().__init__()
self.size = 40 # set sprite size
# load image from file
self.image = pygame.image.load("Break_object.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.x = x_position
self.rect.y = y_position
self.broken = False # boolean for if object is broken or not
# method to draw breakable object to screen
def draw(self, screen):
if not self.broken: # if object is not broken
screen.blit(self.image, [self.rect.x, self.rect.y])
# initialise sword class, for attacking
class Sword(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# set size
self.width = 22
self.height = 22
# load pygame image sprite
self.image = pygame.image.load("Sword_spr.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.x = player_obj.rect.x + player_obj.size
self.rect.y = player_obj.rect.y + player_obj.size
# create variables for sword image/sprite at different directions
self.image_up = self.image
self.image_down = pygame.transform.rotate(self.image, 180)
self.image_left = pygame.transform.rotate(self.image, 90)
self.image_right = pygame.transform.rotate(self.image, 270)
self.curr_image = self.image_up
self.delay = pygame.time.get_ticks() # amount of milliseconds before sword sprite disappears
self.will_draw = False # boolean to draw sword object onto screen
# method to draw sprite to screen
def draw(self, screen):
screen.blit(self.curr_image, [self.rect.x, self.rect.y])
# method to handle sword positioning when the player attacks
def attack(self):
# code to put rectangle x value at area where character is facing
if player_obj.last_x > 0:
self.curr_image = self.image_right
self.rect = self.image.get_rect()
self.rect.x = player_obj.rect.x + player_obj.width - 2
elif player_obj.last_x < 0:
self.curr_image = self.image_left
self.rect = self.image.get_rect()
self.rect.x = player_obj.rect.x - self.height + 2
# code to put rectangle y value at area where character is facing
if player_obj.last_y > 0:
self.curr_image = self.image_down
self.rect = self.image.get_rect()
self.rect.y = player_obj.rect.y + player_obj.height - 10
elif player_obj.last_y < 0:
self.curr_image = self.image_up
self.rect = self.image.get_rect()
self.rect.y = player_obj.rect.y - self.height
# code to set sword position if character facing opposite plane
if player_obj.last_x == 0:
self.rect.x = player_obj.rect.x + (player_obj.width / 2) - (self.width / 2)
if player_obj.last_y == 0:
self.rect.y = player_obj.rect.y + (player_obj.height / 2) - (self.width / 2)
# method to handle sword collision with enemies when player attacks
def attack_collision(self, enemy_list):
# create list of enemies hit by player sword
enemies_hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
for enemy in enemies_hit_list:
if not enemy.invulnerable: # if enemy is not invulnerable
enemy.health -= 2
enemy.invulnerable = True
enemies_hit.add(enemy)
# method to handle sword collision with breakable objects
def check_break(self, location, location_breakables):
# create list of objects broken by player sword
broken_obj_list = pygame.sprite.spritecollide(self, location_breakables, False)
for break_obj in broken_obj_list:
if location.overview: # only break object when in correct location to break it
break_obj.broken = True
# create sword object, for use during the game
sword_obj = Sword()
# initialise treasure chest class
class TreasureChest(pygame.sprite.Sprite):
def __init__(self, size, position_x, position_y, treasure):
super().__init__()
self.size = size # set sprite size
# load sprite from file
self.image = pygame.image.load("Chest.png").convert()
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.open_image = pygame.image.load("ChestOpen.png").convert()
self.open_image.set_colorkey(WHITE)
self.rect.x = position_x
self.rect.y = position_y
self.treasure = treasure # string for treasure in chest
self.game_end = False # boolean for if game is ended
self.treasure_message_timer = pygame.time.get_ticks() # timer for displaying treasure message
self.treasure_message_display = True # boolean for displaying treasure message
self.open = False # boolean to check if chest is open
# method to open chest and add treasure to player inventory
def pick_treasure(self):
self.image = self.open_image
player_obj.message("You found " + self.treasure + "!")
player_obj.inventory.append(self.treasure)
# method to change treasure chest treasure name
def set_treasure(self, treasure_name):
self.treasure = treasure_name
# method to check for player-chest collision
def check_collision(self, curr_location_enemies):
if pygame.sprite.collide_rect(player_obj,
self) and not self.open and not curr_location_enemies:
# open chest and display message if collision is detected
self.pick_treasure()
self.open = True
self.treasure_message_timer = pygame.time.get_ticks()
self.treasure_message_display = True
# code to check if treasure message should be displayed
if self.open:
if pygame.time.get_ticks() - self.treasure_message_timer <= 2000 and self.treasure_message_display:
player_obj.message("You found " + self.treasure + "!")
# method to draw chest to screen
def draw(self, screen):
screen.blit(self.image, [self.rect.x, self.rect.y])
# method to end game if final chest is reached
def check_end_game(self, curr_location):
# access global variables
global game_end
# code to check if game is finished
if pygame.sprite.collide_rect(player_obj, self):
game_end = True
curr_location.overview = False
# create class for tutorial rectangles
class TutorialRect(pygame.sprite.Sprite):
def __init__(self, width, height, x_position, y_position, message):
super().__init__()
# set size
self.width = width
self.height = height
self.image = pygame.Surface([self.width, self.height]) # create image
self.rect = self.image.get_rect() # generate rect from image
self.rect.x = x_position
self.rect.y = y_position
self.message = message # string for tutorial message
self.shown = False # boolean for if the tutorial message has been shown or not
# method for displaying the tutorial message to the screen
def show_tutorial(self, location):
if self.rect.colliderect(player_obj) and location.overview and not self.shown:
player_obj.message(self.message)
# method to end tutorial message
def end_tutorial(self):
self.shown = True
# create class for menu in-game
class Menu:
def __init__(self):
# load menu logos from file
self.logo = pygame.image.load("Menu_logo.png").convert()
self.logo.set_colorkey(WHITE)
self.start = pygame.image.load("Menu_start.png").convert()
self.start.set_colorkey(WHITE)
self.quit = pygame.image.load("Menu_quit.png").convert()
self.quit.set_colorkey(WHITE)
self.sword = pygame.image.load("Menu_sword.png").convert()
self.sword.set_colorkey(WHITE)
self.sword_rect = self.sword.get_rect() # generate sword rect from sprite
self.sword_position = 0 # integer for sword position index
self.menu_icons = 2 # attribute for total number of menu icons
self.menu = True # boolean for if menu should be displayed