-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (102 loc) · 4.55 KB
/
main.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
import game from game # imports game class
import RPi.GPIO as GPIO
import time
#Serial Pins
SER = 25
SRCLK = 26
RCLK = 27
#Button Pins
bUp = 5
bDown = 6
bLeft = 12
bRight = 13
bForward = 16
bBack = 19
#setup the pin mode for the GPIO
GPIO.setmode(GPIO.BCM)
#turn off the warnings
GPIO.setwarnings(False)
#setup the buttons
GPIO.setup(bUp , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(bDown , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(bLeft , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(bRight , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(bForward, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(bBack , GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#setup the serial lines
GPIO.setup(SER, GPIO.OUT)
GPIO.setup(SRCLK, GPIO.OUT)
GPIO.setup(RCLK, GPIO.OUT)
#Clock Shift Registers to intialize them
GPIO.output(SRCLK,1)
GPIO.output(SER,1)
GPIO.output(RCLK,1)
time.sleep(.0005)
GPIO.output(SRCLK,0)
GPIO.output(SER,0)
GPIO.output(RCLK,0)
def updateOut(gameOb): # send changes to LED matrix
nums = gameop.matrix()
colors = np.zeros((4,4,4,3))
state = gameOb.isWon()
for i in range (0,4): # For each level
for j in range (0,4): # Row
for k in range (0,4): # Colomn
if nums.at(i,j,k) == 2 or nums.at(i,j,k) == 16 or nums.at(i,j,k) == 32: #enable red
colors.at(i,j,k,0) = 1
if nums.at(i,j,k) == 4 or nums.at(i,j,k) == 16 or nums.at(i,j,k) == 64: #enable green
colors.at(i,j,k,1) = 1
if nums.at(i,j,k) == 8 or nums.at(i,j,k) == 32 or nums.at(i,j,k) == 64: #enable blue
colors.at(i,j,k,2) = 1
GPIO.output(RCLK, 0) # enable serail stream to registers
for c in range(0,3): # for each of 3 colors (Red then green then blue)
for k in range (0,4): # For each level
for i in range (0,4): # Row
for j in range (0,4): # Colomn
#begin data shift out;
GPIO.output(SER, colors[i][j][k][c])
GPIO.output(SRCLK,1)
time.sleep(.0005) #Pi Clock speed is too high for registers
GPIO.output(SRCLK,0)
#Clock Register. This latches the current state of the serial buffer to the output
#register, updating the cube;
GPIO.output(RCLK, 1)
return state # Used to end game
fris = game() #intailize a game object
#Set Interupts for buttons. Starting with translations
GPIO.add_event_detect(bUp , GPIO.RISING, callback = fris.up_movement() , bouncetime = 25)
GPIO.add_event_detect(bDown , GPIO.RISING, callback = fris.down_movement() , bouncetime = 25)
GPIO.add_event_detect(bLeft , GPIO.RISING, callback = fris.left_movement() , bouncetime = 25)
GPIO.add_event_detect(bRight , GPIO.RISING, callback = fris.right_movement() , bouncetime = 25)
GPIO.add_event_detect(bForward, GPIO.RISING, callback = fris.forward_movement() , bouncetime = 25)
GPIO.add_event_detect(bBack , GPIO.RISING, callback = fris.backward_movement(), bouncetime = 25)
#Check for win and update board with new random pieces
GPIO.add_event_detect(bUp , GPIO.FALLING, callback = updateOut(fris), bouncetime = 25)
GPIO.add_event_detect(bDown , GPIO.FALLING, callback = updateOut(fris), bouncetime = 25)
GPIO.add_event_detect(bLeft , GPIO.FALLING, callback = updateOut(fris), bouncetime = 25)
GPIO.add_event_detect(bRight , GPIO.FALLING, callback = updateOut(fris), bouncetime = 25)
GPIO.add_event_detect(bForward, GPIO.FALLING, callback = updateOut(fris), bouncetime = 25)
GPIO.add_event_detect(bBack , GPIO.FALLING, callback = updateOut(fris), bouncetime = 25)
textPlay = input("Enable text game? (y/n) ") #Taking user input for the movement choice
while(1): # main game loop
if (textPlay == 'y' or textPlay == 'Y' )
fris.printBoard() # prints the current game board to terminal (Debug / if you don't have an LED Cube)
movement_choice = input("Make your move::::>>>> ") #Taking user input for the movement choice
if movement_choice == "w":
fris.up_movement()
elif movement_choice == "s":
fris.down_movement()
elif movement_choice == "a":
fris.left_movement()
elif movement_choice == "d":
fris.right_movement()
elif movement_choice == ",":
fris.forward_movement()
elif movement_choice == '.':
fris.backward_movement()
#update cube
if (updateOut(fris)):
break
else:
movement_choice = input("Press enter to show current board: ")
fris.printBoard()