def __init__(self, nick):
     radio.on()
     radio.send(nick)
     self.nick = nick
     self.image_a = microbit.Image().copy()
     self.image_b = microbit.Image().copy()
     self.image_up = microbit.Image().copy()
     self.image_down = microbit.Image().copy()
Beispiel #2
0
def get_ident(wait=1000):
    max_ident = 0
    loading_display = microbit.Image(BLANK_IMAGE)
    wait /= 25

    configure_radio()
    for pixel in sorted(range(25), key=lambda x: random.random()):
        start = microbit.running_time()
        message = radio.receive_bytes()
        while message:
            try:
                sender, *_ = message
                max_ident = max(max_ident, sender)
            except ValueError:
                pass

            message = radio.receive_bytes()

        loading_display.set_pixel(pixel % 5, pixel // 5, 9)
        microbit.display.show(loading_display)
        end = microbit.running_time() - start
        if end < wait:
            microbit.sleep(wait - end)

    microbit.display.scroll("#{}".format(max_ident + 1))
    return max_ident + 1
def display_boxes_and_players(top, middle_top, middle_bottom, bottom,
                              position):
    """A Non-Fruitful Function that handles the graphics on the microbit by taking the boxes positions and converting it to a string"""
    image = ""
    for index in range(5):
        image = image + str(top[index])

    image = image + ":"
    for index in range(5):
        image = image + str(middle_top[index])

    image = image + ":"
    for index in range(5):
        image = image + str(middle_bottom[index])

    image = image + ":"
    for index in range(5):
        image = image + str(bottom[index])

    image = image + ":"
    for index in range(5):
        if (index == position):
            image = image + "9"
        else:
            image = image + "0"

    microbit.display.show(microbit.Image(image))
def main():
    print("David Fisher can write Python!")
    # mb.display.scroll("Dave Fisher")
    # mb.display.show(mb.Image.HAPPY)
    # mb.display.show([mb.Image.ARROW_N, mb.Image.ARROW_E,
    #                  mb.Image.ARROW_S, mb.Image.ARROW_W])
    # print("Starting an infinite loop")
    # mb.display.show(mb.Image.ALL_CLOCKS, loop=True)
    xImage = mb.Image("20002:06060:00900:06060:20002")
    mb.display.show(xImage)

    image1 = mb.Image("77000:77000:00000:00000:00000")
    image2 = mb.Image("07700:07700:00000:00000:00000")
    image3 = mb.Image("00000:07700:07700:00000:00000")
    image4 = mb.Image("00000:77000:77000:00000:00000")
    mb.display.show([image1, image2, image3, image4], loop=True)
Beispiel #5
0
 def gen_image(self):
     img_data = []
     for dy in range(5):
         for dx in range(5):
             value = self.get_pixel(self.pos[0] + dx, self.pos[1] + dy)
             img_data.append('9' if value else '0')
         img_data.append(':')
     return microbit.Image(''.join(img_data[:-1]))
Beispiel #6
0
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 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)

    # 绘制闪烁的玩家
    img.set_pixel(2, 2, (tick & 1) * 4 + 5)
    d.show(img)
Beispiel #8
0
    def __init__(self, blur_factor):
        self.set_blur_factor(blur_factor)

        #  pixel buffers (double buffering FTW!)
        self.pixels = bytearray(5*5)
        self.pixels_temp = bytearray(5*5)

        # image used to display pixels
        self.image = microbit.Image(5, 5)
Beispiel #9
0
def images():
    global timer1, ___numberVar, ___booleanVar, ___stringVar, ___imageVar, ___numberList, ___booleanList, ___stringList, ___imageList
    microbit.display.show(microbit.Image('00000:00000:00000:00000:00000'))
    microbit.display.show(___imageVar.invert())
    microbit.display.show(___imageVar.shift_up(___numberVar))
    microbit.display.show(___imageVar.shift_down(___numberVar))
    microbit.display.show(___imageVar.shift_right(___numberVar))
    microbit.display.show(___imageVar.shift_left(___numberVar))
    microbit.display.show(microbit.Image.HEART)
def microbit_checkmark_image():
    """Shows a checkmark image whenever the function is called, when the play is successful"""
    checkmark = microbit.Image("00000:" \
                               "00009:" \
                               "00090:" \
                               "90900:" \
                               "09000:")
    microbit.display.show(
        checkmark)  #Non-fruitful function that shows a checkmark
Beispiel #11
0
def draw_ball(my_id):
    s = (
        ("5" * int(my_id) + "2" * (len(ids_occupied) - int(my_id)) + "00000")[:5] + ":"
        "00000:"
        "00000:"
        "00000:"
    )
    mb.display.show(mb.Image(s))
    if state['ball_pos']:
        x_pos, y_pos = state['ball_pos']
        print(x_pos, y_pos)
        mb.display.set_pixel(x_pos, y_pos, 9)
