-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDefender.py
2506 lines (1679 loc) · 76.5 KB
/
Defender.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
#Standardize alive = True/False not 1/0
#change enemyship garbage collection to the same as human
#have some enemies fligh away from defender fast
#!/usr/bin/env python
#print("moves:",moves,end='\r', flush=True)
#notes: check all playfield[v][h] in all versions to make sure v comes first. I found one where it was switched
# and this may account for when the zombie dots don't die
# - ship objects that also have a sprite should have
# their HV co-ordinates looked at. We want to draw the sprite around the center of the sprite, not the corner
# Look at SpaceDot homing missile for an example.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# --
# ____ _____ _____ _____ _ _ ____ _____ ____ --
# | _ \| ____| ___| ____| \ | | _ \| ____| _ \ --
# | | | | _| | |_ | _| | \| | | | | _| | |_) | --
# | |_| | |___| _| | |___| |\ | |_| | |___| _ < --
# |____/|_____|_| |_____|_| \_|____/|_____|_| \_\ --
# --
#------------------------------------------------------------------------------
#offender? yes! Indeed!!
#------------------------------------------------------------------------------
# Arcade Retro Clock RGB
#
# Copyright 2021 William McEvoy
# Metropolis Dreamware Inc.
#
# NOT FOR COMMERCIAL USE
# If you want to use my code for commercial purposes, contact William McEvoy
# and we can make a deal.
#
#
#------------------------------------------------------------------------------
# Version: 0.1 --
# Date: January 15, 2022 --
# Reason: Converted to use LEDarcade --
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Initialization Section --
#------------------------------------------------------------------------------
from __future__ import print_function
import LEDarcade as LED
import copy
import random
import time
import numpy
import math
from datetime import datetime, timedelta
from rgbmatrix import graphics
random.seed()
start_time = time.time()
#-----------------------------
# Defender Global Variables --
#-----------------------------
#Sprite display locations
ClockH, ClockV, ClockRGB = 0,0, (0,100,0)
DayOfWeekH, DayOfWeekV, DayOfWeekRGB = 0,6, (150,0,0)
MonthH, MonthV, MonthRGB = 0,12, (0,20,200)
DayOfMonthH, DayOfMonthV, DayOfMonthRGB = 2,18, (100,100,0)
CurrencyH, CurrencyV, CurrencyRGB = 0,27, (0,150,0)
#Sprite filler tuple
SpriteFillerRGB = (0,4,0)
#GroundRGB = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
DirtGreen = (0,24,0)
SurfaceGreen = (0,50,0)
DirtYellow = (24,24,0)
SurfaceYellow = (54,54,0)
DirtPurple = (20,0,20)
SurfacePurple = (50,0,50)
DirtOrange = (30,10,0)
SurfaceOrange = (50,30,0)
GroundColorList = ((DirtGreen,SurfaceGreen),
(DirtYellow,SurfaceYellow),
(DirtPurple,SurfacePurple),
(DirtOrange,SurfaceOrange)
)
GroundColorCount = 4
ExplosionBrightnessModifier = 100 #used to increase brightness of particles
ExplosionR = 0
ExplosionG = 0
ExplosionB = 0
LaserR = 150
LaserG = 75
LaserB = 0
DefenderWorldWidth = 2048
MaxMountainHeight = 16
HumanCount = 15
EnemyShipCount = 25
AddEnemyCount = 25
SpawnNewEnemiesTargetCount = 5
SpawnNewHumansTargetCount = 5
ShipTypes = 27
RedrawGroundWaveCount = 5
#Movement
DefenderSpeed = 1
ReversingAdjustmentSpeed = 0.25
ReversingSteps = 64
OldSpeed = 0
SlowingDown = 0
DefenderSpeedIncrement = 0.25
DefenderMaxSpeed = 7
DefenderMinSpeed = 1
DefenderMoveUpRate = 10
DefenderMoveDownRate = 10
ReversingChance = 2000
DefenderSpeedChangeChance = 100
DefenderDirection = 1
DefenderReversing = 0
UpDownChance = 150
HumanMoveChance = 3
EnemyMoveSpeed = 6
GarbageCleanupChance = 500
GroundRadarChance = 10
FrontRadarChance = 15
ShootGroundShipCount = 20
AttackDistance = LED.HatWidth
HumanRunDistance = LED.HatWidth
ShootTime = time.time()
ShootWaitTime = 0.5
EnemyFearFactor = 10 #the lower the number, the more likely the enemy will run away
#Gravity
GroundParticleGravity = 0.05
HumanParticleGravity = 0.05
EnemyParticleGravity = 0.0198
BombGravity = 0.0198
DevenderReversing = 0
CurrentH = 0
TargetH = 0
#Bomb
DefenderBombVelocityH = 0.6
DefenderBombVelocityV = -0.2
BlastFactor = 4
StrafeLaserStrength = 5
LaserTurnOffChance = 20
BombDropChance = 75
RequestBombDrop = False
RequestGroundLaser = False
RequestedBombVelocityH = 0.2
RequestedBombVelocityV = 0.05
BombDetonationHeight = 20
MaxBombBounces = 3
#Human
HumanCountH = 25
HumanCountV = 0
HumanCountRGB = (100,0,200)
#Enemy
EnemyCountH = 10
EnemyCountV = 0
EnemyCountRGB = (10,0,200)
#Defender
DefenderStartH = 4
#change display based on display dimensions
if(LED.HatWidth > 60):
EnemyCountH = 36
HumanCountH = 50
ClockZoom = 1
DefenderStartH = 2
else:
ClockZoom = 1
#---------------------------------------
#Variable declaration section
#---------------------------------------
ScrollSleep = 0.025
TerminalTypeSpeed = 0.02 #pause in seconds between characters
TerminalScrollSpeed = 0.02 #pause in seconds between new lines
CursorRGB = (0,255,0)
CursorDarkRGB = (0,50,0)
BrightRGB = (0,200,0)
ShadowRGB = (0,5,0)
KeyboardSpeed = 500
CheckClockSpeed = 500
CheckTime = 60
#------------------------------
# Sprites, Arrays, Functions --
#------------------------------
def ScanInFrontOfDefender(H,V,Defender,DefenderPlayfield):
ScanDirection = 2
ScanH = Defender.h + Defender.width #start in front of ship
ScanV = Defender.v
Item = ''
ItemList = ['NULL']
RadarRange = LED.HatWidth - 14
# x 1234567890...50
try:
for x in range(0,RadarRange,2):
ScanH, ScanV = LED.CalculateDotMovement(ScanH,ScanV,ScanDirection)
if(ScanH + H < DefenderPlayfield.width):
Item = DefenderPlayfield.map[ScanV + V][ScanH + H].name
ItemList.append(Item)
break
except:
print("ERROR at location:",ScanV + V, ScanH + H)
return ItemList
def ScanFarAway(H,V,Defender,DefenderPlayfield):
#HV are the current upper left hand corner of the displayed playfield window
ScanDirection = 2 # 4 way direction, 2 = right/east/forward
ScanH = Defender.h + Defender.width #start in front of ship
ScanV = Defender.v
Item = ''
ItemList = [('EmptyObject',0,0)]
RadarStart = 5
RadarStop = LED.HatWidth
RadarStepH = 4
RadarStepV = 4
# x 20...50
try:
found = False
#Scan multiple lines
for y in range (-2,4,RadarStepV):
ScanV = Defender.v + y
#print("ScanFarAway: ScanH ScanV:",ScanH, ScanV)
for x in range(RadarStart,RadarStop,RadarStepH):
ScanH = Defender.h + Defender.width + x
ScanH, ScanV = LED.CalculateDotMovement(ScanH,ScanV,ScanDirection)
#LED.TheMatrix.SetPixel(ScanH, ScanV,50,50,250)
if(ScanH + H < DefenderPlayfield.width):
if(DefenderPlayfield.map[ScanV][ScanH + H].alive == True):
Item = DefenderPlayfield.map[ScanV][ScanH + H].name
#print("SCAN - adding item to list:",DefenderPlayfield.map[ScanV][ScanH + H].name, DefenderPlayfield.map[ScanV][ScanH + H].h,DefenderPlayfield.map[ScanV ][ScanH + H].v)
ItemList.append((Item,ScanH,ScanV))
found = True
#print("SCAN - Found:", DefenderPlayfield.map[ScanV][ScanH + H].name)
break
if(found == True):
break
except:
print("ERROR at location:",ScanV , ScanH + H)
return ItemList
def LookForTargets2(H,V,TargetName,Defender, DefenderPlayfield,Canvas,EnemyShips):
#Draw a box, scan each location and make a list of enemies
#HV are the current upper left hand corner of the displayed playfield window
#Defender.h is relative to the LED display (64x32)
if (DefenderDirection == 1):
ScanStartH = H + Defender.width + Defender.h
ScanStopH = ScanStartH + 40
else:
ScanStartH = H
ScanStopH = ScanStartH + 40 + Defender.h
ScanStartV = Defender.v -5
ScanStopV = Defender.v + 8
EnemyName = ''
EnemyH = 0
EnemyV = 0
if(ScanStartV <= 0):
ScanStartV = 0
if(ScanStartV >= LED.HatHeight - 1):
ScanStartV = LED.HatHeight - 1
if(ScanStartH >= DefenderPlayfield.width - 1):
ScanStartH = DefenderPlayfield.width - 1
#build list of enemies that fall within the radar box
#graphics.DrawLine(Canvas,ScanStartH -H, ScanStartV,ScanStopH-H,ScanStartV, graphics.Color(0,255,0))
#graphics.DrawLine(Canvas,ScanStartH-H, ScanStartV,ScanStartH-H,ScanStopV, graphics.Color(0,255,0))
#graphics.DrawLine(Canvas,ScanStartH-H, ScanStopV,ScanStopH-H,ScanStopV, graphics.Color(0,255,0))
#graphics.DrawLine(Canvas,ScanStopH-H, ScanStopV,ScanStopH-H,ScanStartV, graphics.Color(0,255,0))
#Built ItemList
TargetFound = False
ItemList = []
for Ship in EnemyShips:
#print('Scanning:',Ship.h,Ship.v)
if(Ship.alive == True):
#LED.TheMatrix.SetPixel(Ship.h - H,Ship.v,255,255,255)
#graphics.DrawCircle(Canvas,Ship.h - H,Ship.v,4,graphics.Color(255,255,255))
if((ScanStartH <= Ship.h <= ScanStopH) and (ScanStartV <= Ship.v <= ScanStopV)):
TargetFound = True
EnemyName = Ship.name
EnemyH = Ship.h
EnemyV = Ship.v
#print("LookForTarget2: EnemyFound HV",EnemyH, EnemyV)
break
return EnemyName, EnemyH, EnemyV, TargetFound
def LookForTargets(H,V,TargetName,Defender, DefenderPlayfield,Canvas):
## Deprecaged, old and slow
global RequestBombDrop
#This is my tradition method of examining the playfield to see if enemies are within range of the radar probes
#examining things one cell at a time
#HV are the current upper left hand corner of the displayed playfield window
EnemyName = 'EmptyObject'
EnemyH = -1
EnemyV = -1
#To improve performance, we will not scan every pixel in radar range
#Radar scanning will be interlaced
#This procedure is called continuously so lets scan a net instead of a solid area
#The size of the holes in the net is determined by ScanStep
StartX = H + LED.HatWidth -1
StopX = H + 10
StartY = 0
StopY = LED.HatHeight -1
ScanStep = 4
#Add a bit of randomness to the vertical start pixel
if random.randint(0,1) == 1:
StartY = 1
try:
#Look at furthest part of the screen and start checking for enemies
x = 0
y = 0
#Adjust StartX if it is too close to the end of the playfield
if (StartX >= DefenderPlayfield.width - 1):
StartX = DefenderPlayfield.width - 1
#If an enemy is on screen, take note and exit the loops
#sprites are usually bigger than a dot, so we use range step to increase speed of scan
Found = False
for x in range (StartX,0,-ScanStep):
#print(DefenderPlayfield.map[y][x].name)
for y in range(StartY,StopY,ScanStep):
if(DefenderPlayfield.map[y][x].name == TargetName and DefenderPlayfield.map[y][x].alive == True):
#print('Enemy found on radar:',DefenderPlayfield.map[y][x].h,DefenderPlayfield.map[y][x].v,DefenderPlayfield.map[y][x].name)
#Move defender to follow enemy
if(DefenderPlayfield.map[y][x].v < Defender.v):
#we do randint to stop the jitteriness of ship moving up and down
if(random.randint(0,DefenderMoveUpRate) == 1):
Defender.v = Defender.v - 1
Found = True
break
elif(DefenderPlayfield.map[y][x].v > Defender.v):
if(random.randint(0,DefenderMoveDownRate) == 1):
Defender.v = Defender.v + 1
Found = True
break
if(Found == True):
break
except:
print("ERROR at location: xy H StartX x",x,y,H, StartX,x)
print("A stupid error has occurred when finding targets. Please fix this soon.")
#If an target was found, scan to see if it is in firing range
TargetInRange = False
if(Found == True):
ItemList = ScanFarAway(H,V,Defender,DefenderPlayfield)
#EnemyTargets = ['Human','EnemyShip']
#print("Itemlist from Scanner:")
#print(ItemList)
for i in range (0,len(ItemList)):
EnemyName,EnemyH, EnemyV = ItemList[i]
if(EnemyName == TargetName):
#print("EnemyFound TargetName",EnemyName, TargetName)
TargetInRange = True
break
#if target was on screen, but not in firing range try dropping a bomb
RequestBombDrop = True
#print("Was EnemyNearby?",Found)
#print("Was TargetInRange?",TargetInRange)
return EnemyName,EnemyH, EnemyV, TargetInRange
#if ( any(item in EnemyTargets for item,h,v in ItemList)):
#for x in range (0,45):
# LED.setpixel(Defender.h + 5 + x,Defender.v + 2,255,0,0)
def LookForGroundTargets(Defender,DefenderPlayfield,Ground,Humans,EnemyShips):
global RequestBombDrop
global RequestGroundLaser
#upper left hand corner of currently displayed playfield window
PlayfieldH = round(DefenderPlayfield.DisplayH)
PlayfieldV = DefenderPlayfield.DisplayV
RadarWidth = 10
RadarHeight = 6
RadarAdjustH = 5
RadarAdjustV = 5
GroundV = 0
#To improve performance, we will not scan every pixel in radar range
#Radar scanning will be interlaced
#This procedure is called continuously so lets scan a net instead of a solid area
#The size of the holes in the net is determined by ScanStep
#avoid end of the playfield
if(PlayfieldH + RadarAdjustH + RadarWidth >= DefenderPlayfield.width -1):
PlayfieldH = PlayfieldH - RadarAdjustH - RadarWidth
#Find the ground
for GroundV in range (Defender.v, LED.HatHeight-2):
#LED.TheMatrix.SetPixel(Defender.h,Defender.v + V,255,5,10)
if(Ground.map[GroundV][PlayfieldH + RadarAdjustV] != (0,0,0)):
#print("Ground hit V H:",V,PlayfieldH + Defender.h)
break
#Radar box starts at the ground surface
StartX = PlayfieldH + RadarAdjustH
StopX = PlayfieldH + RadarAdjustH + RadarWidth
StartY = GroundV -2
StopY = GroundV + RadarHeight
ScanStep = 2
if(StopY >= LED.HatHeight):
StopY = LED.HatHeight
#Add a bit of randomness to the vertical start pixel
if random.randint(0,1) == 1:
StartY = StartY + 1
#new method
Found = False
for i in range (0,len(Humans)):
if(StartX <= Humans[i].h <= StopX and StartY <= Humans[i].v <= StopY):
#print("Found Human[i]hv | StartStopX |:",Humans[i].h,Humans[i].v," | ",StartX, StopX )
#LED.TheMatrix.SetPixel(Humans[i].h - PlayfieldH,Humans[i].v,255,255,255)
Found = True
RequestGroundLaser = True
RequestBombDrop = True
#time.4(0.25)
for i in range (0,len(EnemyShips)):
if(StartX <= EnemyShips[i].h <= StopX and StartY <= EnemyShips[i].v <= StopY):
#print("Found EnemyShip[i]hv | StartStopX |:",EnemyShips[i].h,EnemyShips[i].v," | ",StartX, StopX )
#LED.TheMatrix.SetPixel(EnemyShips[i].h - PlayfieldH,EnemyShips[i].v,255,255,255)
Found = True
RequestGroundLaser = True
RequestBombDrop = True
#time.sleep(0.25)
#except:
# print("A stupid error has occurred when ground finding targets. Please fix this soon.")
#if(Found == False):
# RequestGroundLaser = False
return RequestGroundLaser, RequestBombDrop, GroundV, Humans,EnemyShips
def ShootTarget(PlayfieldH, PlayfieldV, TargetName, TargetH,TargetV,Defender, DefenderPlayfield,Canvas):
global ShootTime
TargetHit = False
#PlayfieldH is the upper left hand corner of the playfield window being displayed
#TargetH and TargetV are on screen co-ordinates (64x32)
#we override the target because we want to shoot from the nose of the Defender then
#check to see if we hit the enemy
TargetV = Defender.v + 2
#print("ST - TargetName:",TargetName)
#print("ST - Shooting:",DefenderPlayfield.map[TargetV][TargetH+PlayfieldH].name, TargetH+PlayfieldH, TargetV)
if(DefenderPlayfield.map[TargetV][TargetH].name != "EmptyObject" and DefenderPlayfield.map[TargetV][TargetH].alive == True):
DefenderPlayfield.map[TargetV][TargetH].ConvertSpriteToParticles()
DefenderPlayfield.map[TargetV][TargetH].EraseSpriteFromPlayfield2(DefenderPlayfield)
DefenderPlayfield.map[TargetV][TargetH].alive = False
TargetHit = True
#we want to always shoot straight from the Defender. If it hits, good. If not, too bad.
if(DefenderDirection == -1):
graphics.DrawLine(Canvas,Defender.h , Defender.v +2 , TargetH - DefenderPlayfield.DisplayH, TargetV, graphics.Color(255,0,0))
else:
graphics.DrawLine(Canvas,Defender.h + 7, Defender.v +2 , TargetH , TargetV, graphics.Color(255,0,0))
else:
#Laser misses, draw to end of screen
if(DefenderDirection == 1):
graphics.DrawLine(Canvas,Defender.h + 5, Defender.v +2 , LED.HatWidth , TargetV, graphics.Color(255,0,0))
else:
graphics.DrawLine(Canvas,Defender.h , Defender.v +2 , 0 , TargetV, graphics.Color(255,0,255))
return DefenderPlayfield,TargetHit
def ShootGround(PlayfieldH, PlayfieldV, GroundV, Defender, DefenderPlayfield, Ground, Canvas, Humans, HumanParticles, EnemyShips, GroundParticles):
#PlayfieldH is the upper left hand corner of the playfield window being displayed
#Defender.h and Defender.v are relative to 64x32 display NOT the playfield
#print("Defender.h",Defender.h)
ScanH = round(PlayfieldH + Defender.h + 3)
ScanV = Defender.v + 2
ScreenH = Defender.h + 3
ScreenV = Defender.v + 2
i = 0
#Found = False
#GroundRGB = (0,0,0)
#if(ScanH < DefenderPlayfield.width):
# #find ground under defender
# for i in range (ScanV, LED.HatHeight):
# GroundRGB = Ground.map[i][ScanH]
# if GroundRGB != (0,0,0):
# break
LaserR = random.randint(50,255)
LaserG = random.randint(0,100)
LaserB = random.randint(50,255)
if(ScanH >= DefenderPlayfield.width - 1):
ScanH = DefenderPlayfield.width - 1
LineV = GroundV + 2
if(LineV > LED.HatHeight -1):
LineV = LED.HatHeight -1
if(LineV < Defender.v + 2):
LineV = Defender.v + 2
graphics.DrawLine(Canvas,ScreenH, ScreenV, ScreenH, LineV, graphics.Color(LaserR,LaserG,LaserB))
#Convert ground to particle
#Explode Ground
for j in range(0,StrafeLaserStrength):
if (GroundV + j < LED.HatHeight):
#print("Strafe HV:",ScanH, GroundV + j)
if(random.randint(0,1) == 1 and Ground.map[GroundV+j][ScanH] != (0,0,0)):
GroundParticles = AddGroundParticles(ScreenH,GroundV+j,LaserR, LaserG, 0,GroundParticles,LaserBlast=True)
else:
Ground.map[GroundV+j][ScanH] = (0,0,0)
#examine the killzone
Humans, HumanParticles, EnemyShips = KillEnemiesInBlastZone(ScanH,GroundV + j,StrafeLaserStrength, Humans, HumanParticles, EnemyShips,DefenderPlayfield)
return DefenderPlayfield, Ground, GroundParticles, Humans, HumanParticles, EnemyShips
def DebugPlayfield(Playfield,h,v,width,height):
#Show contents of playfield - in text window, for debugging purposes
print ("Map width height:",width,height)
print ("===============================================================")
for V in range(0,height):
for H in range (0,width):
name = Playfield[V+v][H+h].name
#print ("Display: ",name,V,H)
if (name == 'EmptyObject'):
print (' ',end='')
#draw border walls
#draw interior
elif (name == 'Wall'):
print (' #',end='')
elif (name == 'Ground'):
print (' G',end='')
#draw Human
elif (name == 'Human'):
print (' H',end='')
#draw EnemyShip
elif (name == 'EnemyShip'):
print ('**',end='')
#draw interior
elif (name == 'WallBreakable'):
print (' o',end='')
elif (Playfield[V][H].alive == 1):
print (' ?',end='')
#print ("Name?:",name," alive:",Playfield[V][H].alive)
elif (Playfield[V][H].alive == 0):
print (' !',end='')
#print ("Name!:",name," alive:",Playfield[V][H].alive)
else:
print (' X',end='')
#print ("NameX:",name," alive:",Playfield[V][H].alive)
print('')
print ("=============================================")
return
def CreateHumans(HumanCount,Ground,DefenderPlayfield):
Humans = []
#humans must be located at least HatWidth from the start
for count in range (0,HumanCount):
#LED.HumanSprite.framerate = random.randint(15,50)
TheSprite = LED.HumanSprite
TheSprite.h = random.randint(63,DefenderWorldWidth)
TheSprite.v = random.randint(16,LED.HatHeight-1)
if(random.randint(0,1) == 1):
TheSprite.direction = 1
else:
TheSprite.direction = -1
Humans.append(copy.deepcopy(TheSprite))
print("Placing humans:",count)
DefenderPlayfield.CopyAnimatedSpriteToPlayfield(Humans[count].h,Humans[count].v,Humans[count])
return Humans, DefenderPlayfield
def CreateEnemyWave(ShipType,ShipCount,Ground,DefenderPlayfield):
global EnemyShipCount
EnemyShipCount = ShipCount
EnemyShips = []
for count in range (0,ShipCount):
NewSprite = copy.deepcopy(LED.ShipSprites[ShipType])
NewSprite.framerate = random.randint(2,12)
NewSprite.name = "EnemyShip"
if(random.randint(0,EnemyFearFactor) == 1):
NewSprite.afraid = True
else:
NewSprite.afraid = False
if(random.randint(0,1) == 1):
NewSprite.direction = 1
else:
NewSprite.direction = -1
EnemyShips.append(NewSprite)
#EnemyShips[count].ConvertSpriteToParticles()
Finished = False
while (Finished == False):
#Find a spot in the sky for the ship
h = random.randint(64,DefenderWorldWidth)
v = random.randint(1,LED.HatHeight-1)
try:
if(Ground.map[v][h] == (0,0,0)):
EnemyShips[count].h = h
EnemyShips[count].v = v
Finished = True
print("Placing EnemyShips # HV:",count,h,v)
DefenderPlayfield.CopyAnimatedSpriteToPlayfield(EnemyShips[count].h,EnemyShips[count].v,EnemyShips[count])
except:
print("Error placing ship HV",h,v)
return EnemyShips,DefenderPlayfield
def AddEnemyShips(EnemyShips,ShipType,ShipCount,Ground,DefenderPlayfield):
global EnemyShipCount
for count in range (0,ShipCount):
NewSprite = copy.deepcopy(LED.ShipSprites[ShipType])
NewSprite.framerate = random.randint(2,12)
NewSprite.name = "EnemyShip"
if(random.randint(0,EnemyFearFactor) == 1):
NewSprite.afraid = True
else:
NewSprite.afraid = False
if(random.randint(0,1) == 1):
NewSprite.direction = 1
else:
NewSprite.direction = -1
Finished = False
while (Finished == False):
#Find a spot in the sky for the ship
h = random.randint(64,DefenderWorldWidth)
v = random.randint(1,LED.HatHeight-1)
try:
if(Ground.map[v][h] == (0,0,0)):
NewSprite.h = h
NewSprite.v = v
Finished = True
print("Placing EnemyShips i hv:",count, h,v)
EnemyShips.append(NewSprite)
DefenderPlayfield.CopyAnimatedSpriteToPlayfield(h,v,EnemyShips[count])
print("Placing EnemyShip HV: h,v")
except:
print("Error placing ship HV",h,v)
#We might have to let garbage cleanup determine if they are alive or not
#otherwise the counts get messed up
#Update total enemy count
EnemyShipCount = sum(1 for e in EnemyShips if e.alive == 1)
return EnemyShips, EnemyShipCount, DefenderPlayfield
def AddHumans(Humans,NewHumanCount,Ground,DefenderPlayfield):
global HumanCount
#humans must be located at least HatWidth from the start
for count in range (0,NewHumanCount):
#LED.HumanSprite.framerate = random.randint(15,50)
TheSprite = LED.HumanSprite
TheSprite.h = random.randint(63,DefenderWorldWidth)
TheSprite.v = random.randint(16,LED.HatHeight-1)
TheSprite.alive == True
if(random.randint(0,1) == 1):
TheSprite.direction = 1
else:
TheSprite.direction = -1
Humans.append(copy.deepcopy(TheSprite))
DefenderPlayfield.CopyAnimatedSpriteToPlayfield(Humans[count].h,Humans[count].v,Humans[count])
#HumanCount = len(Humans)
HumanCount = sum(1 for h in Humans if h.alive == 1)
return Humans,HumanCount, DefenderPlayfield
def DropPilot(H,V,Humans,DefenderPlayfield):
global HumanCount
#humans must be located at least HatWidth from the start
TheSprite = LED.HumanSprite
TheSprite.h = H
TheSprite.v = V
TheSprite.alive = True
#pilots run away
TheSprite.direction = 1
#if(random.randint(0,1) == 1):
# TheSprite.direction = 1
#else:
# TheSprite.direction = -1
HumanCount = HumanCount + 1
Humans.append(copy.deepcopy(TheSprite))
DefenderPlayfield.CopyAnimatedSpriteToPlayfield(Humans[-1].h,Humans[-1].v,Humans[-1])
return Humans, HumanCount, DefenderPlayfield
def AddGroundParticles(h,v,r,g,b,GroundParticles,LaserBlast = False):
#particles don't interact with anything other than the ground so
#they are HV co-ordinates that match the display, not the playfield
NewParticle = LED.Ship(h, v,r,g,b,2,2,2,1,1,'ground',0,0)
#NewParticle.velocityH = random.random() * (random.randint(0,1) *2 -1) / 5
if(LaserBlast == True):
NewParticle.velocityH = random.uniform(-1,1)
NewParticle.velocityV = random.uniform(-1,1)
else:
NewParticle.velocityH = random.random() * -1
NewParticle.velocityV = random.random() * -1
NewParticle.alive = True
GroundParticles.append(NewParticle)
return GroundParticles
def AddHumanParticles(h,v,r,g,b,HumanParticles):
#particles don't interact with anything other than the ground so
#they are HV co-ordinates that match the display, not the playfield
NewParticle = LED.Ship(h,v,r,g,b,2,2,2,1,1,'human',0,0)
NewParticle.velocityH = random.random() * -2
NewParticle.velocityV = random.random() * -2
NewParticle.alive = True
HumanParticles.append(NewParticle)
#print('AddHumanParticles HV:',h,v)
return HumanParticles
def KillEnemiesInBlastZone(BlastH,BlastV,BlastStrength, Humans, HumanParticles, EnemyShips,DefenderPlayfield):
#DisplayH is the current window (upper left hand coordinates) of the playfield
#BlastHV use playfield co-ordinates
DisplayH = DefenderPlayfield.DisplayH
#Define hitbox
h1 = BlastH - BlastStrength
h2 = BlastH + BlastStrength
v1 = BlastV - BlastStrength
v2 = BlastV + BlastStrength
ph = 0
pv = 0
if(v1 <= 0):
v1 = 0
if(v2 >= LED.HatHeight-1):
v2 = LED.HatHeight-1
#print("Kill Humans Blast Zone DisplayH:",DisplayH)
#print("Blast Zone h1 v1 h2 v2",h1,v1,h2,v2)
HumanCount = len(Humans)
#print("(Killzone)Human Count:",HumanCount)
for i in range (0,HumanCount):
if(Humans[i].alive == 1 and (h1 <= Humans[i].h <= h2) and (v1 <= Humans[i].v <= v2)):
print("----------> Human killed")
Humans[i].alive = False
#print("*******************************************************************")
#print("")
#print("i alive hv:",i, Humans[i].alive, Humans[i].h,Humans[i].v)
#print("")
for j in range (0,4):
ph = Humans[i].h - DisplayH
pv = Humans[i].v
#print("ph pv:",ph,pv)
HumanParticles = AddHumanParticles((Humans[i].h + j -DisplayH),Humans[i].v,255,0,0,HumanParticles)
ShipCount = len(EnemyShips)
#print("(Killzone)EnemyShip Count:",ShipCount)
for i in range (0,ShipCount):
if(EnemyShips[i].alive == 1 and (h1 <= EnemyShips[i].h <= h2) and (v1 <= EnemyShips[i].v <= v2)):
#print("----------> Enemy ship killed")
EnemyShips[i].alive = False
EnemyShips[i].ConvertSpriteToParticles()