def gameStart(self): display.scroll("3... 2... 1... Launch...", delay=70) while True: astroidField.createAstroid() astroidField.drawAstroids() spaceShip.draw() if spaceShip.collide(): self.gameOver() break sleep(self.frameRate) astroidField.hideAstroids() spaceShip.hide() if button_a.was_pressed(): spaceShip.moveLeft() elif button_b.was_pressed(): spaceShip.moveRight() astroidField.moveAstroids() astroidField.clearPassedAstroids() self.increaseDifficulty()
def setup(self): while True: try: self.board.add_ship() x = 0 y = 0 doublepress = False while not doublepress: a_was_pressed = button_a.was_pressed() b_was_pressed = button_b.was_pressed() a_pressed = button_a.is_pressed() b_pressed = button_b.is_pressed() if a_pressed and b_pressed: self.board.rotate_ship() if a_was_pressed and b_was_pressed: doublepress = True elif a_pressed: x += 1 try: self.board.move_ship(x=x, y=y) except InvalidPositionError: x = 0 self.board.move_ship(x=x, y=y) elif b_pressed: y += 1 try: self.board.move_ship(x=x, y=y) except InvalidPositionError: y = 0 self.board.move_ship(x=x, y=y) except StopIteration: break
def wait_start(): global uids, lid while True: if button_a.was_pressed(): di.show("F") uids = find_used_ids() gnid(uids) break
def get_sensor_data(): x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() a = button_a.was_pressed() b = button_b.was_pressed() print(x, y, z, a, b) return x, y
def process_local_generated_events(): if button_b.was_pressed(): send_and_expect_response(RESET_MESSAGE) elif button_a.was_pressed(): send_and_expect_response(ALERT_MESSAGE) elif status['status'] != ALERT_MESSAGE: if running_time() > status['last_reset'] + ALERT_DELAY: send_and_expect_response(ALERT_MESSAGE) if running_time() > status['last_heartbeat'] + HEARTBEAT_PERIOD: send_and_expect_response(HEARTBEAT_MESSAGE)
def anim(): while 1: with open('anim0.txt', 'r') as _f: for _ in range(60): content = _f.readline() display.show(Image(content.strip())) sleep(100) if button_a.was_pressed() or button_b.was_pressed(): return
def alive(): # Event loop. while not DEAD: update_display() incoming = radio.receive() if incoming is not None and len(incoming) == 2: # bomb dropped! parse_msg(incoming) sleep(100) if button_a.was_pressed() or random.randint(0, 100) == 0: send_bomb()
def _detect_event(self): now = self._ticks_ms() for timer in self.timer_names: if (timer in self.timers) and (self.timers[timer] is not None): if self.timers[timer] < now: self.timers[timer] = None return timer if button_a.was_pressed(): return "button_a" if button_b.was_pressed(): return "button_b" if accelerometer.was_gesture("shake"): return "shake" return None
def process_input(): nonlocal gpos nonlocal gdir nonlocal gmove if button_a.was_pressed(): gmove = DIRS[gdir] gpos[0] += DIRS[gdir][0] gpos[1] += DIRS[gdir][1] # Re-direct if previous gdir is no longer possible possible_dirs = get_passages(gpos[0], gpos[1]) while gdir not in possible_dirs: gdir = (gdir + 1) % 4 if button_b.was_pressed(): possible_dirs = get_passages(gpos[0], gpos[1]) gdir = (gdir + 1) % 4 while gdir not in possible_dirs: gdir = (gdir + 1) % 4
def position(sprite, board, send=False): blink(sprite, board) while not (button_a.is_pressed() and button_b.is_pressed()): sleep(100) # if button is pressed but not released yet, keep waiting if button_a.is_pressed() or button_b.is_pressed(): continue a = button_a.was_pressed() b = button_b.was_pressed() if a or b: sprite = move(sprite, b, a) blink(sprite, board) if send: radio.send("{}{}".format(*sprite[0])) return sprite
def setting_mode(threshold): while (button_a.is_pressed() or button_b.is_pressed()): display.show(Image.CLOCK12) sleep(300) display.show(Image.CLOCK3) sleep(300) display.show(Image.CLOCK6) sleep(300) display.show(Image.CLOCK9) sleep(300) display.clear() display.show(threshold) sleep(300) display.show(Image.ARROW_E) sleep(400) display.show(Image.ARROW_W) sleep(400) display.clear() last_click = running_time() while (running_time() < last_click + 5000): if button_b.was_pressed(): last_click = running_time() threshold = threshold + 1 display.show(Image.ARROW_N) sleep(300) display.show(threshold) sleep(300) display.clear() if button_a.was_pressed(): last_click = running_time() threshold = threshold - 1 display.show(Image.ARROW_S) sleep(300) display.show(threshold) sleep(300) display.clear() tfile = open("tstore", "w") tfile.write(str(threshold)) tfile.close() display.show(Image.YES) sleep(700) display.clear() return (threshold)
def main(server=False): if server: display.show(Image.FABULOUS) start_server() else: start_client() if server: draw_screen() # Event loop. while True: should_redraw = False if button_a.was_pressed(): pull() should_redraw = True elif radio.receive() == "A": push() should_redraw = True if should_redraw: draw_screen()
def main(): quiet = False print('hello world') display.show(Image.HEART) sleep(random.randint(0, 10) * 1000) if button_a.was_pressed(): do_something() quiet = not quiet if button_b.was_pressed(): if not quiet: sing() do_something() wave = accelerometer.current_gesture() if wave != '': do_something() gc.collect()
def main(): print('hello world') display.show(Image.HEART) sleep(random.randint(0, 10) * 1000) if button_a.was_pressed(): global quiet quiet = not quiet do_something() if button_b.was_pressed(): sing() do_something() #music.play(music.BIRTHDAY) wave = accelerometer.current_gesture() if wave != '': do_something() gc.collect()
def wait_start(): global uids, lid, joystick, car_enabled go = 0 while True: if button_b.was_pressed(): joystick = 1 di.show("J") go = 1 if pin_logo.is_touched(): di.show("E") go = 1 if button_a.was_pressed(): go = 1 car_enabled = 1 move_motor_lr(0, 0) di.show("C") if go == 1: sleep(500) di.clear() uids = find_used_ids() gnid(uids) break
def run(self): #"""Run the game.""" i = 0 wave = 200 wiggle = 80 while wave > 20: self.show() if button_a.was_pressed(): self.move_cannon() self.update_cannon() if button_b.was_pressed(): self.fire() self.update_sky() self.update_bullet() i += 1 if i % wiggle == 0: self.invaders_move() if i == wave: if self.drop_invaders() == -1: break i = 0 wave -= 1 if wiggle > 20: wiggle -= 5
from microbit import display, button_a, button_b, Image, sleep count = 0 minute = 0 counting = False image_none = Image("00000:00000:00000:00000:00000") image_all = Image("55555:55555:55555:55555:55555") image_off = Image("00005:00050:00500:05000:50000") display.show(image_off) # loop while True: sleep(1000) a_pressed = button_a.was_pressed() b_pressed = button_b.was_pressed() if not counting and a_pressed: # start timer counting = True count = 0 minute = 0 display.show(image_all) display.set_pixel(0, 0, 8) if counting: if b_pressed: # stop timer counting = False display.show(image_off) elif minute < 25:
# A micro:bit Firefly. # By Nicholas H.Tollervey. Released to the public domain. import radio import random from microbit import display, Image, button_a, sleep # Create the "flash" animation frames. Can you work out how it's done? flash = [Image().invert()*(i/9) for i in range(9, -1, -1)] # The radio won't work unless it's switched on. radio.on() # Event loop. while True: # Button A sends a "flash" message. if button_a.was_pressed(): radio.send('flash') # a-ha # Read any incoming messages. incoming = radio.receive() if incoming == 'flash': # If there's an incoming "flash" message display # the firefly flash animation after a random short # pause. sleep(random.randint(50, 350)) display.show(flash, delay=100, wait=False) # Randomly re-broadcast the flash message after a # slight delay. if random.randint(0, 9) == 0: sleep(500) radio.send('flash') # a-ha
# By Nicholas H.Tollervey. Released to the public domain. import radio import random from microbit import display, Image, button_a, button_b, sleep # Create the "flash" animation frames. Can you work out how it's done? flash = [Image().invert() * (i / 9) for i in range(9, -1, -1)] # The radio won't work unless it's switched on. radio.on() display.show(flash, delay=100, wait=False) # Event loop. while True: # Button A sends a "flash" message. if button_a.was_pressed() or button_b.was_pressed(): radio.send('flash') # a-ha # Read any incoming messages. incoming = radio.receive() if incoming == 'flash': # If there's an incoming "flash" message display # the firefly flash animation after a random short # pause. sleep(random.randint(50, 350)) display.show(flash, delay=100, wait=False) # Randomly re-broadcast the flash message after a # slight delay. if random.randint(0, 10) < 4: sleep(random.randint(0, 10000)) radio.send('flash')
def wait_for_button(): while not (button_a.was_pressed() or button_b.was_pressed()): sleep(100)
def main(): def generate_difficulty_img(diff): img = Image(5, 5) for i in range(diff + 1): for j in range(i + 1): img.set_pixel(i, 4 - j, 8) return img def render(): nonlocal gpos nonlocal gdir nonlocal gmove cell_x = gpos[0] - 1 cell_y = gpos[1] - 1 s = render_cells((cell_x, cell_y), gmove) s.set_pixel(2, 2, 8) if time % 3 != 0 and gmove == (0, 0): x = DIRS[gdir][0] * 2 y = DIRS[gdir][1] * 2 s.set_pixel(2 + x, 2 + y, 4) gmove = (0, 0) return s def process_input(): nonlocal gpos nonlocal gdir nonlocal gmove if button_a.was_pressed(): gmove = DIRS[gdir] gpos[0] += DIRS[gdir][0] gpos[1] += DIRS[gdir][1] # Re-direct if previous gdir is no longer possible possible_dirs = get_passages(gpos[0], gpos[1]) while gdir not in possible_dirs: gdir = (gdir + 1) % 4 if button_b.was_pressed(): possible_dirs = get_passages(gpos[0], gpos[1]) gdir = (gdir + 1) % 4 while gdir not in possible_dirs: gdir = (gdir + 1) % 4 def is_win(): nonlocal gpos global g_w global g_h if gpos[0] == g_w - 1 and gpos[1] == g_h - 1: return True else: return False current_diff = 0 while True: # Difficulty selector # while True: display.show(generate_difficulty_img(current_diff)) if button_b.was_pressed(): current_diff = (current_diff + 1) % 5 if button_a.was_pressed(): break # The actual game # time = 0 gdir = DIR_RIGHT gpos = [0, 0] gmove = (0, 0) generate_cells(2 + current_diff) while not is_win(): process_input() display.show(render()) time += 1 sleep(50) display.show(Image.HAPPY) sleep(2000)
sleep(200) theString.append(".") return ".".join(theString) stringgg = selectString(length=selectLength()) radio.send(stringgg) display.scroll("Sent")""" active = False while True: incoming = radio.receive() if incoming == "visitor": music.play(music.BA_DING) active = True a, b = (button_a.was_pressed(), button_b.was_pressed() ) #This will reset was_pressed to false while active: if button_a.was_pressed() or button_b.was_pressed(): active = False display.show(Image.YES) music.play(music.POWER_DOWN) sleep(1000) display.clear() else: for i in [ Image("00000\n" "00000\n" "00900\n" "00000\n" "00000"), Image.SQUARE_SMALL, Image.SQUARE
tools = (Image.SKULL, Image.PITCHFORK, Image.PACMAN) random.seed(463473567345343) my_id = 'thommy' tool = 0 peers = set() receives = 0 toggle = 0 radio.on() radio.config(channel=9) while True: #if accelerometer.was_gesture('shake'): if button_a.was_pressed(): tool = random.randrange(3) #display.clear() #sleep(1000) #display.show(tool) sleep_time = random.randint(200, 500) print('sleep time: %s' % sleep_time) if receives >= 10: print('sending: %s %s' % (my_id, tool)) radio.send('%s %s' % (my_id, tool)) receives = 0 if toggle: display.show('%s' % len(peers))
'z': 6, '-z': 1, }.get(gesture,0) display.scroll("magician") radio.on() haptic_map = { # .=1, -=3 1: '.', 2: '..', 3: '-', 4: '-.', 5: '-..', 6: '--' } while True: if button_a.was_pressed(): # Haptic for symbol in haptic_map[get_number()]: pin0.write_digital(1) if symbol == '.': sleep(100) else: sleep(300) pin0.write_digital(0) sleep(200) elif button_b.was_pressed(): # Screen display.show(str(get_number())) sleep(1500) display.clear() sleep(600)
from microbit import sleep, button_a import radio import PCA9685 import servo def playnote(sc, smap, note, numticks, ticklength): sc.position(smap.index(note), 30) # move servo enough to hit glass sleep(100) # give it time to move that far sc.position(smap.index(note), 0) # move it back sleep(100) sc.release(smap.index(note)) sleep((ticklength * numticks) - 200) # rest for the length of the note radio.on() sc = servo.Servos() # http://www.musicnotes.com/SheetMusic/mtd.asp?ppn=MN0127456 swt = ('a4:2', 'b4:2', 'g4', 'a4:2', 'c5:2', 'd5:2', 'b4', 'c5:2', 'b4:2') smap = ('g4', 'a4', 'b4', 'c5', 'd5') while True: incoming = radio.receive() if incoming == 'dingdong' or button_a.was_pressed(): for _ in range(5): for x in swt: y = x.split(':') playnote(sc, smap, y[0], len(y) > 1 and int(y[1]) or 1, 200)
from microbit import display, button_a, button_b, Image, accelerometer import radio from speech import say radio.on() #This code was used for a "messaging" system """while True: incoming = radio.receive() #display.show(Image.YES) display.scroll(incoming) say(incoming)""" debounce = False while True: if accelerometer.was_gesture( "shake") or button_a.was_pressed() or button_b.was_pressed(): if not debounce: radio.send("visitor") display.show(Image.YES) debounce = True sleep(10)
while True: if STATE == "Receive_Message": if isPrinted == 0: print("\nSTATE: Receive_Message") display.clear() msg_receive = radio.receive() if msg_receive is not None: print("Received a Data Frame:", msg_receive) isPrinted = 0 PREV_STATE = "Receive_Message" STATE = "Process_Received_Data_Frame" elif (button_a.was_pressed()) and (power == "ON"): DataFrame_NO = 0 isPrinted = 0 STATE = "Send_Message" else: isPrinted = 1 elif STATE == "Process_Received_Data_Frame": print("\nSTATE: Process_Received_Data_Frame") msg_decoded = Decoded_Data(msg_receive) if msg_decoded[0:2] == "GW": if msg_decoded[3:] == "#":
import sys sys.exit(0) from microbit import button_a, sleep, uart, pin1, pin2 from microbit import display, Image class BitOut(retune.Retuner): def output(self, mess): uart.write(bytes(mess)) midistream = BitOut() display.show(Image.HAPPY) while not button_a.was_pressed(): sleep(30) try: uart.init(31250, tx=pin2, rx=pin1) display.show(Image.NO) while not button_a.was_pressed(): mess = uart.read(4) if mess: display.show(Image.MUSIC_CROTCHET) midistream.write(mess) except Exception as e: midibit_fail = e raise finally: uart.init(115200)
for n, v in enumerate(a_bin[2:]): display.set_pixel( n, 3, v * 8) #Visualize angle in binary notation (led on = 1) #display.show(accelerometer.get_y()/11.4, delay=450) sleep(200) velocity = 0 vdelta = 1 #used for trimming velocity change while aiming while button_a.is_pressed( ): #release button a when velocity is desired value velocity += 1 * vdelta display.show(velocity) sleep(370) if velocity == 5 or velocity == 1: #time to change count direction vdelta = vdelta * (-1) velocity = v0 + velocity if button_a.was_pressed(): #trigger pressed! y = (x * (math.tan(angle))) - (g * (x**2)) / ( (2 * (velocity**2)) * ((math.cos(angle))**2) ) #calculate hight of virtual projectile when reaching negotiated distance display.show("F F ") #Firing, stand by. sleep(500) if y < -hitarea: # projectile would be below zero (the x-axis plane) at correct distance, and outside hit area, too short display.scroll("short", delay=95, wait=False) elif y > hitarea: # projectile above hit area at correct distance, too long display.scroll("long", delay=95, wait=False) else: #Confirmed hit! radio.send('H') #Tell opponent it got hit. display.show("*") display.scroll("HIT") display.show("*") break