Beispiel #12
0
def make_image(led_location):
    """Takes in a location for which pixel should be turned on on the bottom row.
        Returns a micro:bit Image object to display.
        Note that the location of the pixels should be one of:
            01234"""
    upper_portion_of_grid = "00000:00000:00000:00000:"
    possible_bottom_row_values = ["90000", "09000", "00900", "00090", "00009"]
    image_string = upper_portion_of_grid + possible_bottom_row_values[led_location]

    # to understand what this function is doing, uncomment the print statement below
    # print(image_string)

    return microbit.Image(image_string)
Beispiel #13
0
def display_frame(frame):
    """Converts the frame from a list of rows to a string, and then into a micro:bit image, and then draws the frame"""
    row_0_str = str(frame[0][0]) + str(frame[0][1]) + str(frame[0][2]) + str(
        frame[0][3]) + str(frame[0][4])
    row_1_str = str(frame[1][0]) + str(frame[1][1]) + str(frame[1][2]) + str(
        frame[1][3]) + str(frame[1][4])
    row_2_str = str(frame[2][0]) + str(frame[2][1]) + str(frame[2][2]) + str(
        frame[2][3]) + str(frame[2][4])
    row_3_str = str(frame[3][0]) + str(frame[3][1]) + str(frame[3][2]) + str(
        frame[3][3]) + str(frame[3][4])
    row_4_str = str(frame[4][0]) + str(frame[4][1]) + str(frame[4][2]) + str(
        frame[4][3]) + str(frame[4][4])
    image_string = row_0_str + ":" + row_1_str + ":" + row_2_str + ":" + row_3_str + ":" + row_4_str
    image = microbit.Image(image_string)
    microbit.display.show(image)
    def display(self):
        """ Write the contents of the SmartDisplay to the
            MB image buffer and display it.

            The method applies clipping to keep the brightness
            values within the permitted range.
        """
        img_array = bytearray()
        local_int = int
        # img_array = [min(max(local_int(x * 9.0), 0), 9) for row in self.leds for x in row]
        for row in self.leds:
            for x in row:
                x = local_int(x * 9.0)
                x = min(max(x, 0), 9)
                img_array.append(x)
        img = microbit.Image(5, 5, img_array)
        microbit.display.show(img)
Beispiel #15
0
    def run(self, button_a=False, button_b=False):
        if self.crashed():
            return EndGame(self.row), None

        if button_a:
            self.pos = max(0, self.pos - 1)
        if button_b:
            self.pos = min(4, self.pos + 1)

        if self.time >= self.delay:
            self.forward()

        frame_buffer = [[x for x in row] for row in self.field]
        frame_buffer[4][self.pos] = 9
        screen = microbit.Image(':'.join(''.join(map(str, row))
                                         for row in frame_buffer))
        return self, screen
Beispiel #16
0
def generateur():
    listeChoix = ['0', '9']
    a = ""
    b = ""
    c = ""
    d = ""
    e = ""
    
    for i in range (6):
        for j in range (6):
                a += random.choice(listeChoix)
                b += random.choice(listeChoix)
                c += random.choice(listeChoix)
                d += random.choice(listeChoix)
                e += random.choice(listeChoix)
    chaine = a+': '+b+': '+c+': '+d+': '+e
    tempImage = m.Image(chaine)
    return tempImage
Beispiel #17
0
def armar_imagen(posicion_del_enemigo, posicion_del_aliado):
    texto = ""
    for posicion_actual in range(5):
        if posicion_actual == posicion_del_enemigo:
            texto = texto + "1"
        else:
            texto = texto + "0"
    for fila in range(3):
        texto = texto + ":"
        for posicion_actual in range(5):
            if (posicion_actual == posicion_del_enemigo and estado_enemigo == 'disparando') \
                or (posicion_actual == posicion_del_aliado and estado_aliado == 'disparando'):
                texto = texto + '1'
            else:
                texto = texto + '0'
    texto = texto + ':'
    for posicion_actual in range(5):
        if posicion_actual == posicion_del_aliado:
            texto = texto + "1"
        else:
            texto = texto + "0"
    return microbit.Image(texto)
Beispiel #18
0
def sinkingBoat():
    boat1 = mb.Image("05050:"
                     "05050:"
                     "05050:"
                     "99999:"
                     "09990")

    boat2 = mb.Image("00000:"
                     "05050:"
                     "05050:"
                     "05050:"
                     "99999")

    boat3 = mb.Image("00000:"
                     "00000:"
                     "05050:"
                     "05050:"
                     "05050")

    boat4 = mb.Image("00000:"
                     "00000:"
                     "00000:"
                     "05050:"
                     "05050")

    boat5 = mb.Image("00000:"
                     "00000:"
                     "00000:"
                     "00000:"
                     "05050")

    boat6 = mb.Image("00000:"
                     "00000:"
                     "00000:"
                     "00000:"
                     "00000")

    all_boats = [boat1, boat2, boat3, boat4, boat5, boat6]
    mb.display.show(all_boats, delay=200)
