-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimonSays.py
138 lines (117 loc) · 2.71 KB
/
SimonSays.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
# Add your Python code here. E.g.
from microbit import *
import random
#red_led = 0
#orange_led = 1
#yellow_led = 8
#green_led = 12
#red_switch = 4
#orange_switch = 6
#yellow_switch = 7
#green_switch = 3
code = "BABBB"
correctSeq = "#"
currentSeq = "#"
def genRandomColour():
i = random.randint(0, 3)
if i == 0:
return "r"
elif i == 1:
return "o"
elif i == 2:
return "y"
elif i == 3:
return "g"
def appendRandomSequence(length):
global correctSeq
for i in range(0, length):
if correctSeq == "#":
correctSeq = genRandomColour()
else:
correctSeq += genRandomColour()
def flash(colour):
if colour == "#":
return
if colour == "r":
pin0.write_digital(1)
sleep(500)
pin0.write_digital(0)
sleep(500)
elif colour == "g":
pin12.write_digital(1)
sleep(500)
pin12.write_digital(0)
sleep(500)
elif colour == "o":
pin1.write_digital(1)
sleep(500)
pin1.write_digital(0)
sleep(500)
elif colour == "y":
pin8.write_digital(1)
sleep(500)
pin8.write_digital(0)
sleep(500)
def flashAll():
pin0.write_digital(1)
pin12.write_digital(1)
pin1.write_digital(1)
pin8.write_digital(1)
sleep(500)
pin0.write_digital(0)
pin12.write_digital(0)
pin1.write_digital(0)
pin8.write_digital(0)
def enterCode(colour):
if colour == "#":
return
global currentSeq
#display.on()
if currentSeq == "#":
currentSeq = colour
else:
currentSeq = currentSeq + colour
#display.off()
#flash(colour)
display.off()
pin4.read_digital()
pin6.read_digital()
pin7.read_digital()
pin3.read_digital()
def flashSeq():
for char in correctSeq:
flash(char)
appendRandomSequence(3)
i = 0
while i < 3:
appendRandomSequence(1)
flashSeq()
while correctSeq != currentSeq:
lastchar = "#"
if pin4.read_digital():
lastchar = "r"
elif pin6.read_digital():
lastchar = "o"
elif pin7.read_digital():
lastchar = "y"
elif pin3.read_digital():
lastchar = "g"
enterCode(lastchar)
if (correctSeq[len(currentSeq) - 1] != currentSeq[len(currentSeq) - 1]) and (currentSeq != "#"):
flashAll()
currentSeq = "#"
#display.on()
correctSeq = "#"
appendRandomSequence(4)
#display.off()
i = 0
sleep(500)
flashSeq()
else:
flash(lastchar)
currentSeq = "#"
i += 1
#flashAll()
display.on()
while True:
display.scroll(code)