Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP2215 - Colour sequence game
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
mzxs1g21
COMP2215 - Colour sequence game
Commits
aaaece42
Commit
aaaece42
authored
2 years ago
by
mzxs1g21
Browse files
Options
Downloads
Patches
Plain Diff
Colour sequence game
parent
7c21df08
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
Colour_sequence.py
+85
-0
85 additions, 0 deletions
Colour_sequence.py
with
85 additions
and
0 deletions
Colour_sequence.py
0 → 100644
+
85
−
0
View file @
aaaece42
import
machine
import
neopixel
import
random
import
time
# Assign buttons and LED
button_pins
=
[
20
,
21
,
22
]
# Pins for buttons 1, 2, and 3 respectively
# Set up buttons as inputs with pull-down resistors
for
pin
in
button_pins
:
machine
.
Pin
(
pin
,
machine
.
Pin
.
IN
,
machine
.
Pin
.
PULL_DOWN
)
np
=
neopixel
.
NeoPixel
(
machine
.
Pin
(
28
),
1
)
np
[
0
]
=
(
0
,
0
,
0
)
# Initially set the led to nothing
np
.
write
()
# Blinks the LED green once
def
blink_green
():
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
time
.
sleep_ms
(
500
)
np
[
0
]
=
(
0
,
255
,
0
)
np
.
write
()
time
.
sleep_ms
(
500
)
# Blinks the LED red once
def
blink_red
():
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
time
.
sleep_ms
(
500
)
np
[
0
]
=
(
255
,
0
,
0
)
np
.
write
()
time
.
sleep_ms
(
500
)
# Blinks the LED blue once
def
blink_blue
():
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
time
.
sleep_ms
(
500
)
np
[
0
]
=
(
0
,
0
,
255
)
np
.
write
()
time
.
sleep_ms
(
500
)
button_press_duration
=
0
# Generate random a sequence of random length of nummbers from 0,1,2
rep
=
random
.
randint
(
3
,
5
)
sequence
=
[
random
.
randint
(
0
,
2
)
for
i
in
range
(
rep
)]
for
button
in
sequence
:
if
button
==
0
:
blink_red
()
elif
button
==
1
:
blink_green
()
elif
button
==
2
:
blink_blue
()
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
button_presses
=
[]
while
len
(
button_presses
)
<
rep
:
# Check each button to see if it's been pressed
for
i
,
pin
in
enumerate
(
button_pins
):
if
machine
.
Pin
(
pin
).
value
()
==
0
:
button_presses
.
append
(
i
)
time
.
sleep
(
0.3
)
# Wait a short time to avoid registering multiple presses
if
sequence
==
button_presses
:
print
(
"
Correct!
"
)
blink_green
()
blink_green
()
blink_green
()
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
else
:
print
(
"
Incorrect!
"
)
blink_red
()
blink_red
()
blink_red
()
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
\ No newline at end of file
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