microbit使用python

makecode上的python

使LED隨著板子移動而變動


old_x = 0

old_y = 0


def on_forever():

basic.pause(20)

x = input.acceleration(Dimension.X)


y = input.acceleration(Dimension.Y)

global old_x

global old_y


led_x = Math.map(x, -1000, 800, 0, 4)

led_y = Math.map(y, -1000, 800, 0, 4)

if led_x == 5:

led_x = 4

if led_y == 5:

led_y = 4

if old_x != led_x or old_y != led_y:


led.unplot(old_x, old_y)

led.plot(led_x, led_y)

old_x = led_x

old_y = led_y


basic.forever(on_forever)


使用Thonny的micropython

from microbit import *

import microbit


old_x = 0

old_y = 0


while True:

sleep(20)

x = accelerometer.get_x()

y = accelerometer.get_y()

led_x = (x - (-1024) ) /410

led_x = int(led_x)

led_y = (y - (-1000) ) /410

led_y = int(led_y)


if led_x == 5:

led_x = 4

if led_y == 5:

led_y = 4

if old_x != led_x or old_y != led_y:

microbit.display.set_pixel(old_x, old_y, 0)

microbit.display.set_pixel(led_x, led_y, 9)

old_x = led_x

old_y = led_y