-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimon.py
202 lines (158 loc) · 4.33 KB
/
simon.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
import RPi.GPIO as GPIO
import time
import random
import wiringpi
from time import sleep
# saved 4 notes frequencies
c_note = 261.6256
d_note = 293.6648
e_note = 329.6276
f_note = 349.2282
# buttons gpioPins
red_btn = 6
yellow_btn = 17
green_btn = 27
blue_btn = 5
# leds gpioPins
red_led = 26
yellow_led = 21
green_led = 20
blue_led = 19
# speaker gpioPin
speaker = 25
# color, sound and buttons settings
lights = [red_led, yellow_led, green_led, blue_led]
buttons = [red_btn, yellow_btn, green_btn, blue_btn]
# sounds = {red: c_note, yellow: d_note, green: e_note, blue: f_note}
sounds = [c_note, d_note, e_note, f_note]
numbers_colors = {1: red, 2: yellow, 3: green, 4: blue}
led_dict = {"red": red_led, "yellow": yellow_led, "green":green_led, "blue":blue_led}
btn_dict = {"red": red_btn, "yellow": yellow_btn, "green":green_btn, "blue":blue_btn}
# game statistics
steps = []
players_turn = 0
# flags for game status
is_game_over = False
is_won_level = False
# main function
def main() :
init_gpio()
start_game()
# reset game
def reset_game() :
is_won_level = False
is_game_over = False
steps = []
GPIO.output(lights.GPIO.LOW)
start_game()
# start the game
def start_game() :
# flash all 4 lights
flash_led(red_led)
flash_led(yellow_led)
flash_led(green_led)
flash_led(blue_led)
while (not is_game_over) :
add_color_to_steps()
display_pattern()
player_turn()
if is_game_over :
game_over()
reset_game()
time.sleep(2)
def player_turn() :
while True :
pass
# deal with user's turn and valid if correct or finished game
def verify_player_selection(color) :
if not is_game_over and not is_won_level :
if color == steps[-1] :
return True
else : return False
else :
return False
# add a color to the steps
def add_color_to_steps() :
number = random.randint(1, 4)
steps.append(numbers_colors.get(number))
''' events '''
# red event
def red_button_callback(channel) :
if verify_player_selection("red") :
flash_led(red_led)
make_sound(c_note)
else :
game_over()
# yellow event
def yellow_button_callback(channel) :
if verify_player_selection("yellow") :
flash_led(yellow_led)
make_sound(d_note)
else :
game_over()
# green event
def green_button_callback(channel) :
if verify_player_selection("green") :
flash_led(green_led)
make_sound(e_note)
else :
game_over()
# blue event
def blue_button_callback(channel) :
if verify_player_selection("blue") :
flash_led(blue_led)
make_sound(f_note)
else :
game_over()
''' events finished '''
# displays the needed colors and sounds
def display_pattern():
for i in steps :
index = 0
if i == "red":
index = 0
elif i == "yellow" :
index = 1
elif i == "green" :
index = 2
else :
index = 3
make_sound(sounds[index])
flash_led(led_dict.get(i))
# when game is over, flash all lights and make a sound
def game_over():
play_sound(200)
# initialize all needed gpio pins
def init_gpio() :
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#buttons
GPIO.setup(red_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(yellow_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(green_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(blue_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#leds
GPIO.setup(red_led, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(yellow_led, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(green_led, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(blue_led, GPIO.OUT, initial=GPIO.LOW)
# add events
GPIO.add_event_detect(red_btn, GPIO.RISING, callback=red_button_callback)
GPIO.add_event_detect(yellow_btn, GPIO.RISING, callback=yellow_button_callback)
GPIO.add_event_detect(green_btn, GPIO.RISING, callback=green_button_callback)
GPIO.add_event_detect(blue_btn, GPIO.RISING, callback=blue_button_callback)
#init speaker
wiringpi.wiringPiSetupGpio()
wiringpi.softToneCreate(speaker)
# set the speaker with a frequency and play a note
def make_sound(sound) :
wiringpi.softToneWrite(speaker, sound)
sleep(0.1)
# handle the flash for the button
def flash_led(led) :
GPIO.output(led, GPIO.HIGH)
time.sleep(2)
GPIO.output(led, GPIO.LOW)
# sets main to start automatically
if __name__ == "__main__":
main()