from machine import Pin, PWM
import time
import random
# botões
btn_single = Pin(14, Pin.IN, Pin.PULL_UP) # baixo
btn_multi = Pin(13, Pin.IN, Pin.PULL_UP) # cima
r = PWM(Pin(16)); g = PWM(Pin(17)); b = PWM(Pin(18))
for led in (r, g, b): led.freq(1000)
buzz = PWM(Pin(15))
buzz.freq(1000)
def color(rv, gv, bv):
r.duty_u16(rv); g.duty_u16(gv); b.duty_u16(bv)
def beep(freq, dur):
buzz.freq(freq)
buzz.duty_u16(50000)
time.sleep(dur)
buzz.duty_u16(0)
# escolher modo
modo = None
sub_modo_single = None
print("Escolhe o modo: botão de baixo = 1 jogador | botão de cima = multiplayer")
melhor_tempo = 999999
melhor_tempo_multi = 999999
dono_recorde = None
rondas_validas = 0
while modo is None:
if btn_single.value() == 0:
modo = 1
print("Modo single player")
time.sleep(0.5)
print("Escolhe o modo: botão de baixo = clássico | botão de cima = azul/verde")
while sub_modo_single is None:
if btn_single.value() == 0:
sub_modo_single = 1
print("Modo clássico selecionado")
if btn_multi.value() == 0:
sub_modo_single = 2
print("Modo azul/verde selecionado")
time.sleep(0.5)
if btn_multi.value() == 0:
modo = 2
print("Modo multiplayer")
time.sleep(0.5)
while True:
print("\nNova ronda")
color(65535, 0, 0); time.sleep(1) # Vermelho
color(65535, 35000, 0); time.sleep(1) # Amarelo
color(0, 0, 0)
wait_ms = random.randint(500, 2000)
start_wait = time.ticks_ms()
false_start = False
while time.ticks_diff(time.ticks_ms(), start_wait) < wait_ms:
if btn_single.value() == 0 or btn_multi.value() == 0:
false_start = True
break
if not false_start and (modo == 2 or (modo == 1 and sub_modo_single == 1)):
if random.randint(1, 2) == 1:
color(0, 0, 65535)
azul_start = time.ticks_ms()
# O azul fica aceso por 0.5 a 1 segundo
azul_duration = random.randint(500, 1000)
while time.ticks_diff(time.ticks_ms(), azul_start) < azul_duration:
if btn_single.value() == 0 or btn_multi.value() == 0:
false_start = True
break
color(0, 0, 0)
time.sleep(0.2)
if false_start:
print("Falso arranque! Clicaste antes do verde!")
color(65535, 0, 0); beep(200, 0.3)
time.sleep(1)
while btn_single.value() == 0 or btn_multi.value() == 0: pass
color(0, 0, 0); continue
cor_alvo = "verde"
if modo == 1 and sub_modo_single == 2:
if random.randint(1, 2) == 1:
color(0, 65535, 0)
cor_alvo = "verde"
else:
color(0, 0, 65535)
cor_alvo = "azul"
else:
color(0, 65535, 0)
start = time.ticks_ms()
if modo == 1:
while True:
if btn_single.value() == 0:
if sub_modo_single == 2 and cor_alvo == "azul":
print("Erraste o botão! Era Azul (Cima).")
beep(200, 0.5); break
reaction = time.ticks_diff(time.ticks_ms(), start)
print(f"Tempo: {reaction} ms")
if reaction < melhor_tempo:
melhor_tempo = reaction
print("Novo Recorde!")
print(f"Melhor pontuação atual: {melhor_tempo} ms")
beep(1000, 0.1); break
if sub_modo_single == 2 and btn_multi.value() == 0:
if cor_alvo == "verde":
print("Erraste o botão! Era Verde (Baixo).")
beep(200, 0.5); break
reaction = time.ticks_diff(time.ticks_ms(), start)
print(f"Tempo: {reaction} ms")
if reaction < melhor_tempo:
melhor_tempo = reaction
print("Novo Recorde!")
print(f"Melhor pontuação atual: {melhor_tempo} ms")
beep(1000, 0.1); break
else:
t1 = None; t2 = None
while t1 is None or t2 is None:
if t1 is None and btn_single.value() == 0:
t1 = time.ticks_diff(time.ticks_ms(), start)
beep(1000, 0.1)
print(f"Jogador 1 - ({t1} ms)")
if t2 is None and btn_multi.value() == 0:
t2 = time.ticks_diff(time.ticks_ms(), start)
beep(1000, 0.1)
print(f"Jogador 2 - ({t2} ms)")
if t1 < t2:
winner = 1; reaction = t1
print("O jogador 1 ganhou a ronda!")
else:
winner = 2; reaction = t2
print("O jogador 2 ganhou a ronda!")
if reaction < melhor_tempo_multi:
melhor_tempo_multi = reaction
dono_recorde = winner
print(f"Melhor pontuação geral: {melhor_tempo_multi} ms (Jogador {dono_recorde})")
rondas_validas += 1
if rondas_validas == 3:
print(f"\nO jogador {dono_recorde} venceu o jogo")
break
color(0, 0, 0)
while btn_single.value() == 0 or btn_multi.value() == 0: pass
time.sleep(1)