-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
1643 lines (1259 loc) · 60.3 KB
/
game.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
# a tower defence game based on direction selection and special moves on different units. You place units to defend against waves of enemies
# written by: Ben
import simplegui
import math
import random
import time
frames_passed = 0
hp = 3
currency = 0
delta_time = 0
previous_time = time.time()
canvas_copy = None
game_screen = 'menu'
WIDTH = 1200
HEIGHT = 950
# for selection states, and letting the code know, wut step of the placement process the player is at
state = 'select'
character_selected = ''
info_selected = None
pseudo_place = None
# list of everything on field
placed_units = []
enemies_spawned = []
#used for seeing whos on the field
placed_units_names = []
# 'nero', 'ishtar', 'mashu', 'fox', 'healer'
squad = []
Sbutton = None
Rbutton = None
# menu buttons
selection = None
journal = None
controls = None
# stage selection
stage1 = None
stage2 = None
stage3 = None
# pause menu buttons
menu = None
stage_selection = None
# end lvl buttons
stage = None
menu2 = None
current_music = 'menu'
music = {
'stage1': simplegui.load_sound('https://vgmsite.com/soundtracks/nier-automata-original-soundtrack/wbcxjbpx/1-02%20City%20of%20Ruins%20-%20Rays%20of%20Light.mp3'),
'stage2': simplegui.load_sound('https://vgmsite.com/soundtracks/nier-automata-original-soundtrack/iclchkkb/1-04%20Memories%20of%20Dust.mp3'),
'stage3': simplegui.load_sound('https://vgmsite.com/soundtracks/nier-automata-original-soundtrack/fcuncwqu/1-08%20A%20Beautiful%20Song.mp3'),
'menu': simplegui.load_sound('https://vgmsite.com/soundtracks/nier-automata-original-soundtrack/sqvubkiu/2-15%20Weight%20of%20the%20World.mp3')
}
# pictures for the 2 buttons next to the character info screen
retreat = simplegui.load_image('https://imgur.com/ko0vthW.png')
special = simplegui.load_image('https://imgur.com/NLkt6LB.png')
# set lvls and trails and enemy spawns
stages = {
'stage1': {
'image': simplegui.load_image('https://imgur.com/R2nt83K.png'),
'trail': [[0, 250], [350, 250], [350, 450], [1150, 450]],
'spawn': [['bug', 100], ['bug', 600], ['bug', 900], ['bug', 1400], ['spider', 1700], ['bug', 2000], ['spider', 2400], ['bug', 2800], ['bug', 3400],
['bug', 4000], ['bug', 4100], ['spider', 4150], ['bug', 4200], ['bug', 5000] ,['spider', 5050], ['bug', 5100], ['spider', 5150], ['bug', 5200],
['spider', 5250], ['spider', 5280], ['spider', 5320]]
},
'stage2': {
'image': simplegui.load_image('https://imgur.com/FnH1MsK.png'),
'trail': [[0, 150], [350, 150], [350, 650], [650, 650], [650, 350], [1200, 350]],
'spawn': [['bug', 200], ['fly', 1050], ['bug', 1500], ['fly', 2200], ['bug', 2500], ['spider', 2700], ['fly', 3000], ['laser', 3500],
['bug', 3800], ['bug', 4100], ['fly', 4400], ['kamikaze', 4500], ['fly', 4700], ['bug', 4900], ['laser', 5100], ['fly', 5200],
['fly', 5300], ['fly', 5400], ['kamikaze', 5450], ['kamikaze', 5600], ['laser', 5700], ['laser', 5780], ['fly', 5800]]
},
'stage3': {
'image': simplegui.load_image('https://imgur.com/sPBLLdr.png'),
'trail': [[150, 0], [150, 350], [350, 350], [350, 150], [550, 150], [550, 650], [950, 650], [950, 250], [1200, 250]],
'spawn': [['bug', 200], ['fly', 500], ['spider', 850], ['caster', 1300], ['bug', 2000], ['bug', 2060], ['spider', 2120], ['fly', 2200], ['kamikaze', 2800],
['caster', 3500], ['laser', 3600], ['knight', 3700], ['spider', 3750], ['kamikaze', 3850], ['bug', 3900], ['caster', 5000], ['caster', 5100],
['radiance', 5150], ['knight', 5200], ['knight', 5250], ['kamikaze', 5300], ['kamikaze', 5350], ['fly', 5400], ['laser', 5500],
['knight', 6000], ['caster', 6050], ['kamikaze', 6100], ['kamikaze', 6120], ['kamikaze', 6140], ['fly', 6200], ['laser', 6250], ['fly', 6300], ['kamikaze', 6320],
['kamikaze', 6350], ['knight', 6400]]
}
}
# used for figuring which stage is selected and the enemy count
currentlvl = None
currentlvl_left = None
enemies_info = {
'bug': {
'img': simplegui.load_image('https://imgur.com/oHfBPdq.png'),
'info': [110, 83],
'size': [55, 41.5]
},
'fly': {
'img': simplegui.load_image('https://imgur.com/BXU7AwU.png'),
'info': [124, 200],
'size': [62, 100]
},
'spider': {
'img': simplegui.load_image('https://imgur.com/nvswHO4.png'),
'info': [130, 91],
'size': [65, 45.5]
},
'laser': {
'img': simplegui.load_image('https://imgur.com/npLNtr0.png'),
'info': [250, 274],
'size': [100, 124]
},
'caster': {
'img': simplegui.load_image('https://imgur.com/U0RLtRp.png'),
'info': [98, 193],
'size': [49, 96]
},
'kamikaze': {
'img': simplegui.load_image('https://imgur.com/98dPXJ7.png'),
'info': [475, 289],
'size': [75, 37.8]
},
'radiance': {
'img': simplegui.load_image('https://imgur.com/bduE2rf.png'),
'info': [310, 450],
'size': [290, 430]
},
'knight': {
'img': simplegui.load_image('https://imgur.com/BcmxYpU.png'),
'info': [369, 394],
'size': [123, 131.3]
}
}
units_info = {
'nero': {
'img': simplegui.load_image('https://imgur.com/RQhqLuC.png'),
'special': simplegui.load_image('https://imgur.com/mv0t3Dy.png'),
'pseudo': simplegui.load_image('https://imgur.com/lrTqNGD.png'),
'icon': simplegui.load_image('https://imgur.com/ApYDvNL.png'),
'icon-shaded': simplegui.load_image('https://imgur.com/GvhCpiF.png'),
'icon-info': [880, 880],
'info': [1400, 650],
'size': [200, 100],
'cost': 3,
'info-pic': simplegui.load_image('https://imgur.com/FZo2aNQ.png')
},
'fox': {
'img': simplegui.load_image('https://imgur.com/ovqOwge.png'),
'special': simplegui.load_image('https://imgur.com/T2VqmWY.png'),
'pseudo': simplegui.load_image('https://imgur.com/aeEfchA.png'),
'icon': simplegui.load_image('https://imgur.com/mjuSNgJ.png'),
'icon-shaded': simplegui.load_image('https://imgur.com/DuWfYhO.png'),
'icon-info': [150, 150],
'info': [1600, 1600],
'size': [200, 200],
'cost': 12,
'info-pic': simplegui.load_image('https://imgur.com/eOvRThN.png')
},
'mashu': {
'img': simplegui.load_image('https://imgur.com/BpewDSD.png'),
'special': simplegui.load_image('https://imgur.com/Lr7XvPO.png'),
'pseudo': simplegui.load_image('https://imgur.com/NogI2oZ.png'),
'icon': simplegui.load_image('https://imgur.com/AlDF1q3.png'),
'icon-shaded': simplegui.load_image('https://imgur.com/XsydQla.png'),
'icon-info': [200, 200],
'info': [1000, 1000],
'size': [100, 100],
'cost': 5,
'info-pic': simplegui.load_image('https://imgur.com/muqrCqG.png')
},
'ishtar': {
'img': simplegui.load_image('https://imgur.com/Y7AI1kU.png'),
'special': simplegui.load_image('https://imgur.com/36ULA8o.png'),
'pseudo': simplegui.load_image('https://imgur.com/XUNsP4F.png'),
'icon': simplegui.load_image('https://imgur.com/Uvh0OSb.png'),
'icon-shaded': simplegui.load_image('https://imgur.com/ejcAeWF.png'),
'icon-info': [200, 200],
'info': [1200, 1200],
'size': [140, 140],
'cost': 8,
'info-pic': simplegui.load_image('https://imgur.com/AAqMCRP.png')
},
'healer': {
'img': simplegui.load_image('https://imgur.com/T0q7xbH.png'),
'special': simplegui.load_image('https://imgur.com/HctrdsL.png'),
'pseudo': simplegui.load_image('https://imgur.com/Hpp2IqM.png'),
'icon': simplegui.load_image('https://imgur.com/1DAWD1p.png'),
'icon-shaded': simplegui.load_image('https://imgur.com/7Pc7Nod.png'),
'icon-info': [150, 150],
'info': [1000, 1000],
'size': [110, 110],
'cost': 6,
'info-pic': simplegui.load_image('https://imgur.com/QqN0OFa.png')
}
}
screen_images = {
'start_screen': simplegui.load_image('https://imgur.com/fTgqrgJ.png'),
'lvl-selection': simplegui.load_image('https://imgur.com/hhunJrA.png'),
'pause': simplegui.load_image('https://imgur.com/VjxH90U.png'),
'end': simplegui.load_image('https://imgur.com/lSU0sZT.png'),
'controls': simplegui.load_image('https://imgur.com/4eWoY6I.png'),
'journal': simplegui.load_image('https://imgur.com/3vhjSeW.png'),
'lose': simplegui.load_image('https://imgur.com/9Rx9LXq.png')
}
# used to reset stages
def reset():
global frames_passed
global hp
global currency
global delta_time
global previous_time
global placed_units
global enemies_spawned
global placed_units_names
frames_passed = 0
hp = 3
currency = 0
delta_time = 0
previous_time = time.time()
placed_units = []
enemies_spawned = []
placed_units_names = []
# creates the 4 directional ranges for each unit
def create_range(offset, unit):
left = []
# left
for x in range(offset[0] + 1):
x_offset = int(math.ceil(unit.position[0] / 100.0)) - x
y_offset = int(math.ceil(unit.position[1] / 100.0))
left.append([x_offset, y_offset])
if offset[1] != 0:
for y in range(1, offset[1] + 1):
y_offset = int(math.ceil(unit.position[1] / 100.0)) - y
left.append([x_offset, y_offset])
y_offset2 = int(math.ceil(unit.position[1] / 100.0)) + y
left.append([x_offset, y_offset2])
right = []
# left
for x in range(offset[0] + 1):
x_offset = int(math.ceil(unit.position[0] / 100.0)) + x
y_offset = int(math.ceil(unit.position[1] / 100.0))
right.append([x_offset, y_offset])
if offset[1] != 0:
for y in range(1, offset[1] + 1):
y_offset = int(math.ceil(unit.position[1] / 100.0)) - y
right.append([x_offset, y_offset])
y_offset2 = int(math.ceil(unit.position[1] / 100.0)) + y
right.append([x_offset, y_offset2])
up = []
# left
for y in range(offset[0] + 1):
y_offset = int(math.ceil(unit.position[1] / 100.0)) - y
x_offset = int(math.ceil(unit.position[0] / 100.0))
up.append([x_offset, y_offset])
if offset[1] != 0:
for x in range(1, offset[1] + 1):
x_offset = int(math.ceil(unit.position[0] / 100.0)) - x
up.append([x_offset, y_offset])
x_offset2 = int(math.ceil(unit.position[0] / 100.0)) + x
up.append([x_offset2, y_offset])
down = []
# left
for y in range(offset[0] + 1):
y_offset = int(math.ceil(unit.position[1] / 100.0)) + y
x_offset = int(math.ceil(unit.position[0] / 100.0))
down.append([x_offset, y_offset])
if offset[1] != 0:
for x in range(1, offset[1] + 1):
x_offset = int(math.ceil(unit.position[0] / 100.0)) - x
down.append([x_offset, y_offset])
x_offset2 = int(math.ceil(unit.position[0] / 100.0)) + x
down.append([x_offset2, y_offset])
return [left, right, up, down]
def draw_unit(itself, canvas, img, img_info, size):
canvas.draw_image(img, (img_info[0] / 2, img_info[1] / 2), (img_info[0], img_info[1]), itself.position, size)
# draws hp
max_hp_copy = itself.max_hp
hp_copy = itself.hp
canvas.draw_polygon([[itself.position[0] - 30, itself.position[1] + 30],
[itself.position[0] + 30, itself.position[1] + 30],
[itself.position[0] + 30, itself.position[1] + 35],
[itself.position[0] - 30, itself.position[1] + 35]], 1, 'Grey', 'Grey')
canvas.draw_polygon([[itself.position[0] - 30, itself.position[1] + 30],
[itself.position[0] + 30 - ((1 - (hp_copy/max_hp_copy)) * 60), itself.position[1] + 30],
[itself.position[0] + 30 - ((1 - (hp_copy/max_hp_copy)) * 60), itself.position[1] + 35],
[itself.position[0] - 30, itself.position[1] + 35]], 1, 'Green', 'Green')
max_special_gauge_copy = itself.max_special_gauge
special_gauge_copy = itself.special_gauge
canvas.draw_polygon([[itself.position[0] - 30, itself.position[1] + 35],
[itself.position[0] + 30, itself.position[1] + 35],
[itself.position[0] + 30, itself.position[1] + 40],
[itself.position[0] - 30, itself.position[1] + 40]], 1, 'Grey', 'Grey')
canvas.draw_polygon([[itself.position[0] - 30, itself.position[1] + 35],
[itself.position[0] + 30 - ((1 - (special_gauge_copy/max_special_gauge_copy)) * 60), itself.position[1] + 35],
[itself.position[0] + 30 - ((1 - (special_gauge_copy/max_special_gauge_copy)) * 60), itself.position[1] + 40],
[itself.position[0] - 30, itself.position[1] + 40]], 1, 'orange', 'orange')
# sets the 2 buttons and depending on which side, will draw the unit info on the oppposite side of the unit placed
def draw_otherInfo(itself, canvas):
global Rbutton
global Sbutton
if itself.position[0] < 600:
x1, x2, x3, x4 = 800, 1100, 830, 1000
xS, xR = 750, 850
Rbutton = Button([xR, 150], 75, 75)
Sbutton = Button([xS, 150], 75, 75)
else:
x1, x2, x3, x4 = 100, 400, 130, 200
xS, xR = 450, 350
Rbutton = Button([xR, 150], 75, 75)
Sbutton = Button([xS, 150], 75, 75)
for sqr in itself.real_range:
x_sqr = sqr[0] * 100
y_sqr = sqr[1] * 100
canvas.draw_polygon([[x_sqr, y_sqr], [x_sqr - 100, y_sqr], [x_sqr - 100, y_sqr - 100], [x_sqr, y_sqr - 100]], 1, 'rgb(255,165,0)', 'rgb(255,165,0, .5)')
canvas.draw_image(units_info[itself.name]['info-pic'], (512 / 2, 724 / 2), (512, 724), [x4, 400], (409.6, 579.2))
canvas.draw_image(retreat, (100 / 2, 100 / 2), (100,100), [xR, 150], (75, 75))
canvas.draw_image(special, (100 / 2, 100 / 2), (100,100), [xS, 150], (75, 75))
return x1, x2, x3, x4
def spawn_enemy(stage_lvl):
global frames_passed
global bug
global fly
# hp, damage, move_speed, position, range, enemy_type, enemy_name, currency
for i in stage_lvl:
if frames_passed == i[1]:
if i[0] == 'bug':
new_enemy = Enemy(4, 1, 2, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 30, 'ground', 'bug', 1)
elif i[0] == 'fly':
new_enemy = Enemy(4, 1, 2, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 100, 'air', 'fly', 1)
elif i[0] == 'spider':
new_enemy = Enemy(2, 1, 2.5, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 50, 'ground', 'spider', 1)
elif i[0] == 'laser':
new_enemy = Enemy(8, 1, 1, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 150, 'ground', 'laser', 1)
elif i[0] == 'radiance':
new_enemy = Enemy(30, 3, .5, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 150, 'air', 'radiance', 1)
new_enemy.aoeOrNot = True
new_enemy.aoe_range = 150
elif i[0] == 'caster':
new_enemy = Enemy(10, 2, 1, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 150, 'ground', 'caster', 1)
new_enemy.aoeOrNot = True
elif i[0] == 'kamikaze':
new_enemy = Enemy(4, 4, 5, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 80, 'ground', 'kamikaze', 1)
new_enemy.explodeOrNot = True
elif i[0] == 'knight':
new_enemy = Enemy(12, 2, 1, [stages[currentlvl]['trail'][0][0], stages[currentlvl]['trail'][0][1]], 80, 'ground', 'knight', 1)
enemies_spawned.append(new_enemy)
# finds trail, so melee units can only be placed on trails, and ranged units must be placed everywhere but trail
def find_can_place(stage_real):
stage = stage_real.copy()
stage = [[math.ceil(i[0]/100),math.ceil(i[1]/100)] for i in stage]
stage[0] = [stage[0][0] + 1, stage[0][1]]
part = 1
temp = stage[0].copy()
ground = []
ground.append(stage[0])
# [[1, 3], [5, 3], [5, 5], [12, 5]]
while temp != stage[-1]:
if part != len(stage):
if stage[part][0] - temp[0] == 0 and stage[part][1] - temp[1] == 0:
part += 1
else:
if stage[part][0] - temp[0] > 0:
temp[0] += 1
elif stage[part][0] - temp[0] < 0:
temp[0] -= 1
elif stage[part][1] - temp[1] > 0:
temp[1] += 1
elif stage[part][1] - temp[1] < 0:
temp[1] -= 1
ground.append(temp.copy())
return ground
# finds which units are on the field
def update_onfield():
global placed_units_names
temp = []
for i in placed_units:
temp.append(i.name)
placed_units_names = temp
# draws the bottom icons based on your squad characters
def draw_icon(canvas):
global state
global currency
global character_selected
circle = simplegui.load_image('https://imgur.com/jt1aZeN.png')
def draw_only_icon(icon, info, counter, counter2, canvas):
canvas.draw_image(icon, (info[0] / 2, info[1] / 2),
(info[0], info[1]), [counter, counter2], (150, 150))
#draws icons of each unit
counter = 1125
counter2 = 875
for char in squad:
if char == character_selected and state != 'select':
draw_only_icon(circle, [150, 150], counter, counter2, canvas)
elif char not in placed_units_names and currency >= units_info[char]['cost']:
draw_only_icon(units_info[char]['icon'], units_info[char]['icon-info'], counter, counter2, canvas)
elif char in placed_units_names or currency < units_info[char]['cost']:
draw_only_icon(units_info[char]['icon-shaded'], units_info[char]['icon-info'], counter, counter2, canvas)
counter -= 150
# buttons for all the icons of charactesr at the bottom
def create_button():
counter = 1125
counter2 = 875
for char in squad:
units_info[char]['button'] = Button([counter, counter2], 150, 150)
counter -= 150
# needed for displaying important info within gameplay
def draw_hud(canvas):
global currency
global hp
# currency
currency_img = simplegui.load_image('https://imgur.com/KpoRptB.png')
canvas.draw_image(currency_img, (420 / 2, 420 / 2), (420, 420), [1120, 760], (40, 40))
canvas.draw_polygon([(1200, 780), (1200, 740), (1100, 740), (1100, 780)], 3, 'white')
canvas.draw_text(str(currency), (1150, 770), 30, 'white', 'sans-serif')
# hp
hp_img = simplegui.load_image('https://imgur.com/59bDKCf.png')
canvas.draw_polygon([(600, 40), (600, 2), (700, 2), (700, 40)], 3, 'white', 'black')
canvas.draw_image(hp_img, (200 / 2, 200 / 2), (200, 200), [625, 20], (40, 40))
canvas.draw_text(str(hp), [640, 32], 30, 'white', 'sans-serif')
# enemies left
canvas.draw_polygon([(500, 40), (500, 2), (600, 2), (600, 40)], 3, 'white', 'black')
canvas.draw_text(str(currentlvl_left) + '/' + str(len(stages[currentlvl]['spawn'])), [520, 32], 30, 'white', 'sans-serif')
def block_attack_units():
global placed_units
global enemies_spawned
global currency
global currentlvl_left
# for each unit, checks if there is enemy or not.
for units in placed_units:
for enemy in enemies_spawned:
if [int(math.ceil(enemy.position[0] / 100.0)), int(math.ceil(enemy.position[1] / 100.0))] not in units.real_range and enemy in units.blocking:
units.blocking.remove(enemy)
if [int(math.ceil(enemy.position[0] / 100.0)), int(math.ceil(enemy.position[1] / 100.0))] in units.real_range and enemy not in units.blocking and enemy.enemy_type in units.type_target:
units.blocking.append(enemy)
# kill enemies and reset their bools and unit values
for units in placed_units:
units.attack()
# add values after kill (currency)
for enemy in enemies_spawned:
if enemy.hp <= 0:
currentlvl_left -= 1
if units.special_gauge < units.max_special_gauge:
units.special_gauge += 1
enemies_spawned.remove(enemy)
if currency < 15:
currency += enemy.currency
for unit in placed_units:
if enemy in unit.blocking:
unit.blocking.remove(enemy)
if enemy in unit.under_attack:
unit.under_attack.remove(enemy)
def block_attack_enemies():
global placed_units
global enemies_spawned
# takes in enemy, and their ranges. And lets them attack towers
for enemy in enemies_spawned:
for units in placed_units:
if inRange(enemy, units) and enemy not in units.under_attack and len(units.under_attack) < units.can_block:
units.under_attack.append(enemy)
enemy.already_attacking = True
elif len(placed_units) == 0:
enemy.already_attacking = False
elif not inRange(enemy, units) and enemy in units.under_attack and enemy.already_attacking:
units.under_attack.remove(enemy)
enemy.already_attacking = False
# enemy attack towers, and reset their values
for enemy in enemies_spawned:
enemy.reached_end(stages[currentlvl]['trail'])
found = False
for units in placed_units:
if enemy in units.under_attack:
found = True
enemy.attack(units)
for unit in placed_units:
if unit.hp <= 0:
placed_units.remove(unit)
for enemy in units.under_attack:
enemy.already_blocked = False
enemy.already_attacking = False
enemy.moveOrNot = not found
if enemy.enemy_name == 'spider':
enemy.moveOrNot = True
if enemy.moveOrNot:
enemy.move(stages[currentlvl]['trail'])
def draw_aoe(middle, radius):
global canvas_copy
canvas_copy.draw_circle(middle, radius, 2, 'blue', 'blue')
def draw_attack_aoe(middle, radius):
global canvas_copy
canvas_copy.draw_circle(middle, radius, 2, 'red', 'red')
def draw_attack(itself, target):
global canvas_copy
timer_started = frames_passed
canvas_copy.draw_line(itself.position, target.position, 7, 'blue')
def draw_enemy_attack(itself, target):
global canvas_copy
timer_started = frames_passed
canvas_copy.draw_line(itself.position, target.position, 4, 'red')
def draw_heal(target):
global canvas_copy
heal = simplegui.load_image('https://imgur.com/QI8IJpf.png')
canvas_copy.draw_image(heal, (500 / 2, 500 / 2), (500, 500), target.position, (60, 60))
# detect enemy, if in range
def inRange(obj1, obj_found):
a = (obj1.position[0] - obj_found.position[0]) ** 2
b = (obj1.position[1] - obj_found.position[1]) ** 2
distance = math.sqrt(a + b)
if distance < obj1.range:
return True
else:
return False
def cancel_selection():
global state
global character_selected
global pseudo_place
state = 'select'
character_selected = ''
pseudo_place = None
# checks if your either dead or you completed the level
def game_over():
global game_screen
global hp
global frame
if hp <= 0:
frame.set_mouseclick_handler(start_mouse_handler)
game_screen = 'lose'
frame.set_draw_handler(death_screen)
elif currentlvl_left == 0:
frame.set_draw_handler(end_lvl)
game_screen = 'end lvl'
frame.set_mouseclick_handler(start_mouse_handler)
# draw functions for each screen
def end_lvl(canvas):
canvas.draw_image(screen_images['end'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def pause(canvas):
canvas.draw_image(screen_images['pause'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def lvl_selection(canvas):
canvas.draw_image(screen_images['lvl-selection'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def start_screen(canvas):
music[current_music].play()
frame.set_mouseclick_handler(start_mouse_handler)
canvas.draw_image(screen_images['start_screen'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def death_screen(canvas):
canvas.draw_image(screen_images['lose'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def control_screen(canvas):
canvas.draw_image(screen_images['controls'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def journal_screen(canvas):
canvas.draw_image(screen_images['journal'], (1200 / 2, 950 / 2), (1200, 950), (1200 / 2, 950 / 2), (1200, 950))
def create_menu_buttons():
global selection
global journal
global controls
global stage1
global stage2
global stage3
global menu
global stage_selection
global stage
global menu2
# menu buttons
selection = Button([600, 440], 600, 100)
controls = Button([585, 544], 300, 90)
journal = Button([585, 644], 300, 90)
# stage selection
stage1 = Button([592, 287], 300, 100)
stage2 = Button([592, 435], 300, 100)
stage3 = Button([592, 580], 300, 100)
# pause menu buttons
menu = Button([600, 525], 300, 100)
stage_selection = Button([600, 670], 300, 100)
# end lvl buttons
stage = Button([610, 565], 300, 100)
menu2 = Button([600, 675], 300, 100)
# simple enemy
class Enemy():
def __init__(self, hp, damage, move_speed, position, range, enemy_type, enemy_name, currency):
self.hp = hp
self.max_hp = hp
self.damage = damage
self.move_speed = move_speed
self.max_speed = move_speed
self.position = position
self.stage_part = 1
self.range = range
self.moveOrNot = True
self.already_attacking = False
self.already_blocked = False
self.enemy_type = enemy_type
self.enemy_name = enemy_name
self.currency = currency
self.timer = 0
self.aoeOrNot = False
self.aoe_range = 100
self.explode_range = 300
self.explodeOrNot = False
def move(self, stage):
if self.stage_part != len(stage):
if stage[self.stage_part][0] - self.position[0] == 0 and stage[self.stage_part][1] - self.position[1] == 0:
self.position[1] = stage[self.stage_part][1]
self.position[0] = stage[self.stage_part][0]
self.stage_part += 1
elif abs(stage[self.stage_part][0] - self.position[0]) < self.move_speed and abs(stage[self.stage_part][1] - self.position[1]) < self.move_speed:
self.position[1] = stage[self.stage_part][1]
self.position[0] = stage[self.stage_part][0]
self.stage_part += 1
else:
if stage[self.stage_part][0] - self.position[0] > 0:
self.position[0] += self.move_speed
elif stage[self.stage_part][0] - self.position[0] < 0:
self.position[0] -= self.move_speed
elif stage[self.stage_part][1] - self.position[1] > 0:
self.position[1] += self.move_speed
elif stage[self.stage_part][1] - self.position[1] < 0:
self.position[1] -= self.move_speed
def attack(self, attack_who):
# aoe enemies attack
if self.aoeOrNot:
if self.timer >= 2:
middle = attack_who.position
for units in placed_units:
x = (middle[0] - units.position[0]) ** 2
y = (middle[1] - units.position[1]) ** 2
distance = math.sqrt(x + y)
if distance < self.aoe_range:
units.hp -= self.damage
draw_attack_aoe(middle, self.aoe_range)
self.timer = 0
# suicide bombers attack
elif self.explodeOrNot:
middle = self.position
for units in placed_units:
x = (middle[0] - units.position[0]) ** 2
y = (middle[1] - units.position[1]) ** 2
distance = math.sqrt(x + y)
if distance < self.explode_range:
units.hp -= self.damage
draw_attack_aoe(middle, self.explode_range)
self.hp -= self.hp
# default enemy attack
else:
if self.timer >= 1:
attack_who.hp -= self.damage
self.timer = 0
draw_enemy_attack(self, attack_who)
# lhandles hp and enemies alive once they reach the end
def reached_end(self, currentlvl):
global currentlvl_left
global enemies_spawned
global hp
if self.position == currentlvl[-1]:
enemies_spawned.remove(self)
hp -= 1
currentlvl_left -= 1
# displays character when placed
def draw(self, canvas, img, img_info, SPRITE_SIZE):
self.timer += delta_time
# draws hp
max_hp_copy = self.max_hp
hp_copy = self.hp
canvas.draw_image(img, (img_info[0] / 2, img_info[1] / 2), (img_info[0], img_info[1]), self.position, SPRITE_SIZE)
canvas.draw_polygon([[self.position[0] - 30, self.position[1] + 30],
[self.position[0] + 30, self.position[1] + 30],
[self.position[0] + 30, self.position[1] + 35],
[self.position[0] - 30, self.position[1] + 35]], 1, 'Grey', 'Grey')
canvas.draw_polygon([[self.position[0] - 30, self.position[1] + 30],
[self.position[0] + 30 - ((1 - (hp_copy/max_hp_copy)) * 60), self.position[1] + 30],
[self.position[0] + 30 - ((1 - (hp_copy/max_hp_copy)) * 60), self.position[1] + 35],
[self.position[0] - 30, self.position[1] + 35]], 1, 'Green', 'Green')
#canvas.draw_circle(self.position, self.range, 2, 'red')
# melee tower/unit
class nero():
def __init__(self):
self.hp = 10
self.max_hp = 10
self.damage = 1
self.can_block = 2
self.cost = 3
self.position = []
self.range = []
self.special_range = []
self.real_range = [] # range when placed down
self.blocking = []
self.under_attack = []
self.type_target = 'ground'
self.name = 'nero'
self.direction = None
self.special_gauge = 0
self.max_special_gauge = 5
self.timer = 0
self.specialOrNot = False
self.startSpecialTimer = 0
self.attackTimer = 0
def draw(self, canvas, img, img_info, size):
self.timer += delta_time
self.attackTimer += delta_time
draw_unit(self, canvas, img, img_info, size)
def draw_info(self, canvas):
x1, x2, x3, x4 = draw_otherInfo(self, canvas)
# draws info
draw_otherInfo(self, canvas)
canvas.draw_polygon([[x1, 300], [x2, 300], [x2, 500], [x1, 500]], 1, 'rgb(0,0,0, .8)', 'rgb(0,0,0, .8)')
canvas.draw_text('NERO', (x3, 340), 24, 'white', 'serif')
canvas.draw_text('Class: guard (can block 2)', (x3, 370), 24, 'white', 'serif')
canvas.draw_text('Special: extends range by', (x3, 403), 20, 'white', 'serif')
canvas.draw_text('2, and attack 4 at once', (x3, 433), 20, 'white', 'serif')
def attack(self):
# every second, a attack is run, depending whether special or not different attacks will happen
if self.attackTimer >= 1:
if self.specialOrNot:
for i in self.blocking:
i.hp -= self.damage
self.attackTimer = 0
draw_attack(self, i)
else:
if self.blocking:
self.blocking[0].hp -= self.damage
self.attackTimer = 0
draw_attack(self, self.blocking[0])
# sets bools and changes info once special is activated
def special(self):
self.can_block = 4
self.real_range = self.special_range[self.direction]
self.specialOrNot = True
if self.timer >= (self.startSpecialTimer + 10):
self.can_block = 2
self.real_range = self.range[self.direction]
self.specialOrNot = False
self.special_gauge = 0
class fox():
def __init__(self):
self.hp = 5
self.max_hp = 5
self.damage = 2
self.can_block = 1
self.cost = 10
self.aoe_range = 80
self.position = []
self.range = []
self.real_range = [] # range when placed down
self.blocking = []
self.under_attack = []
self.type_target = 'ground air'
self.name = 'fox'
self.direction = None
self.special_gauge = 0
self.max_special_gauge = 10
self.timer = 0
self.specialOrNot = False
self.startSpecialTimer = 0
self.attackTimer = 0
def draw(self, canvas, img, img_info, size):
self.timer += delta_time
self.attackTimer += delta_time
draw_unit(self, canvas, img, img_info, size)
def draw_info(self, canvas):
x1, x2, x3, x4 = draw_otherInfo(self, canvas)
# draws info
draw_otherInfo(self, canvas)
canvas.draw_polygon([[x1, 300], [x2, 300], [x2, 500], [x1, 500]], 1, 'rgb(0,0,0, .8)', 'rgb(0,0,0, .8)')
canvas.draw_text('Tamamo no Mae', (x3, 340), 24, 'white', 'serif')
canvas.draw_text('Class: caster (aoe attack)', (x3, 375), 24, 'white', 'serif')
canvas.draw_text('Special: instantly kills all', (x3, 405), 24, 'white', 'serif')
canvas.draw_text('enemy on the field', (x3, 435), 24, 'white', 'serif')
def attack(self):
# kills everything on the map
if self.attackTimer >= 2:
if self.specialOrNot:
for i in enemies_spawned:
i.hp -= i.hp
self.attackTimer = 0
else:
# normal aoe attack every 2 seconds
if self.blocking:
# aoe attack
middle = self.blocking[0].position
self.blocking[0].hp -= self.damage
for enemies in enemies_spawned:
x = (middle[0] - enemies.position[0]) ** 2
y = (middle[1] - enemies.position[1]) ** 2
distance = math.sqrt(x + y)
if distance < self.aoe_range and enemies.enemy_type in self.type_target:
enemies.hp -= self.damage
draw_aoe(middle, self.aoe_range)
self.attackTimer = 0
def special(self):
self.specialOrNot = True