-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprev_game.py
executable file
·360 lines (243 loc) · 7.52 KB
/
prev_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
#definitions
#add GPIO library
import RPi.GPIO as GPIO
#add time library
import time
import random
#set how to refer the RPi pins
GPIO.setmode(GPIO.BOARD)
#pin for push_buttons
PIN_b1=37
PIN_b2=35
PIN_b3=33
PIN_b4=31
#LEDs pinNumber
PIN_l1=13
PIN_l2=11
PIN_l3=7
PIN_l4=5
PIN_lgreen=38
PIN_lred=40
pin_leds = {1:PIN_l1, 2:PIN_l2, 3:PIN_l3, 4:PIN_l4};
class Game:
def __init__(self):
self.state=1
self.set=1
self.n=3
self.n_max=15
self.vel=1.0
self.max_vel=5
self.answer=1
self.score=0
self.led_min=1
self.led_max=4
self.array=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
self.guess=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
def play(self):
print "game"
self.score=0
self.leds_sequence()
self.guessing()
self.evaluation()
def leds_sequence(self):
l1.off()
l2.off()
l3.off()
l4.off()
for i in range(0,self.n):
self.array[i]=random.randrange(self.led_min,self.led_max+1)
print (str(i) + ": " + "L" + str(self.array[i]) )
time.sleep(1.0/self.vel)
#led turn on
GPIO.output(pin_leds[self.array[i]],1)#turn on led
#sleep time in seconds
time.sleep(1.0/self.vel)
#turn led off
GPIO.output(pin_leds[self.array[i]],0)#turn on led
return;
def guessing(self):
print "press the secuence now: "
for i in range(0,self.n): #to iterate on the factors of the number
#push button
num=input('What is the next led: ')
self.guess[i]=num
#led turn on
return;
def evaluation(self):
#evaluation
self.answer=1
for i in range(0,self.n):
if self.array[i]==self.guess[i]:
self.answer*=1
self.score+=1
sign=" = "
text=" correct!"
else:
self.answer=0
sign=" != "
text=" incorrect!"
print (str(i) + ": " + "L" + str(self.array[i]) + sign + "B" + str(self.guess[i]) + text + " answer value " + str(self.answer))
if self.answer==1:
print "Contratulations you win"
GPIO.output(PIN_lgreen,1)
time.sleep(2.0)
GPIO.output(PIN_lgreen,0)
#green leed on
else:
print ("you score " + str(self.score) + " out of " + str(self.n))
return;
def set_size(self):
print "set size: press B1 increase, B2 for set"
self.n=1
self.set=1
while self.set==1:
print "current size"
self.show_binary_num()
self.set=input('incrase?')
if self.set==1:
self.n+=1
if self.n>self.n_max:
self.n=1
print self.n
return;
def show_binary_num(self):
k=self.n
l1.off()
l2.off()
l3.off()
l4.off()
if k>7:
l1.on()
k-=8
m="1 "
else:
m="0 "
if k>3:
l2.on()
k-=4
m+="1 "
else:
m+="0 "
if k>1:
l3.on()
k-=2
m+="1 "
else:
m+="0 "
if k>0:
l4.on()
m+="1"
else:
m+="0"
print m
return;
def set_vel(self):
print "set vel: press B1 increase, B2 for set"
self.vel=1.00
self.set=1
while self.set==1:
self.set=input('incrase?')
#blink led
if self.set==1:
self.vel+=1
if self.vel>self.max_vel:
self.vel=1.00
print self.vel
return;
def get_button(self):
result=0
while result==0:
if b1.is_pressed():
result=1
if b2.is_pressed():
result=2
if b3.is_pressed():
result=3
if b4.is_pressed():
result=4
return result;
class Led:
def __init__(self, pinNum):
self.pinNum=pinNum
GPIO.setup(pinNum,GPIO.OUT)#defined pin as output
GPIO.output(self.pinNum,0)#turn off led
print "Created Led at pin number:",self.pinNum
def on(self):
GPIO.output(self.pinNum,1)#turn on led
def off(self):
GPIO.output(self.pinNum,0)#turn off led
def set(self, value):
GPIO.output(self.pinNum, value)
def __del__(self):
GPIO.output(self.pinNum,0)#turn on led
class Button:
def __init__(self, pinNum, name):
self.pinNum=pinNum
self.name=name
self.previus_state=0
GPIO.setup(self.pinNum,GPIO.IN)#defined pin as input
print(self.name + " button created at pin: "+ str(self.pinNum))
def pressed_once(self):
value = False
if GPIO.input(self.pinNum)==False:
self.previus_state=0
value = False
if GPIO.input(self.pinNum) and self.previus_state==0:
print(self.name + " button pressed." )
self.previus_state=1
value = True
return value;
def is_pressed(self):
return GPIO.input(self.pinNum);
#create objects Button(pinNumber)
b1=Button(PIN_b1,"b1")
b2=Button(PIN_b2,"b2")
b3=Button(PIN_b3,"b3")
b4=Button(PIN_b4,"b4")
#create objects LED(pinNumber)
l1=Led(pin_leds[1])
l2=Led(pin_leds[2])
l3=Led(pin_leds[3])
l4=Led(pin_leds[4])
lgreen=Led(PIN_lgreen)
lred=Led(PIN_lred)
#create game
game=Game()
print "Press: B1 to play, B2 for set_size, B3 for set_vel, B4 to exit:"
#game.state=input('Press: B1 to play, B2 for set_size, B3 for set_vel, B4 to exit: ')
game.state=game.get_button()
while game.state!=4:
if game.state==1:
game.play()
elif game.state==2:
game.set_size()
game.play()
elif game.state==3:
game.set_vel()
game.play()
#game.state=input('Press: B1 to play, B2 for set_size, B3 for set_vel, B4 to exit: ')
game.state=game.get_button()
#program
try:
while True:
lred.on()
if GPIO.input(b4.pinNum)==False:
l4.off()
b4.previus_state=0
if GPIO.input(b4.pinNum) and b4.previus_state==0:
print(b4.name + " button pressed." )
l4.on()
b4.previus_state=1
l1.set(b1.pressed_once())
l1.set(b1.is_pressed())
l2.set(b2.is_pressed())
l3.set(b3.is_pressed())
except KeyboardInterrupt:
del l1
del l2
del l3
del l4
del lred
del lgreen
GPIO.cleanup() #clean up
3