-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLast_Lab_Defender.py
More file actions
1811 lines (1568 loc) · 72.8 KB
/
Copy pathLast_Lab_Defender.py
File metadata and controls
1811 lines (1568 loc) · 72.8 KB
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
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import math
import random
from OpenGL.GLUT import GLUT_BITMAP_HELVETICA_18
import time
# Global variables
camera_pos = (1000, 1000, 800)
camera_look_at = (327, 77, 75)
axis_decision = (0, 0, 1)
window_height, window_width = 630, 1270
field_of_view = 90
GRID_LENGTH, GRID_WIDTH = 2250, 2250
class Last_Lab_Defender:
def __init__(self):
self.floor_right_max = -GRID_LENGTH // 2
self.floor_left_max = GRID_LENGTH // 2
self.floor_behind_max = -GRID_WIDTH // 2
self.floor_front_max = GRID_WIDTH // 2
self.initiate_all()
def initiate_all(self):
# game intro (multi-stage from Doc 2)
self.intro_starting_time = None
self.game_started = False
self.ongoing_game_restarted = False
self.game_intro_ongoing = True
self.into_skipped = False
self.intro_stage = 1
# time related
self.level_1_time_limit = 40 # seconds
self.level_2_time_limit = 60
self.start_time = time.time()
self.remaining_time = self.level_1_time_limit
self.time_passed = 0
# game level
self.game_level = 1
self.transition_pause = False
self.transition_start = None
# kill count
self.total_kills = 0
self.level_1_kill_limit = 30
self.level_2_kill_limit = 40
self.level_1_limit_crossed = False
self.level_2_limit_crossed = False
# capsule
self.capsule_height = GRID_LENGTH // 10
self.capsule_radius = GRID_WIDTH // 32
self.capsule_position = [self.floor_left_max - 260, self.floor_front_max - 260, 10]
self.capsule_base_position = [self.floor_left_max - 260, self.floor_front_max - 260, 0]
self.capsule_base_height = 10
self.capsule_health = 10
# protagonist
self.player_angle = 0
self.player_spawn_position = (self.floor_left_max - 260, self.floor_front_max - 600, 0)
self.player_body_height = 40
self.player_head_radius = 20
self.shoe_radius = 5
self.player_leg_height = self.player_body_height
self.player_leg_max_radius = 10
self.player_width = 25
self.gun_height = 45
self.gun_facing = [0, 0, 0]
self.player_speed = 10
# bullets
self.bullet_size = 6
self.bullet_speed = 16
self.all_bullets = []
# --- ENEMY & PLAYER STATE ---
self.player_health = 5
self.normal_enemies_killed = 0
self.normal_enemy_kills_for_special = 0
self.enemies = []
#nerfed speed for dense swarms
self.enemy_speed = 0.85
self.enemy_body_radius = 35
self.enemy_head_radius = 25
self.enemy_base_z = self.enemy_body_radius
# spawn timer
self.last_spawn_time = time.time()
# enemy projectiles (Level 2+)
self.enemy_bullets = []
self.enemy_bullet_speed = 3.5
self.enemy_bullet_size = 8
# volley coordinator (Level 2+)
self.enemy_volley_timer = time.time()
self.volley_interval = 2.0
# player hit tolerance
self.player_hit_tolerance = 0
# Level 3 boss state
self.boss_spawned = False
self.cannonballs = []
self.cannonball_speed = 7
self.cannonball_size = 45
self.consecutive_cannonballs_destroyed = 0
# player view
self.first_person_view = False
# pause + game over
self.paused = False
self.pause_start_time = None
self.game_over = False
self.game_won = False
# Cheat mode
self.cheat_mode_active = False
#magazine
self.mag_size = 20
self.ammo_mag = self.mag_size
# GAME INTRO
def game_intro_1(self):
global camera_pos, camera_look_at
camera_pos = (900, 700, 650)
camera_look_at = (300, 200, 150)
x = window_width // 2 - 620
top = window_height // 2
bottom = 30
y_center = (top + bottom) // 2
self.draw_text(x, y_center + 60,
"Our protagonist is the last of the guardians assigned for shielding a capsule containing a secret bio substance. ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center,
"Some aliens have already made it all the way to this realm in search of this. ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center - 60,
"They have obliterated all sorts of defenses and terminated all the guardians except the protagonist ",
GLUT_BITMAP_HELVETICA_18)
def game_intro_2(self):
global camera_pos, camera_look_at
camera_pos = (-200, 650, 250)
camera_look_at = (300, 200, 120)
x = window_width // 2 - 620
top = window_height // 2
bottom = 30
y_center = (top + bottom) // 2
self.draw_text(x, y_center + 60,
"Aliens have already made it all the way to this realm in search of the capsule ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center,
"They have obliterated all sorts of defenses in their path and terminated all the guardians. ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center - 60,
"Only the protagonist survives, who is now stuck with the capsule inside the room",
GLUT_BITMAP_HELVETICA_18)
def game_intro_3(self):
global camera_pos, camera_look_at
camera_pos = (320, 80, 170)
camera_look_at = (300, 200, 140)
x = window_width // 2 - 620
top = window_height // 2
bottom = 30
y_center = (top + bottom) // 2
self.draw_text(x, y_center + 60,
"Now our protagonist has to defend the capsule at any cost using a gunlike weapon to neutralize the enemies. ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center,
"Our guardian's suit can automatically extract lifeline regenerating substances by killing some special aliens colored in red. ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center - 60,
"Furthermore, after a certain number of kills, his weapon can automatically upgrade to a better version with more efficiency.",
GLUT_BITMAP_HELVETICA_18)
def game_intro_4(self):
global camera_pos, camera_look_at
camera_pos = (350, 250, 180)
camera_look_at = (300, 200, 120)
x = window_width // 2 - 620
top = window_height // 2
bottom = 30
y_center = (top + bottom) // 2
self.draw_text(x, y_center + 60,
"Aliens sending the weak unarmed aliens initially to test the power of the threat ",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center,
"Within a timeframe if the protagonist proves to be resilient enough, they will send a more potent armed regiment.",
GLUT_BITMAP_HELVETICA_18)
self.draw_text(x, y_center - 60,
" If the guardian remains unweavered, then the final boss of the aliens will take matters into its own hands.",
GLUT_BITMAP_HELVETICA_18)
def game_intro(self):
global camera_pos
elapsed_time = time.time() - self.intro_starting_time
if elapsed_time >= 4:
self.intro_stage += 1
self.intro_starting_time = time.time()
if self.intro_stage > 4:
self.game_intro_ongoing = False
self.game_started = True
self.start_time = time.time()
camera_pos = (1000, 1000, 800)
self.first_person_view = True
return
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, window_width, 0, window_height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
glColor3f(0.0, 0.2, 0.2)
glBegin(GL_QUADS)
glVertex2f(0, window_height // 2)
glVertex2f(window_width, window_height // 2)
glColor3f(0.1, 0.4, 0.1)
glVertex2f(window_width, 30)
glVertex2f(0, 30)
glEnd()
glEnable(GL_DEPTH_TEST)
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
if self.intro_stage == 1:
self.game_intro_1()
elif self.intro_stage == 2:
self.game_intro_2()
elif self.intro_stage == 3:
self.game_intro_3()
elif self.intro_stage == 4:
self.game_intro_4()
def time_control(self):
self.time_passed = time.time() - self.start_time
current_limit = self.level_1_time_limit if self.game_level == 1 else self.level_2_time_limit
self.remaining_time = max(0, current_limit - self.time_passed)
if self.remaining_time <= 0 and not self.transition_pause:
self.transition_pause = True
self.transition_start = time.time()
self.enemies.clear()
self.enemy_bullets.clear()
self.all_bullets.clear()
self.cannonballs.clear()
def display_time(self):
if self.remaining_time > 59:
time_text = "Remaining Time: 01:00"
else:
time_text = f"Remaining Time: 00:{int(self.remaining_time)}"
self.draw_text(window_width - 250, window_height - 30, time_text)
def draw_protagonist(self):
p_x, p_y, p_z = self.player_spawn_position
glPushMatrix()
glTranslatef(p_x, p_y, p_z)
glRotatef(self.player_angle, 0, 0, 1)
# body
glPushMatrix()
glColor3f(225 / 255, 225 / 255, 230 / 255)
glTranslatef(0, 0, (2 * self.player_body_height))
glRotatef(5, 1, 0, 0)
glScalef(1.2, 0.6, 1.5)
glutSolidCube(self.player_body_height)
glPopMatrix()
# shoes
glPushMatrix()
glColor3f(0, 0, 0)
glTranslatef(-10, -5, 5)
glScalef(1.5, 2.5, 1.5)
gluSphere(gluNewQuadric(), self.shoe_radius, 80, 80)
glPopMatrix()
glPushMatrix()
glColor3f(0, 0, 0)
glTranslatef(-12, 0, 5)
glScalef(1.5, 2.5, 1.5)
gluSphere(gluNewQuadric(), self.shoe_radius, 80, 80)
glPopMatrix()
glPushMatrix()
glColor3f(0, 0, 0)
glTranslatef(+12, 0, 5)
glScalef(1.5, 2.5, 1.5)
gluSphere(gluNewQuadric(), self.shoe_radius, 80, 80)
glPopMatrix()
# legs
glPushMatrix()
glColor3f(60 / 255, 70 / 255, 80 / 255)
glTranslatef(-12, 0, +10)
gluCylinder(gluNewQuadric(), self.player_leg_max_radius * 0.5, self.player_leg_max_radius,
self.player_leg_height, 50, 20)
glPopMatrix()
glPushMatrix()
glColor3f(60 / 255, 70 / 255, 80 / 255)
glTranslatef(12, 0, +10)
gluCylinder(gluNewQuadric(), self.player_leg_max_radius * 0.5, self.player_leg_max_radius,
self.player_leg_height, 50, 20)
glPopMatrix()
# head
glPushMatrix()
glColor3f(240 / 255, 204 / 255, 173 / 255)
glTranslatef(0, -12, self.player_body_height * 3.2)
gluSphere(gluNewQuadric(), self.player_head_radius, 80, 80)
glPopMatrix()
# hat
glPushMatrix()
glColor3f(51 / 255, 51 / 255, 51 / 255)
glTranslatef(0, -12, self.player_body_height * 3.5)
glRotatef(-200, 1, 0, 0)
glScalef(1.05, 1.08, 0.6)
gluSphere(gluNewQuadric(), self.player_head_radius, 80, 80)
glPopMatrix()
# arms
glPushMatrix()
glColor3f(196 / 255, 196 / 255, 196 / 255)
glTranslatef(-30, 0, self.player_body_height * 2 + self.player_leg_height - self.player_head_radius)
gluSphere(gluNewQuadric(), self.player_head_radius // 2, 80, 80)
glPopMatrix()
glPushMatrix()
glColor3f(196 / 255, 196 / 255, 196 / 255)
glTranslatef(-30, 0, self.player_body_height * 2 + self.player_leg_height - self.player_head_radius)
glRotatef(90, 1, 0, 0)
glRotate(45, 1, 0, 0)
gluCylinder(gluNewQuadric(), self.player_leg_max_radius, self.player_leg_max_radius * 0.7,
self.player_leg_height, 50, 20)
glPopMatrix()
glPushMatrix()
glColor3f(240 / 255, 204 / 255, 173 / 255)
glTranslatef(-30, -self.player_leg_height + 18, self.player_body_height * 2 - 6)
glRotatef(90, 1, 0, 0)
glRotate(-25, 1, 0, 0)
glRotatef(45, 0, 1, 0)
gluCylinder(gluNewQuadric(), self.player_leg_max_radius - 2, self.player_leg_max_radius * 0.5,
self.player_leg_height, 50, 20)
glPopMatrix()
glPushMatrix()
glColor3f(196 / 255, 196 / 255, 196 / 255)
glTranslatef(30, 0, self.player_body_height * 2 + self.player_leg_height - self.player_head_radius)
gluSphere(gluNewQuadric(), self.player_head_radius // 2, 80, 80)
glPopMatrix()
glPushMatrix()
glColor3f(196 / 255, 196 / 255, 196 / 255)
glTranslatef(30, 0, self.player_body_height * 2 + self.player_leg_height - self.player_head_radius)
glRotatef(90, 1, 0, 0)
glRotate(45, 1, 0, 0)
gluCylinder(gluNewQuadric(), self.player_leg_max_radius, self.player_leg_max_radius * 0.5,
self.player_leg_height, 50, 20)
glPopMatrix()
glPushMatrix()
glColor3f(240 / 255, 204 / 255, 173 / 255)
glTranslatef(30, -self.player_leg_height + 18, self.player_body_height * 2 - 3)
glRotatef(90, 1, 0, 0)
glRotate(-15, 1, 0, 0)
glRotatef(-45, 0, 1, 0)
gluCylinder(gluNewQuadric(), self.player_leg_max_radius - 2, self.player_leg_max_radius * 0.5,
self.player_leg_height, 50, 20)
glPopMatrix()
# weapon wrist
glPushMatrix()
glColor3f(240 / 255, 204 / 255, 173 / 255)
glTranslatef(0, -50, self.player_leg_height + self.player_body_height * 1.5 - 14)
gluSphere(gluNewQuadric(), self.player_head_radius // 2, 80, 20)
glPopMatrix()
# weapon back
glPushMatrix()
glColor3f(self.level_weapon_head_color[0], self.level_weapon_head_color[1], self.level_weapon_head_color[2])
glTranslatef(0, -35, self.player_leg_height + self.player_body_height * 1.5)
gluSphere(gluNewQuadric(), self.player_head_radius // 2.5, 80, 20)
glPopMatrix()
# weapon barrel
glPushMatrix()
glColor3f(self.level_weapon_head_color[0], self.level_weapon_head_color[1], self.level_weapon_head_color[2])
glTranslatef(0, -35, self.player_leg_height + self.player_body_height * 1.5)
dir_x = math.cos(math.radians(self.player_angle - 90))
dir_y = math.sin(math.radians(self.player_angle - 90))
self.gun_facing = [
p_x + dir_x * (self.gun_height + 20),
p_y + dir_y * (self.gun_height + 20),
p_z + self.player_leg_height + self.player_body_height * 1.5
]
glRotatef(90, 1, 0, 0)
gluCylinder(gluNewQuadric(), self.shoe_radius * 1.5, self.shoe_radius, self.player_leg_height * 2, 50, 80)
glPopMatrix()
# weapon handle
glPushMatrix()
glColor3f(self.level_weapon_handle_color[0], self.level_weapon_handle_color[1],
self.level_weapon_handle_color[2])
glTranslatef(0, -45, self.player_leg_height + self.player_body_height - 5)
glRotatef(15, 1, 0, 0)
gluCylinder(gluNewQuadric(), self.shoe_radius * 1.5, self.shoe_radius, self.player_leg_height - 10, 50, 80)
glPopMatrix()
glPopMatrix()
def bullet_movement(self):
for i in self.all_bullets:
bullet_coord = i["bullet_coord"]
bullet_direction = i["bullet_direction"]
x_move = bullet_coord[0] + self.bullet_speed * bullet_direction[0]
y_move = bullet_coord[1] + self.bullet_speed * bullet_direction[1]
if (-GRID_WIDTH // 2 < x_move < GRID_WIDTH // 2 and -GRID_WIDTH // 2 < y_move < GRID_WIDTH // 2):
bullet_coord[0] = x_move
bullet_coord[1] = y_move
i["bullet_coord"] = bullet_coord
else:
self.all_bullets.remove(i)
def draw_bullets(self):
for i in range(len(self.all_bullets)):
x, y, z = self.all_bullets[i]['bullet_coord']
glPushMatrix()
glTranslatef(x, y, z)
glColor3f(1, 1, 0)
glutSolidCube(self.bullet_size)
glPopMatrix()
glutPostRedisplay()
def draw_capsule(self):
cap_x, cap_y, cap_z = self.capsule_base_position
glPushMatrix()
glTranslatef(cap_x, cap_y, cap_z)
glColor3f(90 / 255, 95 / 255, 100 / 255)
gluCylinder(gluNewQuadric(), self.capsule_radius * 1.5, self.capsule_radius, self.capsule_base_height, 50, 20)
glPopMatrix()
cap_x, cap_y, cap_z = self.capsule_position
glPushMatrix()
glTranslatef(cap_x, cap_y, cap_z)
glColor3f(120 / 255, 255 / 255, 200 / 255)
gluCylinder(gluNewQuadric(), self.capsule_radius, self.capsule_radius, self.capsule_height, 50, 20)
glPopMatrix()
def draw_walls(self, axis_close):
length = GRID_LENGTH // 7
grid_wall_height = length
if axis_close == "+x":
glBegin(GL_QUADS)
glColor3f(4 / 255, 5 / 255, 5 / 255)
glVertex3f(self.floor_right_max, self.floor_front_max, 0)
glVertex3f(self.floor_right_max, self.floor_behind_max, 0)
glColor3f(31 / 255, 33 / 255, 34 / 255)
glVertex3f(self.floor_right_max, self.floor_behind_max, grid_wall_height)
glVertex3f(self.floor_right_max, self.floor_front_max, grid_wall_height)
glEnd()
elif axis_close == "+y":
glBegin(GL_QUADS)
glColor3f(34 / 255, 36 / 255, 37 / 255)
glVertex3f(self.floor_left_max, self.floor_front_max, 0)
glVertex3f(self.floor_right_max, self.floor_front_max, 0)
glColor3f(78 / 255, 82 / 255, 86 / 255)
glVertex3f(self.floor_right_max, self.floor_front_max, grid_wall_height)
glVertex3f(self.floor_left_max, self.floor_front_max, grid_wall_height)
glEnd()
elif axis_close == "-y":
glBegin(GL_QUADS)
glColor3f(4 / 255, 5 / 255, 5 / 255)
glVertex3f(self.floor_left_max, self.floor_behind_max, 0)
glVertex3f(self.floor_right_max, self.floor_behind_max, 0)
glColor3f(31 / 255, 33 / 255, 34 / 255)
glVertex3f(self.floor_right_max, self.floor_behind_max, grid_wall_height)
glVertex3f(self.floor_left_max, self.floor_behind_max, grid_wall_height)
glEnd()
elif axis_close == "-x":
glBegin(GL_QUADS)
glColor3f(34 / 255, 36 / 255, 37 / 255)
glVertex3f(self.floor_left_max, self.floor_front_max, 0)
glVertex3f(self.floor_left_max, self.floor_behind_max, 0)
glColor3f(78 / 255, 82 / 255, 86 / 255)
glVertex3f(self.floor_left_max, self.floor_behind_max, grid_wall_height)
glVertex3f(self.floor_left_max, self.floor_front_max, grid_wall_height)
glEnd()
def draw_lab(self):
# floor
glBegin(GL_QUADS)
glColor3f(135 / 255, 175 / 255, 145 / 255)
glVertex3f(self.floor_left_max, self.floor_front_max, 0)
glColor3f(61 / 255, 66 / 255, 70 / 255)
glVertex3f(self.floor_right_max, self.floor_front_max, 0)
glColor3f(2 / 255, 2 / 255, 9 / 255)
glVertex3f(self.floor_right_max, self.floor_behind_max, 0)
glColor3f(61 / 255, 66 / 255, 70 / 255)
glVertex3f(self.floor_left_max, self.floor_behind_max, 0)
glEnd()
# grid
for i in range(self.floor_left_max - 2, self.floor_right_max + 2, -GRID_LENGTH // 15):
glLineWidth(3)
glBegin(GL_LINES)
glColor3f(110 / 255, 118 / 255, 125 / 255)
glVertex3f(i, self.floor_front_max - 1, 0)
glColor3f(12 / 255, 12 / 255, 9 / 255)
glVertex3f(i, self.floor_behind_max + 1, 0)
glEnd()
for i in range(self.floor_front_max - 2, self.floor_behind_max + 2, -GRID_WIDTH // 15):
glBegin(GL_LINES)
glColor3f(110 / 255, 118 / 255, 125 / 255)
glVertex3f(self.floor_left_max - 1, i, 0)
glColor3f(12 / 255, 12 / 255, 9 / 255)
glVertex3f(self.floor_right_max + 1, i, 0)
glEnd()
# walls
x, y, z = camera_pos
wall_distance_from_camera = {
"+x": math.sqrt(((x + GRID_WIDTH // 2) ** 2) + ((y - 0) ** 2) + ((z - 0) ** 2)),
"+y": math.sqrt(((x - 0) ** 2) + ((y - GRID_LENGTH // 2) ** 2) + ((z - 0) ** 2)),
"-y": math.sqrt(((x - 0) ** 2) + ((y + GRID_LENGTH // 2) ** 2) + ((z - 0) ** 2)),
"-x": math.sqrt(((x - GRID_WIDTH // 2) ** 2) + ((y - 0) ** 2) + ((z - 0) ** 2)),
}
wall_distance_from_camera = dict(
sorted(wall_distance_from_camera.items(), key=lambda item: item[1], reverse=True))
for key, value in wall_distance_from_camera.items():
self.draw_walls(key)
# ENEMY SYSTEM
def get_random_spawn_coordinates(self):
cap_x, cap_y, _ = self.capsule_position
px, py, _ = self.player_spawn_position
exclusion_radius = 500
min_enemy_separation = self.enemy_body_radius * 2.5
min_safe_dist = 200.0
max_attempts = 50
margin = int(self.enemy_body_radius)
# Full arena bounds
x_min = self.floor_right_max + margin
x_max = self.floor_left_max - margin
y_min = self.floor_behind_max + margin
y_max = self.floor_front_max - margin
# LEVELS 1 & 2
if self.game_level in (1, 2):
spawn_padding = 50.0 # how far inside the boundary the enemy first appears
if cap_x >= 0:
opposite_x_wall = self.floor_right_max + spawn_padding
else:
opposite_x_wall = self.floor_left_max - spawn_padding
if cap_y >= 0:
opposite_y_wall = self.floor_behind_max + spawn_padding
else:
opposite_y_wall = self.floor_front_max - spawn_padding
corner_margin = spawn_padding * 2
free_x_min = self.floor_right_max + corner_margin
free_x_max = self.floor_left_max - corner_margin
free_y_min = self.floor_behind_max + corner_margin
free_y_max = self.floor_front_max - corner_margin
for attempt in range(max_attempts):
wall_choice = random.choice(['x_wall', 'y_wall'])
depth_jitter = random.uniform(0.0, 80.0)
if wall_choice == 'x_wall':
if cap_x >= 0:
ex = opposite_x_wall + depth_jitter # inward = +X
else:
ex = opposite_x_wall - depth_jitter # inward = -X
ey = random.uniform(free_y_min, free_y_max)
else:
if cap_y >= 0:
ey = opposite_y_wall + depth_jitter # inward = +Y
else:
ey = opposite_y_wall - depth_jitter # inward = -Y
ex = random.uniform(free_x_min, free_x_max)
dist_to_capsule = math.dist((ex, ey), (cap_x, cap_y))
dist_to_player = math.dist((ex, ey), (px, py))
if dist_to_capsule < min_safe_dist or dist_to_player < min_safe_dist:
continue # too close — reroll
#enemy-vs-enemy spacing check
too_close = False
for existing in self.enemies:
if math.dist((ex, ey), (existing['x'], existing['y'])) < min_enemy_separation:
too_close = True
break
if too_close:
continue
return ex, ey
fallback_x = random.uniform(free_x_min, free_x_max)
fallback_y = (self.floor_behind_max + spawn_padding
if cap_y >= 0
else self.floor_front_max - spawn_padding)
return fallback_x, fallback_y
# LEVEL 3
ex, ey = x_min, y_min
for attempt in range(max_attempts):
ex = random.uniform(x_min, x_max)
ey = random.uniform(y_min, y_max)
dist_to_capsule = math.dist((ex, ey), (cap_x, cap_y))
if dist_to_capsule < exclusion_radius:
continue
too_close_to_enemy = False
for existing_enemy in self.enemies:
if math.dist((ex, ey), (existing_enemy['x'], existing_enemy['y'])) < min_enemy_separation:
too_close_to_enemy = True
break
if too_close_to_enemy:
continue
return int(ex), int(ey)
print(f"[SpawnWarning] Could not find a non-overlapping spawn after {max_attempts} attempts.")
return int(ex), int(ey)
def spawn_enemies(self):
if self.game_level == 1:
# Population threshold: overlapping waves
if len(self.enemies) <= 6:
num_to_spawn = random.randint(8, 10)
for _ in range(num_to_spawn):
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z,
'base_z': self.enemy_base_z,
'run_cycle': random.uniform(0.0, 5.0),
'type': 'normal',
'body_r': self.enemy_body_radius,
'head_r': self.enemy_head_radius,
'color': (0.0, 125 / 255.0, 140 / 255.0)
})
self.last_spawn_time = time.time()
print(f"[L1 Wave] Population threshold hit (<=6). Spawned {num_to_spawn} new enemies. Total: {len(self.enemies)}")
# High-density continuous: 5 every 5s
current_time = time.time()
if current_time - self.last_spawn_time >= 5.0:
for _ in range(5):
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z,
'base_z': self.enemy_base_z,
'run_cycle': random.uniform(0.0, 5.0),
'type': 'normal',
'body_r': self.enemy_body_radius,
'head_r': self.enemy_head_radius,
'color': (0.0, 125 / 255.0, 140 / 255.0)
})
self.last_spawn_time = current_time
print(f"[L1 Spawn] Group of 5 enemies spawned from wall. Active: {len(self.enemies)}")
# Special enemy: every 20 normal kills
if self.normal_enemy_kills_for_special >= 20:
self.normal_enemy_kills_for_special -= 20
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z * 1.5,
'base_z': self.enemy_base_z * 1.5,
'run_cycle': random.uniform(0.0, 5.0),
'type': 'special',
'body_r': self.enemy_body_radius * 1.5,
'head_r': self.enemy_head_radius * 1.5,
'color': (1.0, 0.0, 0.0)
})
print(f"[L1 Spawn] Special enemy spawned after 20 kills! Active: {len(self.enemies)}")
elif self.game_level == 2:
L2_NORMAL_COLOR = (27 / 255.0, 196 / 255.0, 118 / 255.0)
if len(self.enemies) <= 12:
num_to_spawn = random.randint(10, 15)
for _ in range(num_to_spawn):
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z,
'base_z': self.enemy_base_z,
'run_cycle': random.uniform(0.0, 5.0),
'type': 'normal',
'body_r': self.enemy_body_radius,
'head_r': self.enemy_head_radius,
'color': L2_NORMAL_COLOR
})
self.last_spawn_time = time.time()
print(f"[L2 Wave] Population threshold hit (<=12). Spawned {num_to_spawn} new enemies. Total: {len(self.enemies)}")
current_time = time.time()
if current_time - self.last_spawn_time >= 5.0:
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z,
'base_z': self.enemy_base_z,
'run_cycle': random.uniform(0.0, 5.0),
'type': 'normal',
'body_r': self.enemy_body_radius,
'head_r': self.enemy_head_radius,
'color': L2_NORMAL_COLOR
})
self.last_spawn_time = current_time
print(f"[L2 Spawn] Timed enemy spawned. Active: {len(self.enemies)}")
if self.normal_enemy_kills_for_special >= 10:
self.normal_enemy_kills_for_special -= 10
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z * 1.5,
'base_z': self.enemy_base_z * 1.5,
'run_cycle': random.uniform(0.0, 5.0),
'type': 'special',
'body_r': self.enemy_body_radius * 1.5,
'head_r': self.enemy_head_radius * 1.5,
'color': (1.0, 0.0, 0.0)
})
print(f"[L2 Spawn] Special enemy spawned after 10 kills! Active: {len(self.enemies)}")
elif self.game_level == 3:
if not self.boss_spawned and len(self.enemies) == 0:
ex, ey = self.get_random_spawn_coordinates()
self.enemies.append({
'x': ex, 'y': ey,
'z': self.enemy_base_z * 2.5,
'base_z': self.enemy_base_z * 2.5,
'run_cycle': 0.0,
'type': 'boss',
'body_r': self.enemy_body_radius * 2.5,
'head_r': self.enemy_head_radius * 2.5,
'color': (94 / 255.0, 4 / 255.0, 135 / 255.0),
'health': 50,
'max_health': 50
})
self.boss_spawned = True
print("Final Boss Spawned!")
def enemy_movement(self):
target_x, target_y, _ = self.capsule_position
player_x, player_y, _ = self.player_spawn_position
enemies_to_remove = []
for e_idx, enemy in enumerate(self.enemies):
dx = target_x - enemy['x']
dy = target_y - enemy['y']
distance = math.sqrt(dx ** 2 + dy ** 2)
if distance < (self.capsule_radius + enemy['body_r']):
enemies_to_remove.append(e_idx)
self.capsule_health = max(0, self.capsule_health - 1)
print(f"Capsule hit! Remaining health: {self.capsule_health}")
continue
dx_p = player_x - enemy['x']
dy_p = player_y - enemy['y']
distance_p = math.sqrt(dx_p ** 2 + dy_p ** 2)
if distance_p < (self.player_width + enemy['body_r']):
enemies_to_remove.append(e_idx)
if not self.cheat_mode_active:
self.player_health = max(0, self.player_health - 1)
print(f"Player hit! Remaining health: {self.player_health}")
continue
if enemy['type'] == 'boss':
margin = int(enemy['body_r'])
x_min = self.floor_right_max + margin
x_max = self.floor_left_max - margin
base_y = self.floor_behind_max + margin + 100
if 'boss_direction_x' not in enemy:
enemy['boss_direction_x'] = 1
enemy['y'] = base_y
base_sweep_speed = self.enemy_speed * 1.5
enemy['run_cycle'] += 0.05
speed_wobble = math.cos(enemy['run_cycle'] * 1.5) * base_sweep_speed * 1.2
new_x = enemy['x'] + (enemy['boss_direction_x'] * base_sweep_speed) + speed_wobble
if new_x > x_max:
new_x = x_max
enemy['boss_direction_x'] = -1
elif new_x < x_min:
new_x = x_min
enemy['boss_direction_x'] = 1
enemy['x'] = new_x
depth_wobble = math.sin(enemy['run_cycle'] * 2.0) * 20.0
enemy['y'] = base_y + depth_wobble
enemy['z'] = enemy['base_z'] + abs(math.sin(enemy['run_cycle'] * 2)) * 20
else:
if distance > 0:
enemy['x'] += (dx / distance) * self.enemy_speed
enemy['y'] += (dy / distance) * self.enemy_speed
enemy['run_cycle'] += 0.08
bounce_height = abs(math.sin(enemy['run_cycle'])) * 15
enemy['z'] = enemy['base_z'] + bounce_height
for i in sorted(enemies_to_remove, reverse=True):
if i < len(self.enemies):
self.enemies.pop(i)
def bullet_enemy_collision(self):
bullets_to_remove = []
enemies_to_remove = []
for b_idx, bullet in enumerate(self.all_bullets):
bx, by, bz = bullet['bullet_coord']
for e_idx, enemy in enumerate(self.enemies):
if e_idx in enemies_to_remove:
continue
dist_2d = math.sqrt((bx - enemy['x']) ** 2 + (by - enemy['y']) ** 2)
if dist_2d < (enemy['body_r'] + self.bullet_size):
bullets_to_remove.append(b_idx)
# Boss: take damage instead of dying instantly
if enemy['type'] == 'boss':
enemy['health'] -= 1
print(f"[Boss] Hit! Health: {enemy['health']}/{enemy['max_health']}")
if enemy['health'] <= 0:
enemies_to_remove.append(e_idx)
self.total_kills += 1
self.game_won = True
self.game_over = True
print("[Boss] DEFEATED! YOU WIN!")
break
# Normal/special enemies: die in one shot
enemies_to_remove.append(e_idx)
if enemy['type'] == 'special':
if self.player_health < 5:
self.player_health += 1
print(f"Special Enemy Killed! Health increased to {self.player_health}")
else:
self.normal_enemies_killed += 1
#counter for special spawns
self.normal_enemy_kills_for_special += 1
self.total_kills += 1
if self.game_level == 1 and self.total_kills >= self.level_1_kill_limit:
self.level_1_limit_crossed = True
elif self.game_level == 2 and self.total_kills >= self.level_2_kill_limit:
self.level_2_limit_crossed = True
break
for i in sorted(bullets_to_remove, reverse=True):
if i < len(self.all_bullets):
self.all_bullets.pop(i)
for i in sorted(enemies_to_remove, reverse=True):
if i < len(self.enemies):
self.enemies.pop(i)
def display_kill_count(self):
limit = self.level_1_kill_limit if self.game_level == 1 else self.level_2_kill_limit
kill_text = f"Level {self.game_level} total Kills: {self.total_kills}/{limit}"
self.draw_text(window_width - 250, window_height - 60, kill_text)
# ENEMY DRAWING
def draw_enemies(self):
for enemy in self.enemies:
ex, ey, ez = enemy['x'], enemy['y'], enemy['z']
cr, cg, cb = enemy['color']
# Doc 1: Boss scale adjustment
if enemy['type'] == 'boss':
br = enemy['body_r'] * 1.18
hr = enemy['head_r'] * 1.18
else:
br = enemy['body_r']
hr = enemy['head_r']
glPushMatrix()
glTranslatef(ex, ey, ez)
#Yaw orientation
if enemy['type'] == 'boss':
px, py, _ = self.player_spawn_position
dx = px - ex
dz = py - ey
else:
cap_x, cap_y, _ = self.capsule_position
dx = cap_x - ex
dz = cap_y - ey
raw_yaw = math.degrees(math.atan2(dz, dx))
yaw_deg = raw_yaw + 90.0
glRotatef(yaw_deg, 0, 0, 1)
# Body
glColor3f(cr, cg, cb)
gluSphere(gluNewQuadric(), br, 30, 30)
# Head
glPushMatrix()
glTranslatef(0, 0, br + hr - 5)
glColor3f(cr * 0.8, cg * 0.8, cb * 0.8)
gluSphere(gluNewQuadric(), hr, 30, 30)
# Horns
if enemy['type'] == 'boss':
h_base_r = hr * 0.4
h_top_r = hr * 0.1
h_height = hr * 1.5
else:
h_base_r = hr * 0.22
h_top_r = 0.01
h_height = hr * 0.8
glPushMatrix()
glTranslatef(-hr * 0.5, 0, hr * 0.6)
glRotatef(-40, 0, 1, 0)
glRotatef(-10, 1, 0, 0)
gluCylinder(gluNewQuadric(), h_base_r, h_top_r, h_height, 15, 5)
glPopMatrix()
glPushMatrix()
glTranslatef(hr * 0.5, 0, hr * 0.6)
glRotatef(40, 0, 1, 0)
glRotatef(-10, 1, 0, 0)
gluCylinder(gluNewQuadric(), h_base_r, h_top_r, h_height, 15, 5)
glPopMatrix()
glPopMatrix()
# Limb parameters
shoulder_offset = br * 1.1
shoulder_z = br * 0.4
upper_arm_len = br * 1.1
arm_r_top = br * 0.22
arm_r_bot = br * 0.15
forearm_r_top = br * 0.18
forearm_r_bot = br * 0.11
hand_r = hr * 0.35
elbow_z = -upper_arm_len * 0.65
elbow_y = -upper_arm_len * 0.75
if self.game_level == 3:
# Boss: full articulated arms
glPushMatrix()
glColor3f(cr * 0.9, cg * 0.9, cb * 0.9)
glTranslatef(-shoulder_offset, 0, shoulder_z)
gluSphere(gluNewQuadric(), arm_r_top * 1.2, 20, 20)
glPopMatrix()
glPushMatrix()
glColor3f(cr * 0.9, cg * 0.9, cb * 0.9)
glTranslatef(-shoulder_offset, 0, shoulder_z)
glRotatef(90, 1, 0, 0)
glRotatef(45, 1, 0, 0)
gluCylinder(gluNewQuadric(), arm_r_top, arm_r_bot, upper_arm_len, 20, 5)
glPopMatrix()
glPushMatrix()
glColor3f(cr * 0.85, cg * 0.85, cb * 0.85)
glTranslatef(-shoulder_offset, elbow_y, elbow_z)
glRotatef(90, 1, 0, 0)