def play_game(): global col1, col2, fire, aliens, kills col1 = [1, 1, 0, 0, 0] col2 = [1, 1, 0, 0, 0] fire = 0 aliens = 4 kills = 0 win_lose = 0 d = delay n = 1 while win_lose == 0: show_pixels(n) [col1, win_lose] = move_objects(col1, n) [col2, win_lose] = move_objects(col2, n) check_buttons() add_aliens(n) time.sleep(d) n = n + 1 if n % 50 == 0: d = d - delay_incr # print ([col1, col2, aliens, fire, kills]) if win_lose == 1: cp.play_file(win_sound) else: cp.play_file(lose_sound)
def move_missile(col): global aliens, kills, fire i = 0 while i < 5: if col[i] == 11: if i == 0: col[i] = 0 fire = 0 elif col[i - 1] == 2: col[i] = 0 fire = 0 elif col[i-1] == 1: col[i - 1] = 2 col[i] = 0 cp.play_file(hit_sound) kills = kills + 1 aliens = aliens - 1 fire = 0 elif i > 0: col[i - 1] = col[i] col[i] = 0 i = i + 1 return col
def play_game(): a = altitude v = velocity n = 0 cp.pixels.fill(0) show_location(a, v) print ('Altitude', 'Velocity') while a > 0 and a < 12000: t = 0.0 if cp.button_a: t = high_thrust if cp.button_b: t = low_thrust v = gravity + t + v a = a + v show_location(a, v) if n % 10 == 0: print (a, v) time.sleep(delay) if v < final_velocity or a > 12000: cp.play_file(lose_sound) else: cp.play_file (win_sound) time.sleep(2)
def say_interval(number_as_words): words = number_as_words.split() + ["seconds"] for word in words: if word == ",": time.sleep(0.15) else: cp.play_file(WAV_DIR + "/" + word + ".wav") time.sleep(0.050)
def annoncer_vainqueur(temps_equipes): global couleurs_equipes # bipper le bon code et allumer les led correspondantes pixels_equipes = [(5, 6, 7), (7, 8, 9), (0, 1, 2), (2, 3, 4)] print(temps_equipes) sans_reponse = [] vainqueur = None deuxieme = None m1 = None m2 = None for i, t in enumerate(temps_equipes): if t == 0: sans_reponse.append(i + 1) temps_equipes[i] = t = time.monotonic_ns() // 1000000 + 1000 if m1 == None or t <= m1: m1, m2 = t, m1 elif m2 == None or t < m2: m2 = t if len(sans_reponse) == 4: #Pas de vainqueur, pas de réponse cp.pixels.fill((20, 10, 0)) animation[0] = (40, 0, 0) animation[3] = 0 if cp.switch: cp.play_file("/sons/smb_bowserfalls.wav") else: time.sleep(1) cp.pixels.fill((40, 0, 0)) return vainqueur = temps_equipes.index(m1) + 1 if len(sans_reponse) <= 2: deuxieme = temps_equipes.index(m2) + 1 print("L'equipe", vainqueur, "gagne") print("Sans reponse :", sans_reponse) # le switch en dessous des bouttons rends muet et allume la led rouge D13 if cp.switch: for _ in range(1, vainqueur + 1): cp.play_file("/sons/smb_coin.wav") # allumer les bons pixels de la bonne couleur for p in pixels_equipes[vainqueur - 1]: cp.pixels[p] = couleurs_equipes[vainqueur - 1] animation[0] = couleurs_equipes[vainqueur - 1] animation[3] = 0 # si il y a un deuxieme, allumer juste un pixel plus faiblement. if deuxieme != None: print("L'equipe", deuxieme, "est 2e : +", m2 - m1, "millisecondes") dim_color = map(lambda x: x // 6, couleurs_equipes[deuxieme - 1]) cp.pixels[pixels_equipes[deuxieme - 1][1]] = tuple(dim_color)
def read_song(notes): win_lose = True n = 0 while n < len(notes) and win_lose: new_note = read_touch() play_note(new_note) if notes[n] != new_note: win_lose = False n = n + 1 if win_lose == False: cp.play_file(lose_sound) return win_lose
def check_buttons(): global col1, col2, fire if fire == 0: if cp.button_a: if col1[4] == 0: col1[4] = 11 cp.play_file(fire_sound) fire = 1 if cp.button_b: if col2[4] == 0: col2[4] = 11 cp.play_file(fire_sound) fire = 1
def play_game(difficulty): score = 0 if difficulty == 0: delay = 2.0 / 6.0 score = play_round(0, delay) score = score + play_round(1, delay) elif difficulty == 1: delay = 2.0 / 6.0 score = play_round(random.randint(0, 1), delay) score = score + play_round(random.randint(0, 1), delay) elif difficulty == 2: score = play_round(0, random.random() * 0.5) score = score + play_round(1, random.random() * 0.5) if score == 2: cp.play_file(win_sound)
def read_buttons(i): done = 0 n = 0 while done == 0: if cp.button_a: cp.play_file(beep_sound) n = n + 1 if cp.button_b: if n > 0: done = 1 if n > 2: n = 1 if n > i: n = i set_pixels(i - n) return n
def play_round(direction, delay): cp.play_file(launch_sound) fire = 0 i = 0 while i < 6: show_pixel(i, direction, clay_color) j = 0 while j < 5: if fire == 0: if cp.button_a and direction == 0: fire = i if cp.button_b and direction == 1: fire = i time.sleep(delay / 5) j = j + 1 if i == 3: if fire == 3: show_pixel(i, direction, hit_color) cp.play_file(hit_sound) cp.pixels.fill(0) return 1 i = i + 1 cp.pixels.fill(0) cp.play_file(miss_sound) return 0
def move_aliens(col): global aliens, kills, fire win_lose = 0 i = 4 while i >= 0: if col[i] == 1: if i == 4: col[i] = 0 win_lose = -1 elif col[i+1] == 11: col[i + 1] = 2 col[i] = 0 cp.play_file(hit_sound) kills = kills + 1 aliens = aliens - 1 fire = 0 elif i < 4: col[i + 1] = col[i] col[i] = 0 i = i - 1 return [col, win_lose]
def play_game(): cp.pixels.brightness = 50 cp.pixels.fill(0) spot = -1 direction = 1 delay = delay_start win_lose = 0 while win_lose == 0: spot = spot + direction show_spot(spot) play_note(spot) if spot >= 5: direction = -1 if cp.button_b == False: win_lose = -1 elif spot <= 0: direction = 1 delay = delay - delay_incr if cp.button_a == False: win_lose = -1 elif spot > 1 and spot < 5: if cp.button_a or cp.button_b: win_lose = -1 time.sleep(delay) if delay <= delay_end: win_lose = 1 cp.pixels.fill(0) cp.stop_tone() if win_lose > 0: cp.play_file(winner) else: cp.play_file(loser)
def show_pixels(n): global col1, col2 cp.pixels.fill (0) i = 0 while i < 5: if col1[i] == 1: cp.pixels[i] = invader_color if col2[i] == 1: cp.pixels[9 - i] = invader_color if col1[i] == 2: cp.pixels[i] = explosion_color if col2[i] == 2: cp.pixels[9 - i] = explosion_color if col1[i] == 11: cp.pixels[i] = missile_color if col2[i] == 11: cp.pixels[9 - i] = missile_color i = i + 1 if n % 5 == 0: cp.play_file(beep_sound)
def spin_wheel(): cp.pixels.fill(0) delay = 0.1 n = 0 # spin faster while button is down while cp.button_a: set_pixels(n, delay) n = n + 1 delay = delay - random.random() * 0.05 if delay < 0.001: delay = 0.001 # slow down when button comes up while delay < 0.5: set_pixels(n, delay) n = n + 1 delay = delay + random.random() * 0.05 cp.play_file(alert_sound) return n + 1
def play_game(): cp.pixels.fill(blue) i = random.randint(8, 9) while i > 0: set_pixels(i) n = read_buttons(i) i = i - n if i == 0: cp.play_file(win_sound) return n = get_move(i) time.sleep(0.25) i = i - 1 set_pixels(i) cp.play_file(beep_sound) if n == 2: time.sleep(0.25) i = i - 1 set_pixels(i) cp.play_file(beep_sound) if i == 0: cp.play_file(lose_sound)
try: data_float = float(data_string) print("got float:", data_float) except: if data_string.isdigit(): data_float = int(data_string) print("got int:", data_float) else: print("got string:", data_string) data_float = None if data_float is not None: # work with a number # act on the data received if data_float > 100: cp.play_file("Wild_Eep.wav") elif data_float < 50: for x in range(0, 10, 1): cp.pixels[x - 1] = (0, 0, 255) cp.pixels[x] = (255, 0, 0) time.sleep(0.1) elif data_float >= 50 and data_float <= 100: for x in range(0, 10, 1): cp.pixels[x] = (0, 0, 0) time.sleep(0.1) if data_string is not None: # work with a string # act on the data received if data_string == "hello": cp.play_file("Wild_Eep.wav") elif data_string == "chase":
# Python code for Make Music with the Circuit Playground Express # by Rick Leander # Copyright (c) 2020 Rick Leander All rights reserved # Buy the book at https://www.amazon.com/author/rleander# # # shake or tap for tambourine sounds from adafruit_circuitplayground import cp # wave files tap = 'hi_snare.wav' shake = 'cymbal.wav' # main loop cp.detect_taps = 1 while True: if cp.tapped: cp.play_file(tap) if cp.shake(shake_threshold=12): cp.play_file(shake)
# Python code for Make Music with the Circuit Playground Express # by Rick Leander # Copyright (c) 2020 Rick Leander All rights reserved # Buy the book at https://www.amazon.com/author/rleander# # play the wave files found on the board from adafruit_circuitplayground import cp import os # find the .wav files and put them into a list files = os.listdir() wavs = [] for file in files: if (file.endswith('.wav')): wavs.append(file) #main loop i = 0 while True: if cp.button_a: cp.play_file(wavs[i]) if cp.button_b: i = i + 1 if i >= len(wavs): i = 0 cp.play_file(wavs[i])
"three thousand , six hundred"] intervals = [5, 10, 15, 20, 25, 30, 60, 120, 180, 240, 300, 600, 1800, 3600] interval_idx = intervals.index(30) ### default is 30 seconds intervalometer = False last_cmd_ns = None first_cmd_ns = None pixel_indication = True impending = False say_and_reset = False while True: ### CPX switch to left if cp.switch: if intervalometer: cp.play_file(manual_trig_wav) intervalometer = False if cp.button_a: ### left button_a if pixel_indication: cp.pixels.fill(SHUTTER_CMD_COLOUR) last_cmd_ns = time.monotonic_ns() fire_shutter() if first_cmd_ns is None: first_cmd_ns = last_cmd_ns print("Manual", "shutter release at", (last_cmd_ns - first_cmd_ns) / S_TO_NS) if pixel_indication: cp.pixels.fill(BLACK) while cp.button_a: pass ### wait for button release
# Python code for Make Music with the Circuit Playground Express # by Rick Leander # Copyright (c) 2020 Rick Leander All rights reserved # Buy the book at https://www.amazon.com/author/rleander# # # cowbell using touch pad from adafruit_circuitplayground import cp # main loop while True: if cp.touch_A3: cp.play_file ('cowbell.wav')
"""THIS EXAMPLE REQUIRES A WAV FILE FROM THE examples FOLDER IN THE Adafruit_CircuitPython_CircuitPlayground REPO found at: https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground/tree/master/examples Copy the "dip.wav" file to your CIRCUITPY drive. Once the file is copied, this example plays a wav file!""" from adafruit_circuitplayground import cp cp.play_file("dip.wav")
full(A4) time.sleep(lapse) blank(A4) time.sleep(lapse) if cp.button_b or 100<magnitude<125: for n in range(5): full(A2) time.sleep(lapse) blank(A2) time.sleep(lapse) if cp.tapped: curr_folder="numbers\\" temp=str(int((round(cp.temperature,0)))) cp.play_file(curr_folder+temp+ ".wav") else: pass """ import time import array import math import board import digitalio from audioio import RawSample from audioio import AudioOut import audiobusio
# This will execute at the same time on both boards pixels.fill(white) # Wait for and time how long it takes for player to press button start_t = time.monotonic() while not player_button(): pass finish_t = time.monotonic() # Turn-off NeoPixels pixels.fill(black) # Play the shooting sound # 16k mono 8bit normalised version of # https://freesound.org/people/Diboz/sounds/213925/ cp.play_file("PistolRicochet.wav") # The CPBs are no longer synchronised due to reaction time varying # per player # Exchange draw times player_reaction_dur = finish_t - start_t opponent_reaction_dur = get_opponent_reactiontime(player_reaction_dur) # Show green for winner and red for any misdraws (win, misdraw, draw, colour) = show_winner(player_reaction_dur, opponent_reaction_dur) my_results.append(colour) if misdraw: misdraw += 1 elif draw: draws += 1
def quizz(): # Variables d'état A1_touche = False A2_touche = False A4_touche = False A5_touche = False A6_touche = False go = False temps_equipes = [0, 0, 0, 0] temps_premier = None debut = time.monotonic_ns() // 1000000 # boucle infinie while True: # équipe 1 if A1_touche == False and tA1.value: t = time.monotonic_ns() // 1000000 if go: if temps_premier == None: temps_premier = t if temps_equipes[0] == 0: temps_equipes[0] = t A1_touche = True if A1_touche and tA1.value == False: #print('On ne touche plus A1!') #cpb.play_tone(440, 0.5) A1_touche = False # équipe 2 if A2_touche == False and tA2.value: t = time.monotonic_ns() // 1000000 if go: if temps_premier == None: temps_premier = t if temps_equipes[1] == 0: temps_equipes[1] = t A2_touche = True if A2_touche and tA2.value == False: #print('On ne touche plus A2!') #cpb.play_tone(294, 0.5) A2_touche = False # équipe 3 if A5_touche == False and tA5.value: t = time.monotonic_ns() // 1000000 if go: if temps_premier == None: temps_premier = t if temps_equipes[2] == 0: temps_equipes[2] = t A5_touche = True if A5_touche and tA5.value == False: #print('On ne touche plus A5!') #cpb.play_tone(440, 0.5) A5_touche = False # équipe 4 if A6_touche == False and tA6.value: t = time.monotonic_ns() // 1000000 if go: if temps_premier == None: temps_premier = t if temps_equipes[3] == 0: temps_equipes[3] = t A6_touche = True if A6_touche and tA6.value == False: #print('On ne touche plus A6!') #cpb.play_tone(294, 0.5) A6_touche = False # Prof if A4_touche == False and tA4.value: #print('On touche A4!') A4_touche = True if A4_touche and tA4.value == False and go == False: #cp.play_tone(587, 0.4) if cp.switch: cp.play_file("/sons/smb_powerup.wav") cp.pixels.fill((0, 0, 30)) # reinitialisation de l'animation animation[3] = 0 temps_go = time.monotonic_ns() // 1000000 go = True temps_premier = None print("GOOOOO") A4_touche = False # allumer la LED rouge si la switch est a off (son muet). cp.red_led = not cp.switch # condition pour finir la boucle : go == True et un temps a été enregistré il y a plus d'une seconde. if go and temps_premier != None and time.monotonic_ns( ) // 1000000 - temps_premier > 1000: cp.pixels.fill((0, 0, 0)) #fin de la boucle break # temps maximal atteint sans reponse if go and time.monotonic_ns( ) // 1000000 - temps_go > TEMPS_MAXIMUM * 1000: break # inactivite if go == False and time.monotonic_ns( ) // 1000000 - debut > INACTIVITE * 1000: animer() return temps_equipes
from adafruit_circuitplayground import cp import time from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) while True: if cp.button_a: cp.play_file("horn.wav") if cp.button_b: cp.play_file("horn.wav") R = 0 G = 0 B = 0 x, y, z = cp.acceleration tuplaxyz = str(x) + ',' + str(y) + ',' + str(z) print(tuplaxyz) cp.pixels.fill(((R + abs(int(x))), (G + abs(int(y))), (B + abs(int(z))))) uart.write(tuplaxyz.encode('utf-8')) time.sleep(0.1)
import time import math import random read_delay = 0.01 g = 9.801 a_floor = 1 x, y, z = cpx.acceleration a0 = math.sqrt(x * x + y * y + z * z) / g t0 = time.monotonic() x, y, z = cpx.acceleration a1 = math.sqrt(x * x + y * y + z * z) / g t1 = time.monotonic() while True: if cp.button_a: cp.pixels.fill((25, 25, 250)) cp.play_file("ignite.wav") while True: x, y, z = cpx.acceleration a2 = math.sqrt(x * x + y * y + z * z) / g t2 = time.monotonic() slope1 = (a1 - a0) / (t1 - t0) slope2 = (a2 - a1) / (t2 - t1) if (slope1 > 0 and slope2 < 0 and a1 > a_floor): randswing = random.randint(1, 4) if randswing == 1: cp.play_file("Swing01.wav") elif randswing == 2: cp.play_file("Swing02.wav") elif randswing == 3: cp.play_file("Swing03.wav")
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT """ Circuit Playground Bluefruit WAV Playback Press each button on the Circuit Playground Bluefruit to play a different WAV. """ from adafruit_circuitplayground import cp while True: if cp.button_a: cp.play_file("dip.wav") if cp.button_b: cp.play_file("rise.wav")
except adafruit_irremote.IRNECRepeatException: print("NEC repeat!") continue except adafruit_irremote.IRNECRepeatException as e: print("Failed to decode:", e.args) continue print("NEC Infrared code recieved: ", incomingCode) if incomingCode == NEC_PWR: lightMode1 = not lightMode1 if lightMode1 is True: cp.play_file("music3.wav") elif incomingCode == NEC_A: lightMode2 = not lightMode2 if lightMode2 is True: T = int(time.monotonic() - Value) COLOR_1 = (R, G, B) if T < 30: R = int(125 / T) G = int(60 / T) B = int(T * 5) COLOR_1 = (R, G, B) cp.pixels.fill(COLOR_1) cp.pixels.show()
tile_grid[0] = moon # moon is the tilegrid index location moon += 1 # resets timer crescent = time.monotonic() # resets tilegrid index if moon > 35: moon = 0 # if music is NOT playing and you press the a button... if not music_playing and (cp.button_a and not a_pressed): # music begins playing and will loop music_playing = True a_pressed = True print("music playing") # song plays once cp.play_file("moonlight_densetsu.wav") # music_playing state is updated music_playing = False # if the animation IS playing and you press the b button... if not animation_pause and (cp.button_b and not b_pressed): # the animation pauses by updating the animation_pause state animation_pause = True b_pressed = True # debugging REPL message print("animation paused") # if the animation is PAUSED and you press the b button... if animation_pause and (cp.button_b and not b_pressed): # the animation begins again by updating the animation_pause state animation_pause = False b_pressed = True # debugging REPL message
from audiopwmio import PWMAudioOut as AudioOut except ImportError: pass # not always supported by every board! def play_file(filename): print("Playing file: " + filename) wave_file = open(filename, "rb") with WaveFile(wave_file) as wave: with AudioOut(board.SPEAKER) as audio: audio.play(wave) while audio.playing: pass print("Finished") check = 0 while True: x, y, z = cp.acceleration print((x, y, z)) print(check) if abs(y) > 9: check = 0 if abs(y) < 9: check += 1 if check > 50: print("No!") cp.play_file("clock_short.wav") check = 0 time.sleep(10) time.sleep(0.1)