9. Maze

from microbit import *
import random

class Maze:

    maze0 = [
        0b11111111111111111111111111111111,
        0b11111111111111111111111111111111,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11111111111111111111111111111111,
        0b11111111111111111111111111111111,
    ]

    maze2 = [
        0b11111111111111111111111111111111,
        0b11111111111111111111111111111111,
        0b11000000000000000000000000000011,
        0b11000111000111111111100011100011,
        0b11000000000000000000000000000011,
        0b11000111111110001000111111100011,
        0b11000000000000000000000000000011,
        0b11000111000111111111100011100011,
        0b11000000000000000000000000000011,
        0b11111111111111111111111111111111,
        0b11111111111111111111111111111111,
    ]

    maze1 = [
        0b11111111111111111111111111111111,
        0b11111111111111111111111111111111,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000111111110001000111111100011,
        0b11000111111110001000111111100011,
        0b11000000000000000000000000000011,
        0b11000000000000000000000000000011,
        0b11000111000111111111100011100011,
        0b11000111000111111111100011100011,
        0b11000000000000000000000000000011,
        0b11111111111111111111111111111111,
        0b11111111111111111111111111111111,
    ]

    def __init__(self):
        self.maze = random.choice([self.maze0, self.maze1, self.maze2])
        self.x = 2
        self.y = 2
        self.score = 0

    def get_dot(self, x, y):
        return (self.maze[y] >> (31 - x)) & 1

    def draw_slice(self, x, y):
        img = Image(5, 5)
        for row in range(0, 5):
            for col in range(0, 5):
                img.set_pixel(col, row, self.get_dot(col + x - 2, row + y - 2) * 5)
        img.set_pixel(2, 2, 9)
        return img

    def run_game(self):
        while True:
            if accelerometer.get_x() > 200 and self.get_dot(self.x + 1, self.y) != 1:
                self.x += 1
            elif accelerometer.get_x() < -200 and self.get_dot(self.x - 1, self.y) != 1:
                self.x -= 1
            elif accelerometer.get_y() > 200 and self.get_dot(self.x, self.y + 1) != 1:
                self.y += 1
            elif accelerometer.get_y() < -200 and self.get_dot(self.x, self.y - 1) != 1:
                self.y -= 1
            display.show(self.draw_slice(self.x, self.y))
            sleep(200)


game = Maze()
game.run_game()
while True:
    display.show(Image.ARROW_W)
    if button_a.is_pressed():
        display.clear()
        game = Maze()
        game.run_game()
    sleep(1000)

Tasks

  1. Add collision detection, when an attempt to move into a wall is made.

  2. Add scores for each move made, and reduce the score when collisions with walls occur.