def main():
    boat1 = mb.Image("05050:"
                     "05050:"
                     "05050:"
                     "99999:"
                     "09990")

    boat2 = mb.Image("00000:"
                     "05050:"
                     "05050:"
                     "05050:"
                     "99999")

    boat3 = mb.Image("00000:"
                     "00000:"
                     "05050:"
                     "05050:"
                     "05050")

    boat4 = mb.Image("00000:"
                     "00000:"
                     "00000:"
                     "05050:"
                     "05050")

    boat5 = mb.Image("00000:"
                     "00000:"
                     "00000:"
                     "00000:"
                     "05050")

    boat6 = mb.Image("00000:"
                     "00000:"
                     "00000:"
                     "00000:"
                     "00000")

    all_boats = [boat1, boat2, boat3, boat4, boat5, boat6]
    mb.display.show(all_boats, delay=300, loop=True)
Beispiel #20
0
import microbit

brightness_up = [i for i in range(0, 9)]
brightness_down = [i for i in reversed(brightness_up)][:-1]
brightness = brightness_up + brightness_down

# T
IMAGE = (
    "11111:"
    "00100:"
    "00100:"
    "00100:"
    "00100"
)

while True:
    for intensivity in brightness:
        image = IMAGE.replace("1", str(intensivity))
        microbit.display.show(microbit.Image(image), delay=0, wait=False, loop=False, clear=False)
        microbit.sleep(100)
Beispiel #21
0
              "9   9\n"
              "     \n"
              "  9  \n"
              "     \n"
              "9   9\n",

              "9   9\n"
              "     \n"
              "9   9\n"
              "     \n"
              "9   9\n",
             ]

# convert them to images so they can be displayed directly
dice_faces = [microbit.Image(x) for x in dice_faces]

result = 0
result_countdown = int(2000/50) # pretend we started with shaking
last_accelerometer = microbit.accelerometer.get_values()
cheating_enabled = False

# not sure if this is needed, but pretend to seed the random generator
for _ in range(last_accelerometer[0]):
    microbit.random(10)


# main loop
while True:
    microbit.sleep(50)
Beispiel #22
0
player_x = 3


def create_screen(player_x):  #0-4
    line1 = "00000:"
    line2 = "00000:"
    line3 = "00000:"
    line4 = "00000:"
    line5 = ["0", "0", "0", "0", "0"]
    line5[player_x] = "9"
    line5 = line5[0] + line5[1] + line5[2] + line5[3] + line5[4]
    return line1 + line2 + line3 + line4 + line5


current_image = create_screen(player_x)
microbit.display.show(microbit.Image(current_image))

while True:
    if microbit.button_a.was_pressed():
        #moving to left
        if player_x > 0:
            player_x = player_x - 1
        else:
            player_x = 4

    if microbit.button_b.was_pressed():
        #moving to right
        if player_x < 4:
            player_x = player_x + 1
        else:
            player_x = 0
Beispiel #23
0
        x = 0
    else:
        x = x - ord("0")

    banner[i] = x


# calculate delays
number_of_columns = int(len(banner)/5)
exposure = 4.0 # seconds

frame_wait_ms = int(1000*exposure/number_of_columns)


# image that contains window of the banner
image = microbit.Image(5, 5)

# current banner ptr
banner_ptr = 0

banner_ptr_end = len(banner)

# main loop
while True:
    start_frame_ms = microbit.running_time()

    #  display current banner window
    ptr = banner_ptr

    for y in range(5):
        for x in range(5):
Beispiel #24
0
import microbit, random
# background oxo grid at brightness 1
gridImg = microbit.Image("00000\n"
                         "00000\n"
                         "00000\n"
                         "00000\n"
                         "00000\n")

# returns False is a square is blank
def check_squares_filled():
    for x in range(5):
        for y in range(5):
            if microbit.display.get_pixel(x,y) == 0:
                return False
    return True
    
def get_grid():
    return [[microbit.display.get_pixel(x,y) for y in range(5)] for x in range(5)]
    
def grid():
    microbit.display.show(gridImg)
    
# returns -2 to 2 from -1024 to +1024, empirically calculated
def tilt_scale(a):
    a = a + 1024 # range 0 to 2048
    a = min(a, 2048)
    a = max(a, 0)
    # < 512 = 0, 512-853(512+341) = 1, 853-1194 = 2, 1194-1536 = 3,  1536-2048 = 4
    if a<512: return -2
    if a<853: return -1
    if a<1194: return 0
Beispiel #25
0
import microbit
import easygui as easy

