def createApple(self): badApple = True #try and fnd a location for the apple while(badApple): x = microbit.random(5) y = microbit.random(5) badApple = self.checkCollision(x, y) self.apple = [x, y] microbit.display.set_pixel(x, y, self.APPLEBRIGHTNESS)
def led_dance(delay): dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ] while True: dots[microbit.random(5)][microbit.random(5)] = 8 for i in range(5): for j in range(5): microbit.display.set_pixel(i, j, dots[i][j]) dots[i][j] = max(dots[i][j] - 1, 0) microbit.sleep(delay)
def led_dance(delay): dots = [ [0]*5, [0]*5, [0]*5, [0]*5, [0]*5 ] microbit.display.set_display_mode(1) while True: dots[microbit.random(5)][microbit.random(5)] = 128 for i in range(5): for j in range(5): microbit.display.image.set_pixel(i, j, dots[i][j]) dots[i][j] = int(dots[i][j]/2) microbit.sleep(delay)
def step(self): """Update pixels, using pixels_temp""" # the basic algorithm is to # - shift pixels up # - generate random pixels in the empty (bottom) row # - blur everything # generate random first row in pixels # the row will get get discarded because of shifting up for x in range(5): self.pixels[x] = microbit.random(10) # blur pixels into pixels_temp taking # rows 0-3 from pixels rows 1-4 and # row 4 from random pixels row 0 for y in range(5): for x in range(5): v = self.get_pixel(x, y) v += self.get_pixel(x-1, y) v += self.get_pixel(x+1, y) self.pixels_temp[y*5+x] = round(v/self.blur_factor) # now just swap pixel buffers self.pixels, self.pixels_temp = self.pixels_temp, self.pixels
def noise(length_ms): """Random noise""" end_time = round(microbit.running_time() + length_ms) while microbit.running_time() < end_time: microbit.pin0.write_digital(microbit.random(2)) music.stop()
arena1[i - 1] + arena1[i + 1] + arena1[i + 6] + arena1[i + 7] + arena1[i + 8]) # check if the centre cell is alive or not self = arena1[i] # apply the rules of life if self and not (2 <= num_neighbours <= 3): arena2[i] = 0 # not enough, or too many neighbours: cell dies elif not self and num_neighbours == 3: arena2[i] = 1 # exactly 3 neigbours around an empty cell: cell is born else: arena2[i] = self # stay as-is # swap the buffers (arena1 is now the new one to display) arena1, arena2 = arena2, arena1 while True: # randomise the start for i in range(5 * 5): # loop over pixels i = 8 + (i // 5) * 7 + i % 5 arena1[i] = microbit.random(2) # set the pixel randomly show() microbit.sleep(1) # need to yield to update accelerometer (not ideal...) # loop while button a is not pressed while not microbit.button_a.is_pressed() and microbit.accelerometer.get_z() < -800: conway_step() show() microbit.sleep(150)
"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) # get accelerometer difference between now and last 50ms current_accelerometer = microbit.accelerometer.get_values() dx = last_accelerometer[0] - current_accelerometer[0] dy = last_accelerometer[1] - current_accelerometer[1] dz = last_accelerometer[2] - current_accelerometer[2] last_accelerometer = current_accelerometer
import microbit TOLERANCE = 3000 MESSAGES = ["It is certain", "Dont count on it", "Ask again"] def get_accel_total(): x = microbit.accelerometer.get_x() y = microbit.accelerometer.get_y() z = microbit.accelerometer.get_z() return x + y + z def wait_for_shake(): shaken = False last = get_accel_total() while not shaken: this = get_accel_total() diff = last - this if diff < 0: diff = diff * -1 if diff > TOLERANCE: shaken = True last = this microbit.sleep(50) while True: microbit.display.print("8") wait_for_shake() microbit.display.clear() microbit.sleep(2000) message = microbit.random(len(MESSAGES)) microbit.display.scroll(MESSAGES[message])
arena1[i - 1] + arena1[i + 1] + arena1[i + 6] + arena1[i + 7] + arena1[i + 8]) # check if the centre cell is alive or not self = arena1[i] # apply the rules of life if self and not (2 <= num_neighbours <= 3): arena2[i] = 0 # not enough, or too many neighbours: cell dies elif not self and num_neighbours == 3: arena2[ i] = 1 # exactly 3 neigbours around an empty cell: cell is born else: arena2[i] = self # stay as-is # swap the buffers (arena1 is now the new one to display) arena1, arena2 = arena2, arena1 while True: # randomise the start for i in range(5 * 5): # loop over pixels i = 8 + (i // 5) * 7 + i % 5 arena1[i] = microbit.random(2) # set the pixel randomly show() microbit.sleep(1) # need to yield to update accelerometer (not ideal...) # loop while button a is not pressed while not microbit.button_a.is_pressed( ) and microbit.accelerometer.get_z() < -800: conway_step() show() microbit.sleep(150)
# print("have", position, "want", x) if not handled_this_wall: if player_x < x: player_x += 1 elif player_x > x: player_x -= 1 # print("new", position) # print() if wall_update: # update wall position wall_y += 1 if wall_y == 7: wall_y = -1 hole = m.random(5) handled_this_wall = False if wall_y < 5: # draw new wall use_wall_y = max(wall_y, 0) for wall_x in range(5): if wall_x != hole: s(wall_x, use_wall_y, 6) if wall_reached_player and not handled_this_wall: handled_this_wall = True if (player_x != hole): # collision! game over! break score += 1
MESSAGES = ["It is certain", "Dont count on it", "Ask again"] def get_accel_total(): x = microbit.accelerometer.get_x() y = microbit.accelerometer.get_y() z = microbit.accelerometer.get_z() return x + y + z def wait_for_shake(): shaken = False last = get_accel_total() while not shaken: this = get_accel_total() diff = last - this if diff < 0: diff = diff * -1 if diff > TOLERANCE: shaken = True last = this microbit.sleep(50) while True: microbit.display.print("8") wait_for_shake() microbit.display.clear() microbit.sleep(2000) message = microbit.random(len(MESSAGES)) microbit.display.scroll(MESSAGES[message])
""" Adaptation of Advanced Lawnmower Simulator [http://www.worldofspectrum.org/infoseekid.cgi?id=0000089] Press button A to mow the lawn. Remember that grass keeps growing! """ import microbit # lawn. 0=mowed, 255=overgrown lawn = bytearray([microbit.random(100)+100 for _ in range(5*5)]) # position of the player (x, y) player_x, player_y = 0, 0 player_brightness = 9 player_moved = False # main loop while True: # grow the lawn, clip values at 255 for i, v in enumerate(lawn): # 95 in 100 chance grass grows if microbit.random(100) < 95: lawn[i] = min(v+1, 255) # display lawn and the player for y in range(5): for x in range(5): # displaying player?
# print("have", position, "want", x) if not handled_this_wall: if player_x < x: player_x += 1 elif player_x > x: player_x -= 1 # print("new", position) # print() if wall_update: # update wall position wall_y += 1 if wall_y == 7: wall_y = -1 hole = m.random(5) handled_this_wall = False if wall_y < 5: # draw new wall use_wall_y = max(wall_y, 0) for wall_x in range(5): if wall_x != hole: s(wall_x, use_wall_y, 32) if wall_reached_player and not handled_this_wall: handled_this_wall = True if (player_x != hole): # collision! game over! break score += 1
def generate_line(line, start=min_brightness, end=max_brightness): "start and end define range of dimmest to brightest 'flames'" for i in range(display_width): line[i] = start + microbit.random(end - start)
"9 9\n" " \n" " \n" " \n" "9 9\n", "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) # get accelerometer difference between now and last 50ms current_accelerometer = microbit.accelerometer.get_values() dx = last_accelerometer[0] - current_accelerometer[0] dy = last_accelerometer[1] - current_accelerometer[1] dz = last_accelerometer[2] - current_accelerometer[2] last_accelerometer = current_accelerometer