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 ball.direction
  • tk.main_loop() calls redraw which moves the ball on the canvas in the direction set earlier by the callback

See also the API documentation:

class XY:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Ball:

    def __init__(self, canvas: Canvas, color):
        self.canvas = canvas
        self.position = XY((canvas.winfo_width() / 2) - 5, (canvas.winfo_height() / 2) - 5)
        self.direction = XY(0, 0) = canvas.create_oval(self.position.x, self.position.y, self.position.x + 10, self.position.y + 10, fill=color)

    def draw(self):
        new_position = XY(self.position.x+self.direction.x, self.position.y+self.direction.y)
        self.position.x = max(0, min(new_position.x, canvas.winfo_width()-10))
        self.position.y = max(0, min(new_position.y, canvas.winfo_height()-10))
        self.canvas.moveto(, new_position.x, self.position.y)

tk = Tk()
tk.title("Use accelerometer to move ball")
tk.resizable(False, False)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=1280, height=720)
ball = Ball(canvas, 'blue')

def redraw():
    tk.after(10, redraw)

tk.after(10, redraw)

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

with KaspersMicrobit.find_one_microbit() as microbit: