-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelEngine_snake.py
1226 lines (1066 loc) · 38.3 KB
/
pixelEngine_snake.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
import tkinter as tk
import keyboard # for getting key inputs
import time
import random
# =============================================
# IMPORT ANY LIBRARY YOU WANT HERE
# =============================================
# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
# import
# ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
# SOME ASSUMPTIONS
# X values are the column numbers starting from 0 (LEFT) to 19 (RIGHT)
# Y values are the row numbers starting from 0 (TOP) to 39 (BOTTOM)
background = "#000000" # default color of the empty space change if needed
class object:
# This class consists of one object (i.e. group of colored pixels)
# the object acts as a incompressible / indestructible entity that can translate in space
# the object structure is defined as follows
# assume the object's origin to be at the origin of cartesian system then make a dictionary as follows
# {
# "hexValueOfColor" : [(x1,y1),(x2,y2),(x3,y3)],
# "hexValueOfColor" : [(x1,y1),(x2,y2),(x3,y3)],
# "hexValueOfColor" : [(x1,y1),(x2,y2),(x3,y3)],
# ...
# }
# There can be many pixels of same color thus the above coloring system is taken
# color of indivisual pixel can be changed later on using the function provided in this class [not recommended though
# considering the simplicity of our display aka LED MATRIX]
# 'data' is a dictionary consisting of the following
# {
# "z_value" : int,
# "pos" : [int, int],
# "stayInFrame" : True,
# "collision" : True,
# "rotation" : False
# }
# 'z_value' : indicates the position of the object in z axis, higher the value further it is infront of the LED MATRIX,
# thus 0 is at the back and 10 is infront if in any scenario two objects overlap z_value will decide which will be
# displayed infront
# 'pos' : it represents the (X,Y) coordinate of the origin of the object on the LED MATRIX
# 'stayInFrame' : a basic system that prevent the object from going out of bound
# 'collision' : a basic system that prevent the object from colliding with other objects
# objects will collide only if offset() function is used and both the objects have "collision" set to True also both should
# share the same layer (i.e. they have same zVal)
# 'rotation' : enables object to rotate by multiples of 90 in either direction CW / CCW ... this system also checks if
# rotation in the desired direction is possible
# OBJECT VARIABLES
curr_pos = [0,0] # default value
pixelArr = [] # consists of the hexadecial values of the pixels relative to origin ROW-WISE (top row first)
bound_origin = [0,0] # the origin wrt the bounding box
zVal = 0 # the location of object outward from the plane (Z position)
frameCollision = False # turned True when bound collision is enabled
collision = False # turned True when collision is enabled
rotation = False # turned True when rotation is enabled
pixDict = None # storing pixel dictionary to be used when rotation enabled
rot_mid = None # stores mid position when rotation enabled
# other variables
topPad = 0
botPad = 0
lefPad = 0
rigPad = 0
minY = 0
maxY = 0
minX = 0
maxX = 0
def getBoundingBox(self, arrObj):
if isinstance(arrObj, dict):
arr = []
for key in arrObj.keys():
for coord in arrObj[key]:
arr.append(coord)
else:
arr = arrObj.copy()
self.minY = None
self.maxY = None
self.minX = None
self.maxX = None
firstTime = True
# print(arr)
for coord in arr:
if firstTime:
self.minY = coord[1]
self.maxY = coord[1]
self.minX = coord[0]
self.maxX = coord[0]
firstTime = False
# X
if coord[0] < self.minX:
self.minX = coord[0]
if coord[0] > self.maxX:
self.maxX = coord[0]
# Y
if coord[1] < self.minY:
self.minY = coord[1]
if coord[1] > self.maxY:
self.maxY = coord[1]
if self.rotation:
# if rotation is enabled making a free pixel Arr to allow for rotation
j = 1 + 2 * max([
abs(self.maxX),
abs(self.minX),
abs(self.maxY),
abs(self.minY)
])
return [j,j]
else:
return [self.maxX-self.minX+1, self.maxY-self.minY+1]
def calcPadding(self):
self.lefPad = -self.minX
self.rigPad = self.maxX
self.topPad = -self.minY
self.botPad = self.maxY
# self.lefPad = self.bound_origin[0]
# self.rigPad = len(self.pixelArr[0]) - self.bound_origin[0] - 1
# self.topPad = self.bound_origin[1]
# self.botPad = len(self.pixelArr) - self.bound_origin[1] - 1
# print(self.lefPad) # DEBUG
# print(self.rigPad)
# print(self.topPad)
# print(self.botPad)
def calcBoundOrigin(self, size):
# getting midpoint of the object and updating bound origin
boundX = 0
boundY = 0
if self.rotation:
mid = int((size[0] - 1)/2)
self.bound_origin = [mid, mid]
else:
if self.minX < 0:
boundX = -self.minX
else:
boundX = 0
if self.minY < 0:
boundY = -self.minY
else:
boundY = 0
self.bound_origin = [boundX, boundY]
def __init__(self, obj, data):
# here 'obj' holds the dictionary of colored pixels thus defining the object
# 'data' consists of other data as was stated above, these values are used to define the behaviour of this object
# assigning basic variables
if "pos" in data.keys():
self.curr_pos = data["pos"]
else:
self.curr_pos = [0,0]
if "z_value" in data.keys():
self.zVal = data["z_value"]
else:
self.zVal = 0
if "stayInFrame" in data.keys():
self.frameCollision = data["stayInFrame"]
else:
self.frameCollision = False
if "collision" in data.keys():
self.collision = data["collision"]
else:
self.collision = False
if "rotation" in data.keys():
self.rotation = data["rotation"]
else:
self.rotation = False
if self.rotation:
self.pixDict = obj
# making the blank pixel array
# getting order of matrix
size = self.getBoundingBox(obj)
self.calcBoundOrigin(size)
if self.rotation:
mid = int((size[0] - 1)/2)
self.rot_mid = mid
self.pixelArr = []
# making one row
row_temp = []
for i in range(size[0]):
row_temp.append(None)
# adding these rows in pixel array to create the blank array
for i in range(size[1]):
self.pixelArr.append(row_temp.copy())
# coloring apropriate pixels
# iterating through the dictionary and coloring the pixels
for k in obj.keys():
for coord in obj[k]:
# here k stores the hexadecimal value of the color and coord are the relative coordinated of pixels
arrCoord_X = self.bound_origin[0] + coord[0]
arrCoord_Y = self.bound_origin[1] + coord[1]
self.pixelArr[arrCoord_Y][arrCoord_X] = k
self.calcPadding() # calculate padding
def translate(self, delta):
newVec = [
self.curr_pos[0] + delta[0],
self.curr_pos[1] + delta[1]
]
if self.frameCollision:
if delta[0] > 0:
# checking right wall calculation
while (newVec[0] + self.rigPad) >= 20:
newVec[0] -= 1
if delta[0] < 0:
# checking left wall calculation
while (newVec[0] - self.lefPad) <= -1:
newVec[0] += 1
if delta[1] > 0:
# checking bot wall calculation
while (newVec[1] + self.botPad) >= 40:
newVec[1] -= 1
if delta[1] < 0:
# checking bot wall calculation
while (newVec[1] - self.topPad) <= -1:
newVec[1] += 1
self.curr_pos = newVec.copy()
def changeColor(self, coord, color):
arrCoord_X = self.bound_origin[0] + coord[0]
arrCoord_Y = self.bound_origin[1] + coord[1]
self.pixelArr[arrCoord_Y][arrCoord_X] = color
def updateCollision(obj, newPixArr):
obj.pixelArr = newPixArr
coordArr = []
for y in range(len(newPixArr)):
for x in range(len(newPixArr)):
if newPixArr[y][x] is not None:
coordArr.append([
x - obj.rot_mid,
y - obj.rot_mid
])
size = obj.getBoundingBox(coordArr)
obj.calcPadding()
obj.calcBoundOrigin(size)
# this functions is used to check collisions with the objects already present in objArr
def checkOverlap(obj, newPos):
global objArr
isOverLap = False
for ob in objArr:
if ob.collision and (ob != obj) and (ob.zVal == obj.zVal):
for y in range(len(ob.pixelArr)):
for x in range(len(ob.pixelArr[y])):
if ob.pixelArr[y][x] is not None:
if ob.rotation:
coordX = x - ob.rot_mid + ob.curr_pos[0]
coordY = y - ob.rot_mid + ob.curr_pos[1]
else:
coordX = x + ob.curr_pos[0] + ob.minX
coordY = y + ob.curr_pos[1] + ob.minY
for yN in range(len(obj.pixelArr)):
for xN in range(len(obj.pixelArr[yN])):
if obj.pixelArr[yN][xN] is not None:
if obj.rotation:
coordNX = xN - obj.rot_mid + newPos[0]
coordNY = yN - obj.rot_mid + newPos[1]
else:
coordNX = xN + newPos[0] + obj.minX
coordNY = yN + newPos[1] + obj.minY
if (coordNX == coordX) and (coordNY == coordY):
isOverLap = True
if isOverLap:
break
if isOverLap:
break
if isOverLap:
break
if isOverLap:
break
if isOverLap:
break
# print(isOverLap) # DEBUG
return isOverLap
def offset(obj, offset):
if offset[0] == 0:
Xdir = 0
else:
Xdir = int(offset[0] / abs(offset[0]))
if offset[1] == 0:
Ydir = 0
else:
Ydir = int(offset[1] / abs(offset[1]))
C2 = offset.copy()
j = [0,0]
while True:
if offset[0] - j[0] == 0:
step = round(C2[1] - j[1])
else:
step = round((C2[1] - j[1]) / (abs(offset[0] - j[0])))
j[0] += Xdir
isOverlapping = checkOverlap(obj, [
obj.curr_pos[0] + j[0],
obj.curr_pos[1] + j[1]
])
if isOverlapping:
pass
else:
obj.translate([Xdir,0])
for y in range(abs(step)):
j[1] += Ydir
isOverlapping = checkOverlap(obj, [
obj.curr_pos[0] + j[0],
obj.curr_pos[1] + j[1]
])
if isOverlapping:
pass
else:
obj.translate([0,Ydir])
if (j == C2):
break
def rotate(obj, amt):
# this function rotates the given 'obj' by 'amt'
# amt +1 = CCW by 90 degree ; +2 = CCW by 180 degree
# amt -1 = CW by 90 degree ; -2 = CW by 180 degree
def getNewPixArr():
mid = obj.rot_mid
newPixArr = []
# make empty
temp = []
for x in range(len(obj.pixelArr)):
temp.append(None)
for x in range(len(obj.pixelArr)):
newPixArr.append(temp.copy())
# iterating through the original Array and making the necessary swaps
for y in range(len(obj.pixelArr)):
for x in range(len(obj.pixelArr)):
dX = x - mid
dY = y - mid
# CCW SWAP
if amt < 0:
newPixArr[mid + dX][mid - dY] = obj.pixelArr[y][x]
else:
newPixArr[mid - dX][mid + dY] = obj.pixelArr[y][x]
return newPixArr
if not obj.rotation:
raise Exception("object set to rotation:False yet rotate function was accessed")
preservedArr = obj.pixelArr.copy() # save PixArr
# resolve Rotate
while amt > 4:
amt -= 4
while amt < -4:
amt += 4
# iterate rotation
for a in range(abs(amt)):
updateCollision(obj, getNewPixArr())
# checking if this rotation causes collision
if checkOverlap(obj,obj.curr_pos):
# reverting changes
updateCollision(obj, preservedArr.copy())
# breaking the loop
break
# [0,0],[1,0],[2,0],
# [0,1],[1,1],[2,1],
# [0,2],[1,2],[2,2],
# [0,3],[1,3],[2,3],
# [0,4],[1,4],[2,4]
txtDict = {
'0':[
[0,0],[1,0],[2,0],
[0,1], [2,1],
[0,2], [2,2],
[0,3], [2,3],
[0,4],[1,4],[2,4]
],
'1':[
[0,0],
[0,1],
[0,2],
[0,3],
[0,4]
],
'2':[
[0,0],[1,0],[2,0],
[2,1],
[0,2],[1,2],[2,2],
[0,3],
[0,4],[1,4],[2,4]
],
'3':[
[0,0],[1,0],[2,0],
[2,1],
[0,2],[1,2],[2,2],
[2,3],
[0,4],[1,4],[2,4]
],
'4':[
[0,0], [2,0],
[0,1], [2,1],
[0,2],[1,2],[2,2],
[2,3],
[2,4]
],
'5':[
[0,0],[1,0],[2,0],
[0,1],
[0,2],[1,2],[2,2],
[2,3],
[0,4],[1,4],[2,4]
],
'6':[
[0,0],[1,0],[2,0],
[0,1],
[0,2],[1,2],[2,2],
[0,3], [2,3],
[0,4],[1,4],[2,4]
],
'7':[
[0,0],[1,0],[2,0],
[2,1],
[2,2],
[2,3],
[2,4]
],
'8':[
[0,0],[1,0],[2,0],
[0,1], [2,1],
[0,2],[1,2],[2,2],
[0,3], [2,3],
[0,4],[1,4],[2,4]
],
'9':[
[0,0],[1,0],[2,0],
[0,1], [2,1],
[0,2],[1,2],[2,2],
[2,3],
[0,4],[1,4],[2,4]
],
'A':[
[0,0],[1,0],[2,0],
[0,1], [2,1],
[0,2],[1,2],[2,2],
[0,3], [2,3],
[0,4], [2,4]
],
'B':[
[0,0],[1,0],
[0,1], [2,1],
[0,2],[1,2],
[0,3], [2,3],
[0,4],[1,4]
],
'C':[
[1,0],[2,0],
[0,1],
[0,2],
[0,3],
[1,4],[2,4]
],
'D':[
[0,0],[1,0],
[0,1], [2,1],
[0,2], [2,2],
[0,3], [2,3],
[0,4],[1,4],
],
'E':[
[0,0],[1,0],[2,0],
[0,1],
[0,2],[1,2],
[0,3],
[0,4],[1,4],[2,4]
],
'F':[
[0,0],[1,0],[2,0],
[0,1],
[0,2],[1,2],
[0,3],
[0,4],
],
'G':[
[0,0],[1,0],[2,0],
[0,1],
[0,2], [2,2],
[0,3], [2,3],
[0,4],[1,4],[2,4]
],
'H':[
[0,0], [2,0],
[0,1], [2,1],
[0,2],[1,2],[2,2],
[0,3], [2,3],
[0,4], [2,4]
],
'I':[
[0,0],[1,0],[2,0],
[1,1],
[1,2],
[1,3],
[0,4],[1,4],[2,4]
],
'J':[
[2,0],
[2,1],
[0,2], [2,2],
[0,3], [2,3],
[1,4]
],
'K':[
[0,0], [2,0],
[0,1], [2,1],
[0,2],[1,2],
[0,3], [2,3],
[0,4], [2,4]
],
'L':[
[0,0],
[0,1],
[0,2],
[0,3],
[0,4],[1,4],[2,4]
],
'M':[
[0,0], [4,0],
[0,1],[1,1], [3,1],[4,1],
[0,2], [2,2], [4,2],
[0,3], [4,3],
[0,4], [4,4]
],
'N':[
[0,0], [3,0],
[0,1],[1,1], [3,1],
[0,2], [2,2],[3,2],
[0,3], [3,3],
[0,4], [3,4]
],
'O':[
[1,0],
[0,1], [2,1],
[0,2], [2,2],
[0,3], [2,3],
[1,4]
],
'P':[
[0,0],[1,0],[2,0],
[0,1], [2,1],
[0,2],[1,2],[2,2],
[0,3],
[0,4],
],
'Q':[
[1,0],[2,0],
[0,1], [3,1],
[0,2], [3,2],
[0,3], [2,3],[3,3],
[1,4],[2,4],[3,4],[4,4]
],
'R':[
[0,0],[1,0],
[0,1], [2,1],
[0,2],[1,2],
[0,3], [2,3],
[0,4], [2,4]
],
'S':[
[0,0],[1,0],[2,0],
[0,1],
[0,2],[1,2],[2,2],
[2,3],
[0,4],[1,4],[2,4]
],
'T':[
[0,0],[1,0],[2,0],
[1,1],
[1,2],
[1,3],
[1,4]
],
'U':[
[0,0], [2,0],
[0,1], [2,1],
[0,2], [2,2],
[0,3], [2,3],
[0,4],[1,4],[2,4]
],
'V':[
[0,0], [2,0],
[0,1], [2,1],
[0,2], [2,2],
[0,3], [2,3],
[1,4]
],
'W':[
[0,0], [4,0],
[0,1], [4,1],
[0,2], [2,2], [4,2],
[0,3], [2,3], [4,3],
[1,4], [3,4]
],
'X':[
[0,0], [2,0],
[0,1], [2,1],
[1,2],
[0,3], [2,3],
[0,4], [2,4]
],
'Y':[
[0,0], [2,0],
[0,1], [2,1],
[1,2],
[1,3],
[1,4]
],
'Z':[
[0,0],[1,0],[2,0],
[2,1],
[1,2],
[0,3],
[0,4],[1,4],[2,4]
],
'+':[
[1,1],
[0,2],[1,2],[2,2],
[1,3]
],
'-':[
[0,2],[1,2],[2,2]
],
'*':[
[0,2], [2,2],
[1,3],
[0,4], [2,4]
],
'/':[
[2,1],
[1,2],
[0,3]
]
}
def txtObj(txt, cursor, fill, zValue, stayInFrame=True,collision=False):
tLength = cursor[0]
for c in txt:
if c == '1':
tLength += 2
elif c in "MWQ":
tLength += 6
elif c == "N":
tLength += 5
else:
tLength += 4
if tLength > 20:
raise Exception("txt length exceeded X pixel limit for the text = " + txt)
else:
pointArr = []
travellingCursor = cursor.copy()
for c in txt:
charPix = txtDict[c]
for coord in charPix:
pointArr.append([
coord[0] + travellingCursor[0],
coord[1] + travellingCursor[1]
])
if c == '1':
travellingCursor[0] += 2
elif c in "MWQ":
travellingCursor[0] += 6
elif c == "N":
travellingCursor[0] += 5
else:
travellingCursor[0] += 4
# centralize
for index in range(len(pointArr)):
pointArr[index] = [
pointArr[index][0] - cursor[0],
pointArr[index][1] - cursor[1]
]
return object({
fill:pointArr
},{
"z_value": zValue,
"pos": cursor,
"stayInFrame": stayInFrame,
"collision": collision
})
def rectangleObj(C1, C2, fill, zValue, stayInFrame=True,collision=False):
Xsteps = C2[0] - C1[0]
Ysteps = C2[1] - C1[1]
if (Xsteps == 0) or (Ysteps == 0):
return lineObj(C1, C2, fill, zValue, stayInFrame)
Ydir = int(Ysteps / abs(Ysteps))
Xdir = int(Xsteps / abs(Xsteps))
pointArr = []
yPos = C1[1]
for i in range(Ysteps + 1):
j = C1[0] - Xdir # travelling X
while j != C2[0]:
j += Xdir
pointArr.append([j, yPos])
yPos += Ydir
yPos -= Ydir
# centralize
for index in range(len(pointArr)):
pointArr[index] = [
pointArr[index][0] - C1[0],
pointArr[index][1] - C1[1]
]
return object({
fill:pointArr
},{
"z_value": zValue,
"pos": C1,
"stayInFrame": stayInFrame,
"collision": collision
})
def lineObj(C1, C2, fill, zValue, stayInFrame=True,collision=False):
# this function returns a 'object' which is a line from coord C1 to C2 => coord = [C1, C2]
# making point array
Xsteps = C2[0] - C1[0]
Ysteps = C2[1] - C1[1]
pointArr = []
if (Xsteps == 0) and (Ysteps != 0):
# veticle Line
Ydir = int(Ysteps / abs(Ysteps))
j = C1[1] - Ydir # travelling Y
while j != C2[1]:
j += Ydir
pointArr.append([C1[0],j])
elif (Ysteps == 0) and (Xsteps != 0):
# horizontal Line
Xdir = int(Xsteps / abs(Xsteps))
j = C1[0] - Xdir # travelling X
while j != C2[0]:
j += Xdir
pointArr.append([j,C1[1]])
else:
Xdir = int(Xsteps / abs(Xsteps))
Ydir = int(Ysteps / abs(Ysteps))
j = [] # travelling coordinate
first = True
while j != C2:
if first:
first = False
j = C1.copy()
pointArr.append(j)
else:
inst_Ysteps = C2[1] - j[1]
inst_Xsteps = abs(C2[0] - j[0]) # keeping X positive always
dY = round(inst_Ysteps / inst_Xsteps)
j = [j[0] + Xdir, j[1]]
if abs(dY) == 0:
pointArr.append(j)
for c in range(abs(dY)):
j = [j[0], j[1] + Ydir]
pointArr.append(j)
# centralize
for index in range(len(pointArr)):
pointArr[index] = [
pointArr[index][0] - C1[0],
pointArr[index][1] - C1[1]
]
return object({
fill:pointArr
},{
"z_value": zValue,
"pos": C1,
"stayInFrame": stayInFrame,
"collision": collision
})
def move_snake():
temp1 = SnakeHead.curr_pos
offset(SnakeHead, SnakeDirection)
if SnakeHead.curr_pos != temp1: # this if statement basically checks whether the snake was able to move,
for i in range(1,(len(snakeArr))): #this for loop moves the snake body
temp2 = snakeArr[i].curr_pos
snakeArr[i].curr_pos = temp1
temp1 = temp2
if SnakeHead.curr_pos == apple.curr_pos: #extends snake body when it eats an apple
objArr.append(object({"#00ff00":[[0,0]]},
{"z_value": 5,
"pos": temp1,
"stayInFrame": True,
"collision" : True
}))
snakeArr.append(objArr[-1])
apple.curr_pos = [random.randint(1,18),random.randint(4,38)]
# while apple.curr_pos in snakeArr.curr_pos:
# apple.curr_pos = [random.randint(1,18),random.randint(4,38)]
else:
print("game Over")
print(len(snakeArr))
#if it reaches the else statement, then it means that the snake has collided with something
def getKeyState(key):
# returns True if th stated key is presssed
if keyboard.is_pressed(key): # if key 'q' is pressed
return True
return False
objArr = [] # a list of all objects that exist on screen (only objects in this list will be rendered)
end = False # make it True when the game has ended will end the code
once = True # breaker
timeGone = 0 # time Gone
currTime = time.time()
deltaTime = 0
# =================================================================================================
# =================================================================================================
# define your games cross-frame variables here, these variables can store data and carry it across frames
# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
apple = None
snakeArr =[]
SnakeHead = None
SnakeDirection = [0,-1]
obj2 =None
obj1 = None
score = 0
count = 0
# ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
def gameFrame(): # MAKE YOUR GAME IN THIS FUNCTIONS (will be called every frame)
# the object acts as a incompressible / indestructible entity that can translate in space
# the object structure is defined as follows
# assume the object's origin to be at the origin of cartesian system then make a dictionary as follows
# {
# "hexValueOfColor" : [(x1,y1),(x2,y2),(x3,y3)],
# "hexValueOfColor" : [(x1,y1),(x2,y2),(x3,y3)],
# "hexValueOfColor" : [(x1,y1),(x2,y2),(x3,y3)],
# ...
# }
# There can be many pixels of same color thus the above coloring system is taken
# color of indivisual pixel can be changed later on using the function provided in this class [not recommended though
# considering the simplicity of our display aka LED MATRIX]
# 'data' is a dictionary consisting of the following
# {
# "z_value" : int,
# "pos" : [int, int],
# "stayInFrame" : True,
# "collision" : True,
# "rotation" : False
# }
# 'z_value' : indicates the position of the object in z axis, higher the value further it is infront of the LED MATRIX,
# thus 0 is at the back and 10 is infront if in any scenario two objects overlap z_value will decide which will be
# displayed infront
# 'pos' : it represents the (X,Y) coordinate of the origin of the object on the LED MATRIX
# 'stayInFrame' : a basic system that prevent the object from going out of bound
# 'collision' : a basic system that prevent the object from colliding with other objects
# objects will collide only if offset() function is used and both the objects have "collision" set to True also both should
# share the same layer (i.e. they have same zVal)
# 'rotation' : set it to true if you want the object to rotate in future, cannot be changed after object is created
#
# once you have created an object you must add it to the "objArr" list, only the objects in this list shall be rendered
# also the collision will be checked against the objects currently present in this list
# if an object is spawned on top of another object such that both are COLLIDING it will disable both of them from moving
# at all ... you'll have to kill the objects in such a scenario
# to KILL an object just remove it from the "objArr" list
#
# to move an object by (X,Y) units use offset function, remember this function takes input of the offset not the final position
# to use the function follow this syntax
# offset(object, [X,Y])
# object = to the object that you want to move and [X,Y] is the offset vector
#
# to rotate an object use rotate(obj, amt) function to do that the object rotates in CCW direction by 90 * amt degrees
# use negative values for CW rotation
#
# if at some point you want to access the list of all pixels alongwith the colors just access the "matrix" variable you will need
# to call it via global keyword "global matrix"
#
# to change the background color you may access the global "background" variable and change the color
#
# object.pixelArr will give you the relative matrix of the pixels of the particular matrix ... Access it but dont try to change it
#
# to change color of the pixels you may use "object.changeColor()" function in order to use this function you need to know the
# location of the pixel the location should be same as you used while defining the object... use the following syntax
# object.changeColor([X,Y], color)
# color = HEX VALUE of the color you want to change it to
#
# you may make a pixel invisible by setting its color value to None instead of hex value
# by accessing pixelArr you will find out that each object has a rectangular pixelArr and many of the pixels are set to be invisible
# you may bring them to life by setting them to some colorValue
#
# WARNING : if you try to access pixel that does not exist in pixelArr you will get errors
# WARNING : making pixels visible / invisible wont change their collision behaviour... this can be used to create sort of invisible
# maze like structure (imaginations are on you!!)
#
# ========
# txtObj
# ========
#
# to write text use txtObj it is capable of writing the following characters
#
# 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ +-*/
#
# all of these characters are 5 pixel high and 3 pixel wide