Skip to content

Commit adb4958

Browse files
committed
Complete code for embedded Python article
1 parent 68b69bf commit adb4958

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

embedded-python/simon_says.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from microbit import *
2+
from random import randrange
3+
4+
# Define left, stay still and right
5+
directions = ["L", "O", "R"]
6+
points = 0
7+
8+
# While the micro:bit is on
9+
while True:
10+
# Pick a random direction
11+
direction = directions[randrange(3)]
12+
display.show(direction)
13+
# Sleep for a second (1000ms)
14+
sleep(1000)
15+
16+
# Get the X-axis (left-right) tilt
17+
acc_x = accelerometer.get_x()
18+
# Determine direction
19+
if acc_x < -200:
20+
user_in = "L"
21+
elif abs(acc_x) < 200:
22+
user_in = "O"
23+
elif acc_x > 200:
24+
user_in = "R"
25+
26+
# Check win condition
27+
if user_in == direction:
28+
# User input correctly
29+
points += 1
30+
else:
31+
display.scroll(points)
32+
display.show(Image.SAD)
33+
points = 0
34+
sleep(1000)

0 commit comments

Comments
 (0)