def deal_with_command(s): if s[0] != ID: return 0 if s[1] != ord(b'S') and s[1] != ord(b'R') and s[1] != ord(b'Q'): return 0 if s[1] == ord(b'S'): for i in range(4, 8, 1): speed[i - 4] = s[i] set_car_speed() st = struct.pack('4s4B6i', s[0:4], speed[0], speed[1], speed[2], speed[3], compass.get_x(), compass.get_y(), compass.get_z(), accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z()) uart.write(st) sleep(20) if s[1] == ord(b'R'): display.show(Image.SAD) uart.write('+++') for i in range(4, 8, 1): speed[i - 4] = 0 set_car_speed() sleep(1000) reset() return 1
def place1x1Ship(playerSeaX, playerSeaY): posX = 0 posY = 0 display.scroll("1X1 ship") sleep(10000) posX = button_a.get_presses() posY = button_b.get_presses() if posX > 4: display.scroll("Invalid position, please try again") microbit.reset() if posY > 4: display.scroll("Invalid position, please try again") microbit.reset() playerSeaX.append(posX) playerSeaY.append(posY)
while True: sleep(100) if -256 < accelerometer.get_y() < 450: bird = max(0, bird - 1) elif 568 < accelerometer.get_y() < 1024: bird = min(4, bird + 1) display.clear() display.set_pixel(0, bird, 9) pos_terrain = pos // terrain_multiplier lost_status = False for column, (top, bottom) in enumerate(terrain[pos_terrain:pos_terrain + 5]): for y in range(top): display.set_pixel(column, y, 4) if column == 0 and bird == y: lost_status = True for y in range(bottom): display.set_pixel(column, 4 - y, 4) if column == 0 and bird == (4 - y): lost_status = True if lost_status: display.show(Image.SAD) music.play(music.FUNERAL) reset() pos += 1 if pos_terrain > len(terrain): pos = 0
plot(ballX, ballY, brightness[2]) # Turn-on LED (ball) at new position # Other cases where ball is either rising or falling else: # Debug code: print("ballX =" + str(ballX)) # View print() output using REPL plot(oldBallX, oldBallY, brightness[0]) # Turn-off LED at old position plot(ballX, ballY, brightness[2]) # Turn-on LED at new position # Make ball speed-up as it falls and slow-down as it rises. delay = 128 - 8 * ballX sleep(delay) if button_a.is_pressed(): display.scroll(hitCount) # Show current score # End of "while gameOn:" loop # =========================== display.scroll("Game Over,") if hitCount >= maxScore: display.scroll(" You won.") display.show(hapFace) else: display.scroll(" Micro:bit won.") display.show(sadFace) sleep(3000) reset() # Start a new game. # EOF
while True: sleep(100) if -256 < accelerometer.get_y() < 450: bird = max(0, bird - 1) elif 568 < accelerometer.get_y() < 1024: bird = min(4, bird + 1) display.clear() display.set_pixel(0, bird, 9) pos_terrain = pos // terrain_multiplier lost_status = False for column, (top, bottom) in enumerate( terrain[pos_terrain:pos_terrain + 5]): for y in range(top): display.set_pixel(column, y, 4) if column == 0 and bird == y: lost_status = True for y in range(bottom): display.set_pixel(column, 4 - y, 4) if column == 0 and bird == (4 - y): lost_status = True if lost_status: display.show(Image.SAD) music.play(music.FUNERAL) reset() pos += 1 if pos_terrain > len(terrain): pos = 0
field = m.compass.get_field_strength() t = time.ticks_ms() # pylint: disable=no-member if not crossing and abs(field - baseline) > THRESHOLD: crossing = True delta = t - last_change if delta > TIME_LIMIT: num = num + 1 update_temperature() send_single_event(t, num) last_change = t update_display() elif crossing and abs(field - baseline) <= THRESHOLD: crossing = False update_display() if t - last_sync >= SYNC_TIME: sync_data() if m.button_a.was_pressed(): baseline = m.compass.get_field_strength() # Update baseline show_num = not show_num if not show_num: m.display.clear() else: send_full_log() update_display() elif m.button_b.was_pressed(): remove_files() m.reset()
plot(ballX, ballY, brightness[2]) # Turn-on LED (ball) at new position # Other cases where ball is either rising or falling else: # Debug code: print("ballX =" + str(ballX)) # View print() output using REPL plot(oldBallX, oldBallY, brightness[0]) # Turn-off LED at old position plot(ballX, ballY, brightness[2]) # Turn-on LED at new position # Make ball speed-up as it falls and slow-down as it rises. delay = 128 - 8 * ballX sleep(delay) if button_a.is_pressed(): display.scroll(hitCount) # Show current score # End of "while gameOn:" loop # =========================== display.scroll("Game Over,") if hitCount >= maxScore: display.scroll(" You won.") display.show(hapFace) else: display.scroll(" Micro:bit won.") display.show(sadFace) sleep(3000) reset() # Start a new game. # EOF
def reset(self): print("reset") microbit.reset()