-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjinxs_adventure.py
2173 lines (1789 loc) · 92.4 KB
/
jinxs_adventure.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
"""
Jinx and Gravity Game
This is the main game script
"""
import os
import math
import arcade
import random
# from signal import pause
from sys import builtin_module_names
from tokenize import Name
from typing import Optional
from unicodedata import name
from arcade import Point
file_path = os.path.dirname(os.path.abspath(__file__))
SCREEN_TITLE = "Jinx & Gravity"
DEFAULT_LINE_HEIGHT = 45
DEFAULT_FONT_SIZE = 20
# Key Game Layer Variables - central source of control
LAYER_NAME_PLATFORMS = "Platforms"
LAYER_NAME_PLAYER = "Player"
LAYER_NAME_BACKGROUND = "Background"
LAYER_NAME_FOREGROUND = "Foreground"
LAYER_NAME_COINS = "Coins"
LAYER_NAME_HEARTS = "Hearts"
LAYER_NAME_DONT_TOUCH = "Don't Touch"
LAYER_NAME_LADDERS = "Ladders"
LAYER_NAME_MOVING_PLATFORMS = "Moving Platforms"
LAYER_NAME_ENEMIES = "Enemies"
LAYER_NAME_DYNAMIC_ITEMS = "Dynamic Items"
LAYER_NAME_DYNAMIC_TILES = "Dynamic Tiles"
LAYER_NAME_PLAYER_BULLETS = "Player Bullets"
LAYER_NAME_PLAYER_GRENADES = "Player Grenades"
LAYER_NAME_ENEMY_BULLETS = "Enemy Bullets"
LAYER_NAME_ALLIES = "Allies"
LAYER_NAME_SHIELD = "Shield"
LAYER_NAME_POWER_UPS = "Power Ups"
# This script contains all the player features (and most of the physics variables)
# How big are our image tiles?
SPRITE_IMAGE_SIZE = 128
# Scale sprites up or down
SPRITE_SCALING_PLAYER = 0.4
SPRITE_SCALING_TILES = 0.4
# Scaled sprite size for tiles
SPRITE_SIZE = int(SPRITE_IMAGE_SIZE * SPRITE_SCALING_PLAYER)
# Size of grid to show on screen, in number of tiles
SCREEN_GRID_WIDTH = 25
SCREEN_GRID_HEIGHT = 13
# Size of screen to show, in pixels
SCREEN_WIDTH = SPRITE_SIZE * SCREEN_GRID_WIDTH
SCREEN_HEIGHT = SPRITE_SIZE * SCREEN_GRID_HEIGHT
# Calculate Grid Pixel Size
GRID_PIXEL_SIZE = SPRITE_IMAGE_SIZE * SPRITE_SCALING_TILES
# Player Start Location
start_grid_x = 1
start_grid_y = 1
# --- Physics forces. Higher number, faster accelerating.
# Gravity
GRAVITY = 1500
# Damping - Amount of speed lost per second
DEFAULT_DAMPING = 1.0
PLAYER_DAMPING = 0.4
# Friction between objects
PLAYER_FRICTION = 1.0
WALL_FRICTION = 0.7
DYNAMIC_ITEM_FRICTION = 0.6
# Mass (defaults to 1)
PLAYER_MASS = 2.0
# Keep player from going too fast
PLAYER_MAX_HORIZONTAL_SPEED = 450
PLAYER_MAX_VERTICAL_SPEED = 1600
# Force applied while on the ground
PLAYER_MOVE_FORCE_ON_GROUND = 8000
# Force applied when moving left/right in the air
PLAYER_MOVE_FORCE_IN_AIR = 900
# Strength of a jump
PLAYER_JUMP_IMPULSE = 1200
# Close enough to not-moving to have the animation go to idle.
DEAD_ZONE = 1
# Death Cooldown
DEATH_PROTECT = 15
# Constants used to track if the player is facing left or right
RIGHT_FACING = 0
LEFT_FACING = 1
# How many pixels to move before we change the texture in the walking animation
DISTANCE_TO_CHANGE_TEXTURE = 20
# Main Player Class
class PlayerSprite(arcade.Sprite):
""" Player Sprite """
def __init__(self,
ladder_list: arcade.SpriteList,
hit_box_algorithm):
""" Init """
# Let parent initialize
super().__init__()
# Set our scale
self.scale = SPRITE_SCALING_PLAYER
# Images from Character pack
main_path = file_path + "\\src\\resources\\images\\animated_characters\\jinx\\jinx"
# Load textures for idle standing
self.idle_texture_pair = arcade.load_texture_pair(f"{main_path}_idle.png",
hit_box_algorithm=hit_box_algorithm)
self.jump_texture_pair = arcade.load_texture_pair(f"{main_path}_jump.png")
self.fall_texture_pair = arcade.load_texture_pair(f"{main_path}_fall.png")
# Load textures for walking
self.walk_textures = []
for i in range(8):
texture = arcade.load_texture_pair(f"{main_path}_walk{i}.png")
self.walk_textures.append(texture)
# Load textures for climbing
self.climbing_textures = []
texture = arcade.load_texture(f"{main_path}_climb0.png")
self.climbing_textures.append(texture)
texture = arcade.load_texture(f"{main_path}_climb1.png")
self.climbing_textures.append(texture)
# Set the initial texture
self.texture = self.idle_texture_pair[0]
# Hit box will be set based on the first image used.
self.hit_box = self.texture.hit_box_points
# Default to face-right
self.character_face_direction = RIGHT_FACING
# Index of our current texture
self.cur_texture = 0
# How far have we traveled horizontally since changing the texture
self.x_odometer = 0
self.y_odometer = 0
self.ladder_list = ladder_list
self.is_on_ladder = False
def pymunk_moved(self, physics_engine, dx, dy, d_angle):
""" Handle being moved by the pymunk engine """
# Figure out if we need to face left or right
if dx < -DEAD_ZONE and self.character_face_direction == RIGHT_FACING:
self.character_face_direction = LEFT_FACING
elif dx > DEAD_ZONE and self.character_face_direction == LEFT_FACING:
self.character_face_direction = RIGHT_FACING
# Are we on the ground?
is_on_ground = physics_engine.is_on_ground(self)
# Are we on a ladder?
if len(arcade.check_for_collision_with_list(self, self.ladder_list)) > 0:
if not self.is_on_ladder:
self.is_on_ladder = True
self.pymunk.gravity = (0, 0)
self.pymunk.damping = 0.0001
self.pymunk.max_vertical_velocity = PLAYER_MAX_HORIZONTAL_SPEED
else:
if self.is_on_ladder:
self.pymunk.damping = 1.0
self.pymunk.max_vertical_velocity = PLAYER_MAX_VERTICAL_SPEED
self.is_on_ladder = False
self.pymunk.gravity = None
# Add to the odometer how far we've moved
self.x_odometer += dx
self.y_odometer += dy
if self.is_on_ladder and not is_on_ground:
# Have we moved far enough to change the texture?
if abs(self.y_odometer) > DISTANCE_TO_CHANGE_TEXTURE:
# Reset the odometer
self.y_odometer = 0
# Advance the walking animation
self.cur_texture += 1
if self.cur_texture > 1:
self.cur_texture = 0
self.texture = self.climbing_textures[self.cur_texture]
return
# Jumping animation
if not is_on_ground:
if dy > DEAD_ZONE:
self.texture = self.jump_texture_pair[self.character_face_direction]
return
elif dy < -DEAD_ZONE:
self.texture = self.fall_texture_pair[self.character_face_direction]
return
# Idle animation
if abs(dx) <= DEAD_ZONE:
self.texture = self.idle_texture_pair[self.character_face_direction]
return
# Have we moved far enough to change the texture?
if abs(self.x_odometer) > DISTANCE_TO_CHANGE_TEXTURE:
# Reset the odometer
self.x_odometer = 0
# Advance the walking animation
self.cur_texture += 1
if self.cur_texture > 7:
self.cur_texture = 0
self.texture = self.walk_textures[self.cur_texture][self.character_face_direction]
SPRITE_SCALING_ENEMIES = 0.8
ENEMY_SPRITE_IMAGE_SIZE = 64
ALLY_SPRITE_IMAGE_SIZE = 64
SPRITE_SCALING_ALLIES = 0.8
def load_texture_pair(filename):
"""
Load a texture pair, with the second being a mirror image.
"""
return [
arcade.load_texture(filename),
arcade.load_texture(filename, flipped_horizontally=True),
]
# Base enemy class
class Entity(arcade.Sprite):
def __init__(self, name_folder, name_file):
super().__init__()
# Default to facing right
self.facing_direction = RIGHT_FACING
# Used for image sequences
self.cur_texture = 0
self.scale = SPRITE_SCALING_ENEMIES
main_path = file_path + f"/src/resources/images/animated_characters/{name_folder}/{name_file}"
self.idle_texture_pair = load_texture_pair(f"{main_path}_idle.png")
self.jump_texture_pair = load_texture_pair(f"{main_path}_jump.png")
self.fall_texture_pair = load_texture_pair(f"{main_path}_fall.png")
# Load textures for walking
self.walk_textures = []
for i in range(8):
texture = load_texture_pair(f"{main_path}_walk{i}.png")
self.walk_textures.append(texture)
# Load textures for climbing
self.climbing_textures = []
texture = arcade.load_texture(f"{main_path}_climb0.png")
self.climbing_textures.append(texture)
texture = arcade.load_texture(f"{main_path}_climb1.png")
self.climbing_textures.append(texture)
# Set the initial texture
self.texture = self.idle_texture_pair[0]
# Hit box will be set based on the first image used. If you want to specify
# a different hit box, you can do it like the code below.
# self.set_hit_box([[-22, -64], [22, -64], [22, 28], [-22, 28]])
self.set_hit_box(self.texture.hit_box_points)
class Enemy(Entity):
def __init__(self, name_folder, name_file):
# Setup parent class
super().__init__(name_folder, name_file)
self.should_update_walk = 0
self.scale = SPRITE_SCALING_ENEMIES
def update_animation(self, delta_time: float = 1 / 60):
# Figure out if we need to flip face left or right
if self.change_x < 0 and self.facing_direction == RIGHT_FACING:
self.facing_direction = LEFT_FACING
elif self.change_x > 0 and self.facing_direction == LEFT_FACING:
self.facing_direction = RIGHT_FACING
# Idle animation
if self.change_x == 0:
self.texture = self.idle_texture_pair[self.facing_direction]
return
# Walking animation
if self.should_update_walk == 3:
self.cur_texture += 1
if self.cur_texture > 7:
self.cur_texture = 0
self.texture = self.walk_textures[self.cur_texture][self.facing_direction]
self.should_update_walk = 0
return
self.should_update_walk += 1
class GreenWorm(Enemy):
def __init__(self):
# Set up parent class
super().__init__("wormGreen", "wormGreen")
self.health = 10
class BlueSlime(Enemy):
def __init__(self):
# Set up parent class
super().__init__("slimeBlue", "slimeBlue")
self.health = 20
class LavaSnake(Enemy):
def __init__(self):
# Set up parent class
super().__init__("snakeLava", "snakeLava")
self.health = 50
class GreenSlime(Enemy):
def __init__(self):
# Set up parent class
super().__init__("slimeGreen", "slimeGreen")
self.health = 50
class PurpleSlime(Enemy):
def __init__(self):
# Set up parent class
super().__init__("slimePurple", "slimePurple")
self.health = 100
class Thunderer(Enemy):
def __init__(self):
# Set up parent class
super().__init__("thunderer", "thunderer")
self.health = 150
class BlueSlimeBoss(Enemy):
def __init__(self):
# Set up parent class
super().__init__("slimeBlueBoss", "slimeBlueBoss")
self.health = 2500
class Chomper(Enemy):
def __init__(self):
# Set up parent class
super().__init__("chomper", "chomper")
self.health = 350
class SilverSlime(Enemy):
def __init__(self):
# Set up parent class
super().__init__("slimeSilver", "slimeSilver")
self.health = 1000
class DiamondShooter(Enemy):
def __init__(self):
# Set up parent class
super().__init__("diamondshooter", "diamondshooter")
self.health = 500
class PrimarySlime(Enemy):
def __init__(self):
# Set up parent class
super().__init__("primaryslime", "primaryslime")
self.health = 1500
class SecondarySlime(Enemy):
def __init__(self):
# Set up parent class
super().__init__("secondaryslime", "secondaryslime")
self.health = 3000
class SuperThunderer(Enemy):
def __init__(self):
# Set up parent class
super().__init__("superthunderer", "superthunderer")
self.health = 10000
class RobotEnemy(Enemy):
def __init__(self):
# Set up parent class
super().__init__("robot", "robot")
self.health = 5000
class RolyPolyBot(Enemy):
def __init__(self):
# Set up parent class
super().__init__("rolypolybot", "rolypolybot")
self.health = 10000
class MasterVerse(Enemy):
def __init__(self):
# Set up parent class
super().__init__("masterverse", "masterverse")
self.health = 50000
class FlufflePop(Enemy):
def __init__(self):
# Set up parent class
super().__init__("flufflepop", "flufflepop")
self.health = 100000
# Now for ally characters
class Ally(Entity):
def __init__(self, name_folder, name_file):
# Setup parent class
super().__init__(name_folder, name_file)
self.should_update_walk = 0
self.scale = SPRITE_SCALING_ALLIES
def update_animation(self, delta_time: float = 1 / 60):
# Figure out if we need to flip face left or right
if self.change_x < 0 and self.facing_direction == RIGHT_FACING:
self.facing_direction = LEFT_FACING
elif self.change_x > 0 and self.facing_direction == LEFT_FACING:
self.facing_direction = RIGHT_FACING
# Idle animation
if self.change_x == 0:
self.texture = self.idle_texture_pair[self.facing_direction]
return
# Walking animation
if self.should_update_walk == 3:
self.cur_texture += 1
if self.cur_texture > 7:
self.cur_texture = 0
self.texture = self.walk_textures[self.cur_texture][self.facing_direction]
self.should_update_walk = 0
return
self.should_update_walk += 1
class Hooboo(Ally):
def __init__(self):
# Set up parent class
super().__init__("hooboo", "hooboo")
self.health = 500
class FlufflePop(Ally):
def __init__(self):
# Set up parent class
super().__init__("flufflepop", "flufflepop")
self.health = 100000
class Pumbean(Ally):
def __init__(self):
# Set up parent class
super().__init__("pumbean", "pumbean")
self.health = 1500
class Excalibur(Ally):
def __init__(self):
# Set up parent class
super().__init__("excalibur", "excalibur")
self.health = 2500
class MasterVerse(Ally):
def __init__(self):
# Set up parent class
super().__init__("masterverse", "masterverse")
self.health = 50000
class RolyPolyBot(Ally):
def __init__(self):
# Set up parent class
super().__init__("rolypolybot", "rolypolybot")
self.health = 10000
class SecondarySlime(Ally):
def __init__(self):
# Set up parent class
super().__init__("secondaryslime", "secondaryslime")
self.health = 3000
# This is the main weapons script
# Adding coin scaling
COIN_SCALING = 0.5
# Shooting Constants
SPRITE_SCALING_PROJECTILES = 0.6
SHOOT_SPEED = 100
BULLET_SPEED = 2
SHIELD_SPEED = 1000
PLAYER_BULLET_DAMAGE = 10
# How much force to put on the bullet
BULLET_MOVE_FORCE = 1000
# Mass of the bullet
BULLET_MASS = 0.1
# Make bullet less affected by gravity
BULLET_GRAVITY = 300
class GrenadeSprite(arcade.SpriteSolidColor):
""" Bullet Sprite """
def pymunk_moved(self, physics_engine, dx, dy, d_angle):
""" Handle when the sprite is moved by the physics engine. """
# If the bullet falls below the screen, remove it
if self.center_y < -100:
self.remove_from_sprite_lists()
class MenuView(arcade.View):
def on_show_view(self):
arcade.set_background_color(arcade.color.GRAY_BLUE)
def on_draw(self):
self.clear()
arcade.draw_text("Welcome to Jinx's Adventure", self.window.width / 2, self.window.height / 2,
arcade.color.BLACK, font_size=50, anchor_x="center")
arcade.draw_text("A crazy physics platformer", self.window.width / 2, self.window.height / 2 - 75,
arcade.color.RED_BROWN, font_size=25, anchor_x="center")
arcade.draw_text("Click to see the instructions", self.window.width / 2, self.window.height / 2 - 150,
arcade.color.REDWOOD, font_size=20, anchor_x="center")
def on_mouse_press(self, _x, _y, _button, _modifiers):
instructions_view = InstructionView()
self.window.show_view(instructions_view)
class InstructionView(arcade.View):
def on_show_view(self):
""" This is run once when we switch to this view """
arcade.set_background_color(arcade.csscolor.DARK_SLATE_BLUE)
# Reset the viewport, necessary if we have a scrolling game and we need
# to reset the viewport back to the start so we can see what we draw.
arcade.set_viewport(0, self.window.width, 0, self.window.height)
def on_draw(self):
""" Draw this view """
self.clear()
arcade.draw_text("Game Instructions", self.window.width / 2, self.window.height / 2,
arcade.color.WHITE, font_size=50, anchor_x="center")
arcade.draw_text("ASDW or arrows to move, Q/N for bullets", self.window.width / 2, self.window.height / 2-80,
arcade.color.PINK_LAVENDER, font_size=25, anchor_x="center")
arcade.draw_text("E/M to shield, Mouse for grenades. Points increase your level", self.window.width / 2, self.window.height / 2-120,
arcade.color.YELLOW_GREEN, font_size=25, anchor_x="center")
arcade.draw_text("Increasing your level improves bullets, damage and jump", self.window.width / 2, self.window.height / 2-160,
arcade.color.DARK_CHESTNUT, font_size=25, anchor_x="center")
arcade.draw_text("Grenades activate at level one. Keep an eye out for power ups", self.window.width / 2, self.window.height / 2-200,
arcade.color.PINK_LAVENDER, font_size=25, anchor_x="center")
def on_mouse_press(self, _x, _y, _button, _modifiers):
""" If the user presses the mouse button, start the game. """
game_view = GameView()
game_view.setup()
self.window.show_view(game_view)
class GameOverView(arcade.View):
""" View to show when game is over """
def __init__(self):
""" This is run once when we switch to this view """
super().__init__()
self.texture = arcade.load_texture(file_path + "/src/resources/images/tiles/custom_tiles/pizzaman2.png")
# Reset the viewport, necessary if we have a scrolling game and we need
# to reset the viewport back to the start so we can see what we draw.
arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)
def on_draw(self):
""" Draw this view """
self.clear()
self.texture.draw_sized(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2,
SCREEN_WIDTH, SCREEN_HEIGHT)
def on_mouse_press(self, _x, _y, _button, _modifiers):
""" If the user presses the mouse button, re-start the game. """
game_view = GameView()
game_view.setup()
self.window.show_view(game_view)
class GameView(arcade.View):
""" Main Window """
def __init__(self):
""" Create the variables """
# Init the parent class
super().__init__()
# Don't show the mouse cursor
self.window.set_mouse_visible(True)
# Add width and height
self.width = SCREEN_WIDTH
self.height = SCREEN_HEIGHT
# Initialise Frame Count
self.frame_count = 0
# Add the screen title
# If wanted later on can be added here
# Sprite lists we need
self.wall_list: Optional[arcade.SpriteList] = None
self.grenade_list: Optional[arcade.SpriteList] = None
self.item_list: Optional[arcade.SpriteList] = None
self.block_list: Optional[arcade.SpriteList] = None
self.moving_sprites_list: Optional[arcade.SpriteList] = None
self.ladder_list: Optional[arcade.SpriteList] = None
self.coin_list: Optional[arcade.SpriteList] = None
self.heart_list: Optional[arcade.SpriteList] = None
self.enemies_list: Optional[Enemy] = None
self.allies_list: Optional[Enemy] = None
self.player_bullets: Optional[arcade.SpriteList] = None
self.player_list: Optional[arcade.SpriteList] = None
# Player sprite
self.player_sprite: Optional[PlayerSprite] = None
# Track the current state of what key is pressed
self.left_pressed: bool = False
self.right_pressed: bool = False
self.up_pressed: bool = False
self.down_pressed: bool = False
self.shoot_pressed: bool = False
self.shield_pressed: bool = False
self.mouse_pressed: bool = False
# Physics engine
self.physics_engine: Optional[arcade.PymunkPhysicsEngine] = None
# Add camera
self.camera = None
# A Camera that can be used to draw GUI elements
self.gui_camera = None
# Our TileMap Object
self.tile_map = None
# Our Scene Object
self.scene = None
# Shooting mechanics
self.can_shoot = False
self.shoot_timer = 0
# Shielding mechanics
self.can_shield = False
self.shield_timer = 0
# Life mechanics
self.can_die = False
self.death_timer = 0
self.invincibility_timer = 0
# Super bullet mode
self.grenade_booster = 0
# Keep track of the score
self.score = 0
# Do we need to reset the score?
self.reset_score = True
# Set background color
arcade.set_background_color(arcade.color.BLEU_DE_FRANCE)
# Where is the right edge of the map?
self.end_of_map = 0
# Selfs
self.x = 0
self.y = 0
# Level
self.level = 0
# Level_Up
self.level_up = 0
# Lives
self.lives = 3
# Load sounds
self.game_over = arcade.load_sound(file_path+"/src/resources/sounds/gameover2.wav")
self.collect_coin_sound = arcade.load_sound(file_path+"/src/resources/sounds/coin1.wav")
self.jump_sound = arcade.load_sound(file_path+"/src/resources/sounds/jump3.wav")
self.hit_sound = arcade.load_sound(file_path+"/src/resources/sounds/hit2.wav")
self.shoot_sound = arcade.load_sound(file_path+"/src/resources/sounds/hurt3.wav")
# Add messages
self.message1 = None
self.message2 = None
self.message3 = None
self.message4 = None
self.message5 = None
def setup(self):
""" Set up everything with the game """
# Layer Specific Options for the Tilemap
layer_options = {
LAYER_NAME_PLATFORMS: {
"use_spatial_hash": True,
},
LAYER_NAME_ENEMIES: {
"use_spatial_hash": False,
},
LAYER_NAME_ALLIES: {
"use_spatial_hash": False,
},
LAYER_NAME_MOVING_PLATFORMS: {
"use_spatial_hash": False,
},
LAYER_NAME_SHIELD: {
"use_spatial_hash": True,
},
LAYER_NAME_LADDERS: {
"use_spatial_hash": True,
},
LAYER_NAME_COINS: {
"use_spatial_hash": True,
},
LAYER_NAME_DONT_TOUCH: {
"use_spatial_hash": True,
},
}
# Set up the GUI Camera
self.gui_camera = arcade.Camera(self.width, self.height)
# Keep track of the score
self.score = self.score
# Level_Up
self.level_up = 0
# Lives at the start of each level
self.lives = self.lives
# Life mechanics
self.can_die = True
self.death_timer = 0
self.invincibility_timer = 0
# Super bullet mode
self.grenade_booster = 0
# Shielding mechanics
self.can_shield = True
self.shield_timer = 0
# Shooting mechanics
self.can_shoot = True
self.shoot_timer = 0
# Shooting mechanics
self.mouse_pressed = False
# Keep track of the score, make sure we keep the score if the player finishes a level
if self.reset_score:
self.score = 0
self.reset_score = False
# Set up the Camera
self.camera = arcade.Camera(self.width, self.height)
# Create the sprite lists
self.player_list = arcade.SpriteList()
self.grenade_list = arcade.SpriteList()
# Map name
map_name = file_path + f"/src/resources/images/tiled_maps/level_{self.level}.json"
# Load in TileMap
self.tile_map = arcade.load_tilemap(map_name, SPRITE_SCALING_TILES, layer_options)
# Initiate New Scene with our TileMap, this will automatically add all layers
# from the map as SpriteLists in the scene in the proper order.
self.scene = arcade.Scene.from_tilemap(self.tile_map)
# Pull the sprite layers out of the tile map
self.background_list = self.tile_map.sprite_lists[LAYER_NAME_BACKGROUND]
self.wall_list = self.tile_map.sprite_lists[LAYER_NAME_PLATFORMS]
self.coin_list = self.tile_map.sprite_lists[LAYER_NAME_COINS]
self.heart_list = self.tile_map.sprite_lists[LAYER_NAME_HEARTS]
self.dont_touch_list = self.tile_map.sprite_lists[LAYER_NAME_DONT_TOUCH]
self.item_list = self.tile_map.sprite_lists[LAYER_NAME_DYNAMIC_ITEMS]
self.block_list = self.tile_map.sprite_lists[LAYER_NAME_DYNAMIC_TILES]
self.ladder_list = self.tile_map.sprite_lists[LAYER_NAME_LADDERS]
self.moving_sprites_list = self.tile_map.sprite_lists[LAYER_NAME_MOVING_PLATFORMS]
self.power_ups_list = self.tile_map.sprite_lists[LAYER_NAME_POWER_UPS]
# Create player sprite
self.player_sprite = PlayerSprite(self.ladder_list, hit_box_algorithm="Detailed")
# Set player start location
self.player_sprite.center_x = SPRITE_SIZE * start_grid_x + SPRITE_SIZE / 2
self.player_sprite.center_y = SPRITE_SIZE * start_grid_y + SPRITE_SIZE / 2
# Add to player sprite list
self.player_list.append(self.player_sprite)
# Make sure that forground is added afterwards
self.foreground_list = self.tile_map.sprite_lists[LAYER_NAME_FOREGROUND]
# Calculate the right edge of the my_map in pixels
self.end_of_map = self.tile_map.width * GRID_PIXEL_SIZE
# -- Enemies
self.enemies_list = self.tile_map.object_lists[LAYER_NAME_ENEMIES]
self.allies_list = self.tile_map.object_lists[LAYER_NAME_ALLIES]
# Speech
speech_list = []
# Map Allies
for my_object in self.allies_list:
cartesian = self.tile_map.get_cartesian(
my_object.shape[0], my_object.shape[1]
)
ally_type = my_object.properties["type"]
if ally_type == "hooboo":
ally = Hooboo()
elif ally_type == "flufflepop":
ally = FlufflePop()
elif ally_type == "pumbean":
ally = Pumbean()
elif ally_type == "excalibur":
ally = Excalibur()
elif ally_type == "masterverse":
ally = MasterVerse()
elif ally_type == "rolypolybot":
ally = RolyPolyBot()
elif ally_type == "secondaryslime":
ally = SecondarySlime()
else:
raise Exception(f"Unknown ally type {ally_type}.")
ally.center_x = math.floor(
cartesian[0] * SPRITE_SCALING_ALLIES * ALLY_SPRITE_IMAGE_SIZE
)
ally.center_y = math.floor(
(cartesian[1] + 1) * (SPRITE_SCALING_ALLIES * ALLY_SPRITE_IMAGE_SIZE)
)
if "boundary_left" in my_object.properties:
ally.boundary_left = my_object.properties["boundary_left"]
if "boundary_right" in my_object.properties:
ally.boundary_right = my_object.properties["boundary_right"]
if "change_x" in my_object.properties:
ally.change_x = my_object.properties["change_x"]
if "speech" in my_object.properties:
self.speech = arcade.Text(
text = my_object.properties["speech"],
start_x=ally.center_x,
start_y=ally.top,
color = arcade.color.BLACK,
font_size = DEFAULT_FONT_SIZE)
speech_list.append(self.speech)
self.scene.add_sprite(LAYER_NAME_ALLIES, ally)
# Assign speech objects
self.message1 = speech_list[0]
self.message2 = speech_list[1]
self.message3 = speech_list[2]
self.message4 = speech_list[3]
self.message5 = speech_list[4]
# Map Enemy Objects
for my_object in self.enemies_list:
cartesian = self.tile_map.get_cartesian(
my_object.shape[0], my_object.shape[1]
)
enemy_type = my_object.properties["type"]
if enemy_type == "wormGreen":
enemy = GreenWorm()
elif enemy_type == "slimeBlue":
enemy = BlueSlime()
elif enemy_type == "snakeLava":
enemy = LavaSnake()
elif enemy_type == "slimeGreen":
enemy = GreenSlime()
elif enemy_type == "slimePurple":
enemy = PurpleSlime()
elif enemy_type == "slimeSilver":
enemy = SilverSlime()
elif enemy_type == "slimeBlueBoss":
enemy = BlueSlimeBoss()
elif enemy_type == "primaryslime":
enemy = PrimarySlime()
elif enemy_type == "secondaryslime":
enemy = SecondarySlime()
elif enemy_type == "thunderer":
enemy = Thunderer()
elif enemy_type == "superthunderer":
enemy = SuperThunderer()
elif enemy_type == "chomper":
enemy = Chomper()
elif enemy_type == "diamondshooter":
enemy = DiamondShooter()
elif enemy_type == "robot":
enemy = RobotEnemy()
elif enemy_type == "rolypolybot":
enemy = RolyPolyBot()
elif enemy_type == "masterverse":
enemy = MasterVerse()
elif enemy_type == "flufflepop":
enemy = FlufflePop()
else:
raise Exception(f"Unknown enemy type {enemy_type}.")
enemy.center_x = math.floor(
cartesian[0] * SPRITE_SCALING_ENEMIES * ENEMY_SPRITE_IMAGE_SIZE
)
enemy.center_y = math.floor(
(cartesian[1] + 1) * (SPRITE_SCALING_ENEMIES * ENEMY_SPRITE_IMAGE_SIZE)
)
if "boundary_left" in my_object.properties:
enemy.boundary_left = my_object.properties["boundary_left"]
if "boundary_right" in my_object.properties:
enemy.boundary_right = my_object.properties["boundary_right"]
if "change_x" in my_object.properties:
enemy.change_x = my_object.properties["change_x"]
self.scene.add_sprite(LAYER_NAME_ENEMIES, enemy)
# Add bullet spritelist to Scene
self.scene.add_sprite_list(LAYER_NAME_PLAYER_BULLETS)
# Add enemy bullets and shields
self.scene.add_sprite_list(LAYER_NAME_ENEMY_BULLETS)
self.scene.add_sprite_list(LAYER_NAME_SHIELD)