-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOutbreak.py
More file actions
4078 lines (3130 loc) · 150 KB
/
Outbreak.py
File metadata and controls
4078 lines (3130 loc) · 150 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
#!/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.
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# --
# ___ _ _ _ --
# / _ \ _ _| |_| |__ _ __ ___ __ _| | __ --
# | | | | | | | __| '_ \| '__/ _ \/ _` | |/ / --
# | |_| | |_| | |_| |_) | | | __/ (_| | < --
# \___/ \__,_|\__|_.__/|_| \___|\__,_|_|\_\ --
# --
# --
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# Arcade Retro Clock RGB
#
# Copyright 2021 William McEvoy
# Metropolis Dreamware Inc.
# william.mcevoy@gmail.com
#
# 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 os
import LEDarcade as LED
LED.Initialize()
import copy
import random
import time
import numpy
import math
from numba import njit
random.seed()
start_time = time.time()
#-----------------------------
# Outbreak Global Variables --
#-----------------------------
VirusTopSpeed = 5
VirusBottomSpeed = 15
VirusStartSpeed = 15 #starting speed of the viruses
MinBright = 50
MaxBright = 255
OriginalMutationRate = 10000
OriginalMutationDeathRate = 500
MaxMutations = 5 #Maximum number of mutations, if surpassed the virus dies
MutationTypes = 10 #Number of different types of mutations
OriginalReplicationRate = 5000
replicationrate = OriginalReplicationRate
FreakoutReplicationRate = 10 #new replication rate when a virus freaksout
MaxVirusMoves = 1000000 #after this many moves the level is over
FreakoutMoves = 10000 #after this many moves, the viruses will replicate and mutate at a much greater rate
VirusMoves = 0 #used to count how many times the viruses have moved
ClumpingSpeed = 5 #This modifies the speed of viruses that contact each other
ReplicationSpeed = 5 #When a virus replicates, it will be a bit slower. This number is added to current speed.
ChanceOfSpeedup = 100 #determines how often a lone virus will spontaneously speed up
SlowTurnMinMoves = 1 #number of moves a mutated virus moves before turning
SlowTurnMaxMoves = 40 #number of moves a mutated virus moves before turning
MaxReplications = 5 #Maximum number of replications, if surpassed the virus dies
InfectionChance = 5 #Chance of one virus infecting another, lower the number greater the chance
DominanceMaxCount = 550000 #how many ticks with there being only one virus, when reached level over
VirusNameSpeedupCount = 15000 #when this many virus strains are on the board, speed them up
ChanceOfDying = 5000 #random chance of a virus dying
GreatChanceOfDying = 5000 #random chance of a virus dying when too many straings are alive
ChanceOfHeadingToHV = 550000 #random chance of all viruses being interested in the same location
ChanceOfHeadingToFood = 500 #random chance of a virus heading towards the nearest food
FoodCheckRadius = 15 #radius around the virus when looking for food
ChanceOfTurningIntoFood = 5 #Random chance of a dying mutating virus to turn into food
ChanceOfTurningIntoWall = 5 #Random chance of a dying mutating virus to turn into food
VirusFoodWallLives = 5 #Lives of food before it gets eaten and disappears
AuditSpeed = 15000 #Every X tick, an audit text window is displayed for debugging purposes
EatingSpeedAdjustment = 1 #When a virus eats, it gets full and slows down
SpeedIncrements = 100 #how many chunks the speed range is cut up into, for increasing gradually
FoodBrightnessSteps = 25 #each time a food loses life, it gets brighter by this many units
ChanceToStopEating = 100 #chance that a virus decides to stop eating and carry on with life
ChanceOfRandomFood = 250000 #chance that random food will show up, which will draw the viruses to it
MapOffset = 20 #how many pixels from the left screen does the map really start (so we don't overwrite clocks and other things)
BigFoodLives = 500 #lives for the big food particle
BigFoodRGB = (255,0,0)
MaxRandomViruses = 50 #maximum number of random viruses to place on big food maps
VirusMaxCount = 2500 #maximum number of unique virus strains allowed
MaxLevelsPlayed = 25 #quit after 5 maps are played
#Sprite display locations
ClockH, ClockV, ClockRGB = 0,0, (0,150,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)
#RGB Objects
#Canvas = LED.TheMatrix.CreateFrameCanvas()
#Canvas.Fill(0,0,0)
#PreviousFrame = [[(-1, -1, -1) for _ in range(LED.HatWidth)] for _ in range(LED.HatHeight)]
#ScreenArray = [[(0, 0, 0) for _ in range(LED.HatWidth)] for _ in range(LED.HatHeight)]
#---------------------------------------
#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)
fast_rng = None #a faster implementation of random
BrightRGB = (0,200,0)
ShadowRGB = (0,5,0)
ShowCrypto = 'N'
KeyboardSpeed = 500
CheckClockSpeed = 500
CheckTime = 60
random.seed()
start_time = time.time()
#--------------------------------------
# VirusWorld / OUTBREAK --
#--------------------------------------
# Ideas:
# - Mutations happen
# - if virus is mutating, track that in the object itself
# - possible mutations: speed, turning eraticly
# - aggression, defence can be new attributes
# - need a new object virus dot
# - when a virus conquers an area, remove part of the wall and scroll to the next area
# - areas may have dormant viruses that are only acivated once in a while
# - virus will slow down to eat
class FastRandom:
def __init__(self, seed=1):
seed = int.from_bytes(os.urandom(4), 'little') ^ int(time.time() * 1000)
self.state = seed
def randint(self, low, high):
self.state = (1103515245 * self.state + 12345) & 0x7FFFFFFF
return low + (self.state % (high - low + 1))
def random(self):
self.state = (1103515245 * self.state + 12345) & 0x7FFFFFFF
return self.state / 0x7FFFFFFF
def choice(self, seq):
index = self.randint(0, len(seq) - 1)
return seq[index]
#--------------------------------------
# VirusWorld --
#--------------------------------------
# Ideas:
# - Mutations happen
# - if virus is mutating, track that in the object itself
# - possible mutations: speed, turning eraticly
# - aggression, defence can be new attributes
# - need a new object virus dot
# - when a virus conquers an area, remove part of the wall and scroll to the next area
# - areas may have dormant viruses that are only acivated once in a while
# -
class VirusWorld(object):
#Started out as an attempt to make cars follow shapes. I was not happy with the results so I converted into a petri dish of viruses
DefaultColorList = {
' ' : ( 0, 0, 0),
'-' : ( 20, 20, 20),
'.' : ( 50, 50, 50),
'o' : ( 80, 80, 80),
'O' : (110,110,110),
'@' : (140,140,140),
'$' : (170,170,170),
'A' : ( 0, 0, 40),
'B' : ( 10, 10, 50),
'C' : ( 20, 20, 60),
'D' : ( 30, 30, 70),
'E' : ( 40, 40, 80),
'F' : ( 50, 50, 90),
'G' : ( 60, 60,100),
'H' : ( 70, 70,110),
'I' : ( 80, 80,120),
'J' : ( 90, 90,130),
'K' : (100,100,140),
'L' : (110,110,150),
'|' : (150,150,175),
'*' : (200,200,200),
'=' : (220,220,220),
'#' : (150,150,150),
'1' : ( 0,200, 0),
'2' : (150, 0, 0),
'3' : (150,100, 0),
'4' : ( 0, 0,100),
'5' : (200, 0, 50),
'6' : (125,185, 0),
'7' : (200, 0,200),
'8' : ( 50,150, 75)
}
DefaultTypeList = {
' ' : 'EmptyObject',
'-' : 'wall',
'.' : 'wall',
'o' : 'wall',
'O' : 'wall',
'@' : 'wall',
'#' : 'wall',
'$' : 'wall',
'*' : 'wallbreakable',
'A' : 'wallbreakable',
'B' : 'wallbreakable',
'C' : 'wallbreakable',
'D' : 'wallbreakable',
'E' : 'wallbreakable',
'F' : 'wallbreakable',
'G' : 'wallbreakable',
'H' : 'wallbreakable',
'I' : 'wallbreakable',
'J' : 'wallbreakable',
'K' : 'wallbreakable',
'L' : 'wallbreakable',
'|' : 'wall',
'1' : 'virus',
'2' : 'virus',
'3' : 'virus',
'4' : 'virus',
'5' : 'virus',
'6' : 'virus',
'7' : 'virus',
'8' : 'virus'
}
def __init__(self,name,width,height,Map,Playfield,CurrentRoomH,CurrentRoomV,DisplayH, DisplayV, mutationrate, replicationrate,mutationdeathrate,VirusStartSpeed):
self.name = name
self.width = width
self.height = height
self.Map = ([[]])
self.Playfield = ([[]])
self.CurrentRoomH = CurrentRoomH
self.CurrentRoomV = CurrentRoomV
self.DisplayH = DisplayH
self.DisplayV = DisplayV
self.mutationrate = mutationrate
self.replicationrate = replicationrate
self.mutationdeathrate = mutationdeathrate
self.VirusStartSpeed = VirusStartSpeed
self.Map = [[0 for i in range(self.width)] for i in range(self.height)]
self.Playfield = [[LED.EmptyObject for i in range(self.width)] for i in range(self.height)]
self.walllives = VirusFoodWallLives
self.Viruses = []
@staticmethod
def GenerateEmptyMap(width, height, fill_char=' '):
"""
Generates a text-based empty map.
Returns:
list[str]: A list of strings representing the empty map.
"""
if len(fill_char) != 1:
raise ValueError("fill_char must be a single character.")
return [fill_char * width for _ in range(height)]
@staticmethod
def GenerateEmptyMapWithBorder(width, height, wall_char='-', fill_char=' '):
"""
Generates a map with a solid border of wall characters.
Returns:
list[str]: A list of strings representing the bordered map.
"""
if len(wall_char) != 1 or len(fill_char) != 1:
raise ValueError("Characters must be single characters.")
if width < 3 or height < 3:
raise ValueError("Minimum map size with border is 3x3.")
map_rows = [wall_char * width]
for _ in range(height - 2):
map_rows.append(wall_char + fill_char * (width - 2) + wall_char)
map_rows.append(wall_char * width)
return map_rows
def AddRandomVirusesToPlayfield(self,VirusesToAdd=25):
global fast_rng
AddedCount = 0
while (AddedCount <= VirusesToAdd):
h = fast_rng.randint(15,self.width-2) #we use a 15 pixel offset because of other display items
v = fast_rng.randint(2,self.height-2)
#print ("hv",h,v,self.Playfield[v][h].name)
if (self.Playfield[v][h].name == 'EmptyObject' or
self.Playfield[v][h].name == 'WallBreakable'
):
#print("empty")
r,g,b = LED.BrightColorList[fast_rng.randint(1,27)]
VirusName = str(r) + '-' + str(g) + '-' + str(b)
self.Playfield[v][h] = Virus(h,v,0,0,r,g,b,1,1, self.VirusStartSpeed ,1,10,VirusName,0,0,10,'West',0,self.mutationrate,0,self.replicationrate,self.mutationdeathrate)
self.Viruses.append(self.Playfield[v][h])
AddedCount = AddedCount + 1
def CopyTextMapToPlayfield(self,TextMap):
global fast_rng
mapchar = ""
r = 0
g = 0
b = 0
h = 0
v = 0
dottype = ""
VirusName = ""
self.Playfield = ([[]])
self.Playfield = [[LED.EmptyObject for i in range(TextMap.width)] for i in range(TextMap.height)]
self.Viruses = []
#read the map string and process one character at a time
#decode the color and type of dot to place
for y in range (0,TextMap.height):
print (TextMap.map[y])
for x in range (0,TextMap.width):
mapchar = TextMap.map[y][x]
r,g,b = TextMap.ColorList.get(mapchar)
dottype = TextMap.TypeList.get(mapchar)
h = x #+ TextMap.h
v = y #+ TextMap.v
if (dottype == "virus"):
VirusName = str(r) + '-' + str(g) + '-' + str(b)
#(h,v,dh,dv,r,g,b,direction,scandirection,speed,alive,lives,name,score,exploding,radarrange,destination,mutationtype,mutationrate, mutationfactor, replicationrate):
self.Playfield[v][h] = Virus(h,v,0,0,r,g,b,1,1, self.VirusStartSpeed ,1,10,VirusName,0,0,10,'West',0,self.mutationrate,0,self.replicationrate,self.mutationdeathrate)
#self.Playfield[y][x].direction = PointTowardsObject8Way(x,y,height/2,width/2)
self.Viruses.append(self.Playfield[v][h])
elif (dottype == "wall"):
self.Playfield[v][h] = LED.Wall(h,v,r,g,b,1,1,'Wall')
elif (dottype == "wallbreakable"):
self.Playfield[v][h] = LED.Wall(h,v,r,g,b,1,self.walllives,'WallBreakable')
return
def CopyMapToPlayfield(self):
#This function is run once to populate the playfield with viruses, based on the map drawing
#XY is actually implemented as YX. Counter intuitive, but it works.
global fast_rng
width = self.width
height = self.height
self.Viruses = []
print ("Map width height:",width,height)
VirusName = ""
print ("RD - CopyMapToPlayfield - Width Height: ", width,height)
x = 0
y = 0
print ("width height: ",width,height)
for y in range (0,height):
#print (*self.Map[y])
for x in range(0,width):
#print ("RD xy color: ",x,y, self.Map[y][x])
SDColor = self.Map[y][x]
print(str(SDColor).rjust(3,' '),end='')
if (SDColor == 1):
r = SDDarkWhiteR
g = SDDarkWhiteG
b = SDDarkWhiteB
self.Playfield[y][x] = LED.Wall(x,y,r,g,b,1,1,'Wall')
elif (SDColor == 2):
r = SDDarkWhiteR + 30
g = SDDarkWhiteG + 30
b = SDDarkWhiteB + 30
#(h,v,r,g,b,alive,lives,name):
self.Playfield[y][x] = LED.Wall(x,y,r,g,b,1,10,'Wall')
#print ("Copying wallbreakable to playfield hv: ",y,x)
elif (SDColor == 3):
r = SDDarkWhiteR + 50
g = SDDarkWhiteG + 50
b = SDDarkWhiteB + 50
#(h,v,r,g,b,alive,lives,name):
self.Playfield[y][x] = LED.Wall(x,y,r,g,b,1,10,'Wall')
#print ("Copying wallbreakable to playfield hv: ",y,x)
elif (SDColor == 4):
r = SDDarkWhiteR
g = SDDarkWhiteG
b = SDDarkWhiteR + 60
#(h,v,r,g,b,alive,lives,name):
self.Playfield[y][x] = LED.Wall(x,y,r,g,b,1,self.walllives,'WallBreakable')
#print ("Copying wallbreakable to playfield hv: ",y,x)
#color 5 and up represents moving viruses.
#We used to let the viruses have a random direction, but that quickly turns into chaos
#now each virus will be steered towards the center of the map
elif (SDColor >=5):
r,g,b = ColorList[SDColor]
VirusName = str(r) + '-' + str(g) + '-' + str(b)
#(h,v,dh,dv,r,g,b,direction,scandirection,speed,alive,lives,name,score,exploding,radarrange,destination,mutationtype,mutationrate, mutationfactor, replicationrate):
self.Playfield[y][x] = Virus(x,y,x,y,r,g,b,1,1, self.VirusStartSpeed ,1,10,VirusName,0,0,10,'West',0,self.mutationrate,0,self.replicationrate,self.mutationdeathrate)
#self.Playfield[y][x].direction = fast_rng.randint(1,8)
self.Playfield[y][x].direction = PointTowardsObject8Way(x,y,height/2,width/2)
self.Viruses.append(self.Playfield[y][x])
else:
#print ('EmptyObject')
self.Playfield[y][x] = LED.EmptyObject
print('')
self.DebugPlayfield()
return;
def CopySpriteToPlayfield(self,TheSprite,h,v, ColorTuple=(-1,-1,-1),ObjectType = 'Wall',Filler='EmptyObject'):
#Copy a regular sprite to the Playfield. Sprite will be treated as a wall.
#print ("Copying sprite to playfield:",TheSprite.name, ObjectType, Filler)
width = self.width
height = self.height
if (ColorTuple == (-1,-1,-1)):
r = TheSprite.r
g = TheSprite.g
b - TheSprite.b
else:
r,g,b = ColorTuple
#Copy sprite to map
for count in range (0,(TheSprite.width * TheSprite.height)):
y,x = divmod(count,TheSprite.width)
#check the sprite grid at location[count] to see if it has a 1 or zero. Remember the grid is a simple array.
#I was young and new when I first wrote the first sprite functions, and did not understand arrays in python. :)
if TheSprite.grid[count] != 0:
if (ObjectType == 'Wall'):
#self.Playfield[y+v][x+h] = LED.Wall(x,y,r,g,b,1,1,'Wall')
SetPlayfieldObject(v=y+v, h=x+h, obj=LED.Wall(x,y,r,g,b,1,1,'Wall'), Playfield=self.Playfield)
elif(ObjectType == 'WallBreakable'):
#self.Playfield[y+v][x+h] = LED.Wall(x,y,r,g,b,1,1,'WallBreakable')
SetPlayfieldObject(v=y+v, h=x+h, obj=LED.Wall(x,y,r,g,b,1,1,'WallBreakable'), Playfield=self.Playfield)
elif(ObjectType == 'Virus'):
#self.Playfield[y+v][x+h] = Virus(x,y,x,y,r,g,b,1,1, self.VirusStartSpeed ,1,10,'?',0,0,10,'West',0,self.mutationrate,0,self.replicationrate,self.mutationdeathrate)
SetPlayfieldObject(v=y+v, h=x+h, obj=Virus(x,y,x,y,r,g,b,1,1, self.VirusStartSpeed ,1,10,'?',0,0,10,'West',0,self.mutationrate,0,self.replicationrate,self.mutationdeathrate), Playfield=self.Playfield)
else:
if (Filler == 'EmptyObject'):
self.Playfield[y+v][x+h] = LED.EmptyObject
elif (Filler == 'DarkWall'):
#dark wall
self.Playfield[y+v][x+h] = LED.Wall(x,y,5,5,5,1,1,'Wall')
else:
self.Playfield[y+v][x+h] = LED.EmptyObject
return;
# def DisplayWindow(self,h,v):
# #This function accepts h,v coordinates for the entire map (e.g. 1,8 20,20, 64,64)
# #Displays what is on the playfield currently, including walls, cars, etc.
# r = 0
# g = 0
# b = 0
# count = 0
# for V in range(0,LED.HatWidth):
# for H in range (0,LED.HatHeight):
# #print ("DisplayWindow hv HV: ",h,v,H,V)
# name = self.Playfield[v+V][h+H].name
# #print ("Display: ",name,V,H)
# if (name == 'EmptyObject'):
# r = 0
# g = 0
# b = 0
# else:
# r = self.Playfield[v+V][h+H].r
# g = self.Playfield[v+V][h+H].g
# b = self.Playfield[v+V][h+H].b
# #Our map is an array of arrays [v][h] but we draw h,v
# LED.TheMatrix.SetPixel(H,V,r,g,b)
# #unicorn.show()
# #SendBufferPacket(RemoteDisplay,LED.HatHeight,LED.HatWidth)
def DisplayWindow(self,h,v,ZoomFactor = 0):
#This function accepts h,v coordinates for the entire map (e.g. 1,8 20,20, 64,64)
#Displays what is on the playfield currently, including walls, cars, etc.
#Zoom factor is used to shrink/expand the display
r = 0
g = 0
b = 0
count = 0
H_modifier = 0
V_modifier = 0
HIndentFactor = 0
VIndentFactor = 0
if (ZoomFactor > 1):
H_modifier = (1 / LED.HatWidth ) * ZoomFactor * 2 #BigLED is 2 times wider than tall. Hardcoding now, will fix later.
V_modifier = (1 / LED.HatHeight ) * ZoomFactor
NewHeight = round(LED.HatHeight * V_modifier)
NewWidth = round(LED.HatWidth * H_modifier)
HIndentFactor = (LED.HatWidth / 2) - (NewWidth /2)
VIndentFactor = (LED.HatHeight / 2) - (NewHeight /2)
else:
IndentFactor = 0
#print("LED.HatWidth",LED.HatWidth," NewWidth",NewWidth," ZoomFactor:",ZoomFactor,"HV_modifier",HV_modifier, "IndentFactor:",IndentFactor)
for V in range(0,LED.HatHeight):
for H in range (0,LED.HatWidth):
#print ("DisplayWindow hv HV: ",h,v,H,V)
name = self.Playfield[v+V][h+H].name
#print ("Display: ",name,V,H)
if (name == 'EmptyObject'):
r = 0
g = 0
b = 0
else:
r = self.Playfield[v+V][h+H].r
g = self.Playfield[v+V][h+H].g
b = self.Playfield[v+V][h+H].b
#Our map is an array V of array H [V][1,2,3,4...etc]
if (ZoomFactor > 0):
LED.TheMatrix.SetPixel((H * H_modifier) + HIndentFactor ,(V * V_modifier) + VIndentFactor,r,g,b)
else:
LED.TheMatrix.SetPixel(H,V,r,g,b)
#unicorn.show()
#SendBufferPacket(RemoteDisplay,LED.HatHeight,LED.HatWidth)
def DisplayWindowZoom(self,h,v,Z1=8,Z2=1,ZoomSleep=0.05):
#uses playfield to display items
if (Z1 <= Z2):
for Z in range (Z1,Z2):
#LED.TheMatrix.Clear()
self.DisplayWindow(h,v,Z)
#time.sleep(ZoomSleep)
else:
for Z in reversed(range(Z2,Z1)):
#LED.TheMatrix.Clear()
self.DisplayWindow(h,v,Z)
#time.sleep(ZoomSleep)
def DisplayWindowWithSprite(self,h,v,ClockSprite):
#This function accepts h,v coordinates for the entire map (e.g. 1,8 20,20, 64,64)
#Displays what is on the playfield currently, including walls, cars, etc.
r = 0
g = 0
b = 0
count = 0
maxV = min(LED.HatHeight, self.height - v)
maxH = min(LED.HatWidth, self.width - h)
for V in range(maxV):
for H in range(maxH):
name = self.Playfield[v+V][h+H].name
if (name == 'EmptyObject'):
r = 0
g = 0
b = 0
else:
r = self.Playfield[v+V][h+H].r
g = self.Playfield[v+V][h+H].g
b = self.Playfield[v+V][h+H].b
#Our map is an array of arrays [v][h] but we draw h,v
LED.TheMatrix.SetPixel(H,V,r,g,b)
#Display clock at current location
#Clock hv will allow external functions to slide clock all over screen
#print ("Clock info hv on: ",ClockSprite.h,ClockSprite.v,ClockSprite.on)
ClockSprite.CopySpriteToBuffer(ClockSprite.h,ClockSprite.v)
#unicorn.show()
#SendBufferPacket(RemoteDisplay,LED.HatHeight,LED.HatWidth)
def CountVirusesInWindow(self,h,v):
#This function accepts h,v coordinates for the entire map (e.g. 1,8 20,20, 64,64)
#and counts how many items are in the area
count = 0
maxV = min(LED.HatHeight, self.height - v)
maxH = min(LED.HatWidth, self.width - h)
for V in range(maxV):
for H in range(maxH):
name = self.Playfield[v+V][h+H].name
#print ("Display: ",name,V,H)
if (name not in ('EmptyObject',"Wall","WallBreakable")):
count = count + 1
return count;
def DebugPlayfield(self):
#Show contents of playfield - in text window, for debugging purposes
height = len(self.Playfield)
width = len(self.Playfield[0]) if height > 0 else 0
print("DebugPlayfield - actual dimensions:", width, "x", height)
print ("Map width height:",width,height)
x = 0
y = 0
for V in range(0,height):
for H in range (0,width):
name = self.Playfield[V][H].name
#print ("Display: ",name,V,H)
if (name == 'EmptyObject'):
print (' ',end='')
#draw border walls
elif (name == 'Wall' and (V == 0 or V == height-1)):
print(' _',end='')
#draw border walls
elif (name == 'Wall' and (H == 0 or H == width-1)):
print(' |',end='')
#draw interior
elif (name == 'Wall'):
print (' #',end='')
#draw interior
elif (name == 'WallBreakable'):
print (' o',end='')
elif (self.Playfield[V][H].alive == 1):
print (' .',end='')
else:
print (' X',end='')
#print ("Name:",name," alive:",self.Playfield[V][H].alive)
#time.sleep(1)
print('')
def FindClosestObject_old(self,SourceH,SourceV, Radius = 10, ObjectType = 'WallBreakable'):
#Find the HV co-ordinates of the closest playfield object
#
#print("Searching for nearby food SourceH SourceV Radius ObjectType",SourceH, SourceV, Radius, ObjectType)
#Prepare co-ordinates for search grid
StartX = SourceH - Radius
StopX = SourceH + Radius
StartY = SourceV - Radius
StopY = SourceV + Radius
ClosestX = -1
ClosestY = -1
MinDistance = 9999
Distance = 0
#Check boundaries
if (StartX < 0):
StartX = 0
if (StartX > LED.HatWidth-1):
StartX = LED.HatWidth-1
if (StartY < 0):
StartY = 0
if (StartY > LED.HatHeight-1):
StartY = LED.HatHeight-1
if (StopX < 0):
StopX = 0
if (StopX > LED.HatWidth-1):
StopX = LED.HatWidth-1
if (StopY < 0):
StopY = 0
if (StopY > LED.HatHeight-1):
StopY = LED.HatHeight-1
#print("Start XY Stop XY",StartX,StartY, StopX, StopY)
for x in range(StartX,StopX):
for y in range(StartY, StopY):
#remember playfield coordinates are swapped
if (self.Playfield[y][x].name == ObjectType):
Distance = GetDistanceBetweenDots(SourceH,SourceV,x,y)
#print ("Distance: ",Distance, " MinDistance:",MinDistance, "xy:",x,y)
if (Distance <= MinDistance):
MinDistance = Distance
ClosestX = x
ClosestY = y
return ClosestX,ClosestY;
@njit
def GetDistanceSquared(self, h1, v1, h2, v2):
dx = h1 - h2
dy = v1 - v2
return dx * dx + dy * dy
def GetDistanceSquared(self, h1, v1, h2, v2):
dx = h1 - h2
dy = v1 - v2
return dx * dx + dy * dy
def FindClosestObject(self, SourceH, SourceV, Radius=10, ObjectType='WallBreakable'):
"""
Find the HV coordinates of the closest object of a given type within a radius.
Uses squared distance comparison to avoid slow math.sqrt calls.
"""
StartX = max(0, SourceH - Radius)
StopX = min(LED.HatWidth, SourceH + Radius + 1)
StartY = max(0, SourceV - Radius)
StopY = min(LED.HatHeight, SourceV + Radius + 1)
ClosestX = -1
ClosestY = -1
MinDistanceSquared = Radius * Radius + 1
for x in range(StartX, StopX):
for y in range(StartY, StopY):
if self.Playfield[y][x].name == ObjectType:
dist_sq = self.GetDistanceSquared(SourceH, SourceV, x, y)
if dist_sq < MinDistanceSquared:
MinDistanceSquared = dist_sq
ClosestX, ClosestY = x, y
return ClosestX, ClosestY
class Virus(object):
def __init__(self,h,v,dh,dv,r,g,b,direction,scandirection,speed,alive,lives,name,score,exploding,radarrange,destination,mutationtype,mutationrate,mutationfactor,replicationrate,mutationdeathrate):
self.h = h # location on playfield (e.g. 10,35)
self.v = v # location on playfield (e.g. 10,35)
self.dh = dh # location on display (e.g. 3,4)
self.dv = dv # location on display (e.g. 3,4)
self.r = r
self.g = g
self.b = b
self.direction = direction #direction of travel
self.scandirection = scandirection #direction of scanners, if equipped
self.speed = speed
self.alive = 1
self.lives = 3
self.name = name
self.score = 0
self.exploding = 0
self.radarrange = 20
self.destination = ""
self.mutationtype = mutationtype
self.mutationrate = mutationrate #high number, greater chance
self.mutationfactor = mutationfactor #used to impact amount of mutation
self.internalcounter = 0 #used to count moves between mutation affects (i.e. turn left every 3 moves)
self.replicationrate = replicationrate
self.mutationdeathrate = mutationdeathrate
self.replications = 0
self.mutations = 0
self.infectionchance = InfectionChance
self.chanceofdying = ChanceOfDying
self.eating = False
self.clumping = True
def Display(self):
if (self.alive == 1):
LED.TheMatrix.SetPixel(self.h,self.v,self.r,self.g,self.b)
def Erase(self):
LED.TheMatrix.SetPixel(self.h,self.v,0,0,0)
#Lower is faster!
def AdjustSpeed(self, increment):
speed = self.speed + increment
if (speed > VirusBottomSpeed):
speed = VirusBottomSpeed
elif (speed < VirusTopSpeed):
speed = VirusTopSpeed
self.speed = speed
#print("Adjust speed: ",speed, increment)
return;
#Lower is faster!
def AdjustInfectionChance(self, increment):
infectionchance = self.infectionchance + increment
if (infectionchance > InfectionChance):
infectionchance = InfectionChance
elif (infectionchance < 1):
infectionchance = 1
self.infectionchance = infectionchance
return;
def Mutate(self):
global MaxMutations
global MutationTypes
x = 0
#number of possible mutations
# direction
# - left 1,2
# - left 1,2,3
# - right 1,2
# - left 1,2,3
# speed up
# speed down
# wobble
# slow curves left
# slow curves right
mutationrate = self.mutationrate
mutationtype = self.mutationtype
mutationfactor = self.mutationfactor
speed = self.speed
MinSpeed = 1 #* CPUModifier
MaxSpeed = 10 #* CPUModifier #higher = slower!
r = 0
g = 0
b = 0
name = 0
#Mutations can be deadly
self.mutations += 1
if (( fast_rng.randint(1,self.mutationdeathrate) == 1)
or (self.mutations >= MaxMutations)):
self.alive = 0
self.lives = 0
self.speed = 999999
self.name = 'EmptyObject'
self.r = 0
self.g = 0
self.b = 0
else: