-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.py
80 lines (64 loc) · 1.45 KB
/
switch.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
import RPi.GPIO as GPIO
from kbhit import KBHit
import time
GPIO.setmode(GPIO.BCM)
pinList = [2,3,4,17,27,22,10,9]
class switch:
"""Switch class for game"""
pos = 1;
def __init__(self):
self.pos = 0
def game_start(self,position=0):
for i in range(position,8):
round = self.game_round(i)
if round == False:
print "Sorry, try again."
break
print "Win! Next round..."
print "Finished Game!"
def game_round(self,position):
# set round speed and guess
speed = 1./(1+position)
guess = -1
stop = False
# turn on GPIO pins
for i in pinList[position:]:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, GPIO.HIGH)
# turn on correct guesses
for i in pinList[0:position]:
GPIO.output(i, GPIO.LOW)
#start kbhit
kb = KBHit()
# start loop
while True:
for i in pinList[position:]:
GPIO.output(i, GPIO.LOW)
# check keypress
if kb.kbhit():
c = kb.getch()
if ord(c) != 27:
stop = True
guess = pinList.index(i)-1
break
time.sleep(speed)
# check keypress
if stop:
break
pinList.reverse()
for i in pinList[0:len(pinList)-position]:
GPIO.output(i,GPIO.HIGH)
if kb.kbhit():
c = kb.getch()
if ord(c) != 27:
stop = True
guess = len(pinList)-(pinList.index(i)+1)
pinList.reverse()
time.sleep(speed)
if stop:
break
pinList.reverse()
# check results
print guess
print position
return (guess == position)