from microbit import display, accelerometer, sleep, Image from music import play, POWER_UP, NYAN def get_xy(): yaccel = accelerometer.get_y() * accelerometer_sensitivity xaccel = accelerometer.get_x() * accelerometer_sensitivity return yaccel, xaccel def count_lit_pixels(): pixel_count = 0 for xx in range (5): for yy in range (5): if display.get_pixel(xx, yy) != 0: pixel_count += 1 return pixel_count pause = 100 level = 0 accelerometer_sensitivity=1/300 #set initial position x, y = 2, 2 yaccel, xaccel = get_xy() y = max(0, min(4, int(y + yaccel))) x = max(0, min(4, int(x + xaccel))) while pause > 0: yaccel, xaccel = get_xy() newy = max(0, min(4, int(y + yaccel))) newx = max(0, min(4, int(x + xaccel))) if newy != y or newx != x: display.set_pixel(x, y, 1) x, y = newx, newy display.set_pixel(x, y, 9) else: display.set_pixel(newx, newy, 9) pixels = count_lit_pixels() if pixels == 25: play(POWER_UP, wait=False) level += 1 pause -= 5 sleep(200) display.show(str(level)) sleep(1000) display.clear() sleep(pause) play(NYAN, wait=False) display.show('WIN!') sleep(200) display.show(Image.HEART)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question