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
|
"""
A simple maze program. You are the flashing dot and can walk around
using the accelerometer.
"""
import microbit
d = microbit.display
ac = microbit.accelerometer
# the maze data, as binary numbers (outside walls are added automatically)
maze = [
0b0000000000000000,
0b0100010101011110,
0b0100010101010010,
0b0111110100000000,
0b0000000111111110,
0b0111111101000000,
0b0101010001011100,
0b0101000100000100,
0b0100011111111100,
0b0101010001000110,
0b0101000100010010,
0b0101010111010110,
0b0111010101010010,
0b0000010100010010,
0b0111110111111110,
0b0000000000000000,
]
def get_maze(x, y):
if 0 <= x < 16 and 0 <= y < 16:
return (maze[y] >> (15 - x)) & 1
else:
return 1
def draw(x, y, tick):
img = microbit.Image(5,5)
for j in range(5):
for i in range(5):
img.set_pixel(i, j, get_maze(x + i - 2, y + j - 2)*5)
# draw the player, flashing
img.set_pixel(2, 2, (tick & 1)*4+5)
d.show(img)
def main():
x = 0
y = 0
tick = 0
while True:
tick += 1
if tick == 4:
# walk around, with collision detection
tick = 0
if ac.get_x() > 200 and get_maze(x + 1, y) == 0:
x += 1
elif ac.get_x() < -200 and get_maze(x - 1, y) == 0:
x -= 1
elif ac.get_y() > 200 and get_maze(x, y + 1) == 0:
y += 1
elif ac.get_y() < -200 and get_maze(x, y - 1) == 0:
y -= 1
x = min(15, max(0, x))
y = min(15, max(0, y))
# draw the maze
draw(x, y, tick)
microbit.sleep(50)
main()
|