Created
May 28, 2018 09:22
-
-
Save schneider-simon/639d043cadcfa1309596295aa95613ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from adafruit_circuitplayground.express import cpx | |
import time | |
import random | |
sound_enabled = False | |
cpx.pixels.brightness = 0.2 | |
cpx.pixels[0] = 0x003000 | |
cpx.pixels[9] = (30, 0, 0) | |
cpx.adjust_touch_threshold(200) | |
speed = 0 | |
a_values = [] | |
score_player_1 = 0 | |
score_player_2 = 0 | |
base_light_level = cpx.light | |
colors = {"yellow": (255, 255, 0), "green": (0, 255, 0), "red": (255, 0, 0), "white": (255,255,255), "black": (0,0,0)} | |
def play_tone(freq, duration): | |
if cpx.switch: | |
cpx.play_tone(freq, duration) | |
else: | |
time.sleep(duration) | |
def mean(numbers): | |
return float(sum(numbers)) / max(len(numbers), 1) | |
def show_number(maximum, minimum=0, color=(255, 255, 255)): | |
for i in range(0, 10): | |
if i < maximum and i >= minimum: | |
cpx.pixels[i] = color | |
else: | |
cpx.pixels[i] = (0, 0, 0) | |
def show_side(side, color=(255,255,255)): | |
if side == "left": | |
show_number(5, 0, color) | |
else: | |
show_number(10, 5, color) | |
def light_all(rgb): | |
for i in range(0, 10): | |
cpx.pixels[i] = rgb | |
def hand_detected(): | |
return abs(cpx.light - base_light_level) > 50 | |
def count_down(number): | |
for i in range(number, 0, -1): | |
show_number(i) | |
play_tone(i * 100, 0.07) | |
def reset_base_light(): | |
base_light_level = cpx.light | |
def play_step(): | |
count_down(10) | |
start_time = time.monotonic() | |
side = "left" if (random.randint(0,100) > 50) else "right" | |
print("side " + side) | |
counter = 0 | |
while True: | |
counter += 1 | |
if counter == 1: | |
reset_base_light() | |
button_side_pressed = None | |
if cpx.button_a: | |
button_side_pressed = "left" | |
if cpx.button_b: | |
button_side_pressed = "right" | |
if button_side_pressed == side: | |
play_delay = time.monotonic() - start_time | |
print("played: " + str(play_delay)) | |
show_side(side, colors["green"]) | |
cpx.play_file("sounds/success_3.wav") | |
return play_delay | |
else: | |
show_side(side, colors["yellow"]) | |
if button_side_pressed is not None and button_side_pressed is not side: | |
show_side(button_side_pressed, colors["red"]) | |
cpx.play_file("sounds/error.wav") | |
def get_led_index(side, number): | |
if side == "right": | |
return number | |
return 9 - number | |
def blink_led(index, color, times = 3): | |
for i in range(0,times): | |
cpx.pixels[index] = color | |
time.sleep(0.1) | |
cpx.pixels[index] = colors["black"] | |
time.sleep(0.1) | |
def show_score(last_winner): | |
global score_player_1, score_player_2 | |
light_all(colors["black"]) | |
for i in range(0, score_player_1): | |
cpx.pixels[get_led_index("right", i)] = colors["green"] | |
for i in range(0, score_player_2): | |
cpx.pixels[get_led_index("left", i)] = colors["green"] | |
blinking_index = 0 | |
if last_winner == 1: | |
blinking_index = get_led_index("right", score_player_1 - 1) | |
else: | |
blinking_index = get_led_index("left", score_player_2 - 1) | |
cpx.play_file("sounds/success_4.wav") | |
blink_led(blinking_index, colors["green"], 7) | |
light_all(colors["black"]) | |
def play_game(): | |
global score_player_1, score_player_2 | |
player1_delay = play_step() | |
time.sleep(1) | |
player2_delay = play_step() | |
time.sleep(1) | |
winning_side = None | |
if player1_delay < player2_delay: | |
winning_side = "right" | |
score_player_1 += 1 | |
show_score(1) | |
cpx.play_file("sounds/next_level.wav") | |
else: | |
winning_side = "left" | |
score_player_2 += 1 | |
show_score(2) | |
cpx.play_file("sounds/next_level.wav") | |
print("score: " + str(score_player_1) + " " + str(score_player_2)) | |
time.sleep(1) | |
time.sleep(3) | |
while True: | |
play_game() | |
game_over = score_player_1 == 5 or score_player_2 == 5 | |
if game_over: | |
if score_player_1 == 5: | |
print("player 1 won") | |
if score_player_2 == 5: | |
print("player 2 won") | |
score_player_1 = 0 | |
score_player_2 = 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment