Created
June 13, 2017 16:07
-
-
Save seirl/e5abacbe9dafa27dfc2b05295796b306 to your computer and use it in GitHub Desktop.
Microbit Maze
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from microbit import * | |
MAZE = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], | |
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], | |
[1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1], | |
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1], | |
[1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1], | |
[1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], | |
[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1], | |
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1], | |
[1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1], | |
[1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0], | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] | |
def wall(r, c): | |
if not (0 <= r < len(MAZE) and 0 <= c < len(MAZE[0])): | |
return 0 | |
return MAZE[r][c] | |
pos = [1, 1] | |
i = 0 | |
while True: | |
# Display the base pixels | |
for mr in range(5): | |
for mc in range(5): | |
display.set_pixel(mr, mc, wall(mr + pos[0] - 2, mc + pos[1] - 2) * 9) | |
# Movement | |
if any([button_a.was_pressed(), | |
button_b.was_pressed()]): | |
dir = [0, 0] | |
x = accelerometer.get_x() | |
y = accelerometer.get_y() | |
print(x,y) | |
if abs(x) > abs(y): | |
if x > 20: | |
dir = [1, 0] | |
elif x < -20: | |
dir = [-1, 0] | |
elif abs(y) > abs(x): | |
if y > 20: | |
dir = [0, 1] | |
elif y < -20: | |
dir = [0, -1] | |
npos = [pos[0] + dir[0], pos[1] + dir[1]] | |
if not wall(npos[0], npos[1]): | |
pos = npos | |
# Winning position | |
if pos == [9, 11]: | |
display.scroll("YOU WIN") | |
pos = [1, 1] | |
# Display the cursor | |
display.set_pixel(2, 2, 9 * (i % 2)) | |
i += 1 | |
sleep(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment