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 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 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 play(location): display.show(Image.ALL_CLOCKS, delay=50, loop=True, wait=False) number = random.randrange(1, 10000) sleep(random.randrange(10, 500)) radio.send(str(number)) sleep(3000) numbers = [] while True: message = radio.receive() if not message: break numbers.append(int(message)) if number < min(numbers): location = "UNKNOWN" radio.off() display.show(Image.ARROW_E) seconds = 0 start_time = running_time() button_b.was_pressed() while seconds < 8 * 60: if accelerometer.was_gesture("shake"): minutes = seconds // 60 display.scroll("{0}:{1:02d}".format(minutes, seconds - minutes * 60)) if button_b.was_pressed(): display.scroll(location.upper()) sleep(10) seconds = (running_time() - start_time) // 1000 animation = [Image.SKULL, Image()] display.show(animation, delay=500, loop=True, wait=False) while True: sleep(10000) display.off()
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(): 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 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 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
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)
'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)
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: # process timer
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 elif button_b.was_pressed(): # Waive the white flag and give up. alive = False radio.send('WO') # Loosing on walk over break if h == 'H': #This mortar got hit, we lost the game alive = False display.show(Image.ASLEEP, wait=True, clear=False) break elif h == 'WO': break while True: #Display result, Winner will still be alive when match is over. if alive: display.scroll("WINNER") display.show(Image.HAPPY) else:
def wait_for_button(): while not (button_a.was_pressed() or button_b.was_pressed()): sleep(100)
# application imports from microbit import sleep from microbit import * import radio from microbit import display, Image, button_a, button_b, sleep # create an instance of KMotor r = KMotor() radio.on() while True: if button_a.was_pressed(): radio.send('Forward') #Forward message elif button_a.is_pressed() and button_b.is_pressed(): radio.send("Stop") elif button_b.was_pressed(): radio.send('Back') #Back # Read any incoming messages. incoming = radio.receive() if incoming == 'Forward': # turn motor 1 on in forward for 1 second and turn it off #r.motor_on(r.MOTOR_2, r.FORWARD) display.scroll('F') #sleep(3000) r.motor_on(r.MOTOR_1, r.FORWARD, 100) #sleep(5000) #r.motor_off(r.MOTOR_1) #sleep(2000) #r.motor_on(r.MOTOR_1, r.REVERSE,100)
if msg == 'RESET': org = create_genesis_org() print_org(org) write_string(org_to_string(org), FILE) collect() if ticks_ms() < last_reproduction + maturity * 1000: state = 'WAIT' elif state == 'WAIT': state = 'RECV' elif button_a.was_pressed(): display.scroll('G:{} C:{}'.format(org['gender'], org['color'])) elif button_b.was_pressed() and state == 'RECV': state = 'SEND' msg = 'SREQ|{}'.format(org_to_string(org)) radio.send(msg) if state == 'RECV': if msg is not None and msg[:4] == 'SREQ' and randint(1,3) == 1: new_org = org_from_repr(msg[5:]) if new_org['gender'] != org['gender']: print('Parent 1:') print_org(org) print('Parent 2:') print_org(new_org) radio.send('SRSP|{}'.format(org_to_string(org))) org = create_org_from_parents(new_org, org) print('Offspring:')
return x, y radio.on() channel = new_channel() x, y = xy(channel) radio.config(channel=channel) while True: display.set_pixel(x, y, 4) radio.send('hello, microbit') display.set_pixel(x, y, 0) if accelerometer.is_gesture('shake'): channel = new_channel() radio.config(channel=channel) x, y = xy(channel) if button_a.was_pressed(): channel += 1 radio.config(channel=channel) if channel > 99: channel = 0 x, y = xy(channel) if button_b.was_pressed(): channel -= 1 radio.config(channel=channel) if channel == 0: channel = 99 x, y = xy(channel) radio.off()
lightOn(1) elif reading >= -450 and reading < -150: slave = "12" lightOn((1, 2)) elif reading >= -150 and reading < 150: slave = "2" lightOn(2) elif reading >= 150 and reading < 450: slave = "23" lightOn((2, 3)) elif reading >= 450: slave = "3" lightOn(3) a = button_a.was_pressed() b = button_b.was_pressed() if a == True and b == True: display.show(flash, delay=100, wait=False) sleep(random.randint(50, 350)) microbit.display.clear() contValue = False elif a == True: display.show(flash, delay=100, wait=False) sleep(random.randint(50, 350)) radio.send("allIn") while True: direction = compass.heading() soundPlay = False if button_a.was_pressed():
# 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')
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)
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