Accelerometer moves a ball

In this example the accelerometer_data callback is registered for the accelerometer data with microbit.accelerometer.notify

This happens when the micro:bit offers new accelerometer data:

  • the accelerometer_data callback is called with the new data as argument
  • the callback registers the data as the player_direction
  • the while running: loop updates the player_position and redraws the ball

See also the API documentation:

pygame.init()
screen = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()
running = True

player_direction = pygame.Vector2()
player_position = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)


def accelerometer_data(data: AccelerometerData):
    player_direction.x = data.x / 100
    player_direction.y = data.y / 100


with KaspersMicrobit.find_one_microbit() as microbit:
    microbit.accelerometer.notify(accelerometer_data)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        screen.fill("gray")

        new_position = player_position + player_direction
        player_position.x = pygame.math.clamp(new_position.x, 8, screen.get_width() - 8)
        player_position.y = pygame.math.clamp(new_position.y, 8, screen.get_height() - 8)
        pygame.draw.circle(screen, "blue", player_position, 8)

        pygame.display.flip()

        clock.tick(60)

    pygame.quit()