#Defines variables
obstacle_x = random.randint(-2, 2)
obstacle_y = 2
car_x = 0
car_y = -2
game_slowness = 10
has_not_crashed = True
has_lifted_b = True
has_lifted_a = True
timer_to_speed_increase = 0
loops_since_fall = 0
score = 0
CRASH = microbit.Image("90909:09090:99099:09090:90909")
GAME_SPEED_INCREASE = 100


def change_frame_pixel(x_cor, y_cor, brightness, frame):
    """Changes a single pixel in the current frame"""
    #Prevents changed values from being out of range of the screen
    if x_cor > 2:
        x_cor = 2
    elif x_cor < -2:
        x_cor = -2
    if y_cor > 2:
        y_cor = 2
    elif y_cor < -2:
        y_cor = -2
    #Changes the pixel depending on the x,y values
Beispiel #26
0
                else:
                    if user in distance:
                        (n, p, l), _ = distance[user]
                        n = min(n, packet)
                        distance[user] = (n, p, l), microbit.running_time()

            if duplicated:
                microbit.sleep(random.randint(100, 5000))
                break

            bars = []

            now = microbit.running_time()

            for bit, ((n, p, l), seen) in sorted(distance.items(),
                                                 key=lambda x: x[0]):
                if seen + 250 < now:
                    del distance[bit]
                    continue

                power = (n + p + l) // 3
                bar = "9" + "9" * min(4, power // 2) + "4" if power % 2 else ""
                bar += "0" * (5 - len(bar))
                bars.append(bar)

            for _ in range(5 - len(bars)):
                bars.append("".join(
                    str(random.choice(range(4))) for _ in range(5)))

            microbit.display.show(microbit.Image(":".join(bars)))
Beispiel #27
0
# Test des flèches
import microbit

fleche_N = microbit.Image("00900:00900:00900:00000:00000")
fleche_N_E = microbit.Image("00009:00090:00900:00000:00000")
fleche_E = microbit.Image("00000:00000:00999:00000:00000")
fleche_S_E = microbit.Image("00000:00000:00900:00090:00009")
fleche_S = microbit.Image("00000:00000:00900:00900:00900")
fleche_S_O = microbit.Image("00000:00000:00900:09000:90000")
fleche_O = microbit.Image("00000:00000:99900:00000:00000")
fleche_N_O = microbit.Image("90000:09000:00900:00000:00000")
microbit.display.show(fleche_N)
microbit.sleep(1000)
microbit.display.show(fleche_N_E)
microbit.sleep(1000)
microbit.display.show(fleche_E)
microbit.sleep(1000)
microbit.display.show(fleche_S_E)
microbit.sleep(1000)
microbit.display.show(fleche_S)
microbit.sleep(1000)
microbit.display.show(fleche_S_O)
microbit.sleep(1000)
microbit.display.show(fleche_O)
microbit.sleep(1000)
microbit.display.show(fleche_N_O)
import microbit

brightness_up = [i for i in range(0, 9)]
brightness_down = [i for i in reversed(brightness_up)][:-1]
brightness = brightness_up + brightness_down

# Q
IMAGE = ("01110:" "10001:" "10101:" "10010:" "01101")

while True:
    for intensivity in brightness:
        image = IMAGE.replace("1", str(intensivity))
        microbit.display.show(microbit.Image(image),
                              delay=0,
                              wait=False,
                              loop=False,
                              clear=False)
        microbit.sleep(100)
Beispiel #29
0
        return height + ":" + display_left + ":" + floor
    else:
        return height + ":" + display_none + ":" + floor


while True:
    if microbit.button_b.was_pressed():
        if height < 5:
            height += 1

    if microbit.button_a.was_pressed():
        if height > 1:
            height -= 1

    res = radio.receive()
    direction = gesture()
    top = bar(height)

    if res is 'rcv':
        if last is False:
            face = disp(direction, top, display_rcv)
            last = True
        elif last is True:
            face = disp(direction, top, display_floor)
            last = False
    else:
        face = disp(direction, top, display_floor)

    microbit.display.show(microbit.Image(face))
    radio.send(face)
Beispiel #30
0
        "33333"

dice4 = "33333:" \
        "39093:" \
        "30003:" \
        "39093:" \
        "33333"

dice5 = "33333:" \
        "39093:" \
        "30903:" \
        "39093:" \
        "33333"

dice6 = "33333:" \
        "39093:" \
        "39093:" \
        "39093:" \
        "33333"

dice_images = [dice1, dice2, dice3, dice4, dice5, dice6]
microbit.display.show(microbit.Image(random.choice(dice_images)))

while True:
    motion = microbit.accelerometer.get_z()
    print(motion)
    if motion > THRESHOLD:
        microbit.display.show(microbit.Image(random.choice(dice_images)))
        time.sleep(0.1)
    time.sleep(0.1)