Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP2215 - Simon Says
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
djp1g21
COMP2215 - Simon Says
Commits
396d127a
Commit
396d127a
authored
May 14, 2023
by
djp1g21
Browse files
Options
Downloads
Patches
Plain Diff
Added Solution
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Solution.py
+202
-0
202 additions, 0 deletions
Solution.py
with
202 additions
and
0 deletions
Solution.py
0 → 100644
+
202
−
0
View file @
396d127a
"""
Author: Daniel Piper (djp1g21)
"""
from
machine
import
Pin
,
PWM
from
utime
import
sleep
import
neopixel
,
random
,
math
# Get the relevant i/o
led
=
neopixel
.
NeoPixel
(
machine
.
Pin
(
28
),
1
,
bpp
=
3
)
buzzer
=
PWM
(
Pin
(
18
))
button1
=
Pin
(
20
,
Pin
.
IN
,
Pin
.
PULL_UP
)
button2
=
Pin
(
21
,
Pin
.
IN
,
Pin
.
PULL_UP
)
button3
=
Pin
(
22
,
Pin
.
IN
,
Pin
.
PULL_UP
)
# Colour sequence
sequence
:
list
[
str
]
=
[]
# Duration of first flash
MAX_DURATION
:
int
=
0.8
# Equation for flash duration based on level - duration decreases with each new level at a decreasing rate
def
flash_duration
(
level
:
int
)
->
float
:
return
MAX_DURATION
/
math
.
sqrt
(
level
)
# Clear LED
def
show_none
()
->
None
:
led
[
0
]
=
(
0
,
0
,
0
)
led
.
write
()
# Red LED
def
show_red
()
->
None
:
led
[
0
]
=
(
255
,
0
,
0
)
led
.
write
()
# Green LED
def
show_green
()
->
None
:
led
[
0
]
=
(
0
,
255
,
0
)
led
.
write
()
# Blue LED
def
show_blue
()
->
None
:
led
[
0
]
=
(
0
,
0
,
255
)
led
.
write
()
# Red LED and sound for level's duration
def
flash_red
(
level
:
float
)
->
None
:
duration
=
flash_duration
(
level
)
show_red
()
buzzer
.
freq
(
330
)
buzzer
.
duty_u16
(
1000
)
sleep
(
duration
)
buzzer
.
duty_u16
(
0
)
show_none
()
sleep
(
duration
)
# Green LED and sound for level's duration
def
flash_green
(
level
:
float
)
->
None
:
duration
=
flash_duration
(
level
)
show_green
()
buzzer
.
freq
(
440
)
buzzer
.
duty_u16
(
1000
)
sleep
(
duration
)
buzzer
.
duty_u16
(
0
)
led
[
0
]
=
(
0
,
0
,
0
)
led
.
write
()
sleep
(
duration
)
# Blue LED and sound for level's duration
def
flash_blue
(
level
:
float
)
->
None
:
duration
=
flash_duration
(
level
)
show_blue
()
buzzer
.
freq
(
587
)
buzzer
.
duty_u16
(
1000
)
sleep
(
duration
)
buzzer
.
duty_u16
(
0
)
led
[
0
]
=
(
0
,
0
,
0
)
led
.
write
()
sleep
(
duration
)
# Append new colour to sequence
def
update_sequence
()
->
None
:
global
sequence
choice
=
random
.
randint
(
1
,
3
)
if
choice
==
1
:
sequence
.
append
(
"
red
"
)
elif
choice
==
2
:
sequence
.
append
(
"
green
"
)
else
:
sequence
.
append
(
"
blue
"
)
# Play entire sequence for user
def
play_sequence
()
->
None
:
global
sequence
level
=
len
(
sequence
)
for
choice
in
sequence
:
if
choice
==
"
red
"
:
flash_red
(
level
)
elif
choice
==
"
green
"
:
flash_green
(
level
)
else
:
flash_blue
(
level
)
# Wait for input - output corresponding colour for button pressed
def
take_input
()
->
str
:
while
button1
.
value
()
and
button2
.
value
()
and
button3
.
value
():
continue
if
not
button1
.
value
():
return
"
red
"
if
not
button2
.
value
():
return
"
green
"
return
"
blue
"
# Input loop for user attempt
def
attempt_sequence
()
->
bool
:
global
sequence
position
=
0
level
=
len
(
sequence
)
failed
=
False
while
position
<
level
:
choice
=
take_input
()
if
choice
!=
sequence
[
position
]:
return
False
if
choice
==
"
red
"
:
flash_red
(
level
)
elif
choice
==
"
green
"
:
flash_green
(
level
)
else
:
flash_blue
(
level
)
position
+=
1
return
True
def
success
()
->
None
:
led
[
0
]
=
(
50
,
50
,
50
)
led
.
write
()
buzzer
.
freq
(
659
)
buzzer
.
duty_u16
(
1000
)
sleep
(
0.05
)
buzzer
.
duty_u16
(
0
)
sleep
(
0.05
)
buzzer
.
duty_u16
(
1000
)
sleep
(
0.05
)
buzzer
.
duty_u16
(
0
)
sleep
(
0.2
)
# Animation for failed sequence
def
fail
()
->
None
:
led
[
0
]
=
(
80
,
60
,
0
)
led
.
write
()
buzzer
.
freq
(
659
)
buzzer
.
duty_u16
(
1000
)
sleep
(
0.2
)
led
[
0
]
=
(
140
,
120
,
0
)
led
.
write
()
buzzer
.
freq
(
440
)
sleep
(
0.2
)
led
[
0
]
=
(
80
,
40
,
0
)
led
.
write
()
buzzer
.
freq
(
330
)
sleep
(
0.2
)
buzzer
.
duty_u16
(
0
)
show_none
()
# Animation to play at start of each game
def
restart_animation
()
->
None
:
flash_red
(
100
)
flash_green
(
100
)
flash_blue
(
100
)
flash_red
(
100
)
flash_green
(
100
)
flash_blue
(
100
)
# Initial animation and game loop
restart_animation
()
while
True
:
update_sequence
()
play_sequence
()
if
attempt_sequence
():
success
()
continue
fail
()
sleep
(
0.8
)
restart_animation
()
sequence
=
[]
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment