import random
import utime
from machine import Pin, PWM
# Configuración de los LEDs en su respectivo pin
leds = [Pin(16, Pin.OUT), Pin(17, Pin.OUT), Pin(18, Pin.OUT), Pin(15, Pin.OUT), Pin(14, Pin.OUT), Pin(13, Pin.OUT)]
Error = Pin(10,machine.Pin.OUT)
# Configurar los botones
buttons = [Pin(19, Pin.IN, Pin.PULL_UP), Pin(21, Pin.IN, Pin.PULL_UP), Pin(22, Pin.IN, Pin.PULL_UP), Pin(7, Pin.IN, Pin.PULL_UP), Pin(6, Pin.IN, Pin.PULL_UP), Pin(5, Pin.IN, Pin.PULL_UP)]
# Configuración del boton para reiniciar el juego
Again = Pin(11, Pin.IN, Pin.PULL_UP)
# configurando el 7 segmentos para mostrar la puntuación
segmentos = {
1: [0,1,0,0,1,0,0],
2: [1,1,0,1,0,1,1],
3: [1,1,0,1,1,1,0],
4: [0,1,1,1,1,0,0],
5: [1,0,1,1,1,1,0],
6: [1,0,1,1,1,1,1],
7: [1,1,0,0,1,0,0],
8: [1,1,1,1,1,1,1],
9: [1,1,1,1,1,1,0]
}
pines_lvl = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(2, Pin.OUT), Pin(3, Pin.OUT), Pin(28, Pin.OUT), Pin(27, Pin.OUT), Pin(26, Pin.OUT)]
def mostrar_numero(n):
estado = segmentos[n]
for i in range(7):
pines_lvl[i].value(estado[i])
# Configuración del buzzer para que suene con cada acierto o al errar
Buzzalrm = PWM(Pin(9))
frecuencia = 100
duracion = 1
def Alarma (frecuencia, duracion):
Buzzalrm.freq(frecuencia)
Buzzalrm.duty_u16(32767)
utime.sleep(duracion)
Buzzalrm.duty_u16(0)
# Función para mostrar la secuencia
def Secuencia(sequence, level):
delay = max(0.2, 1.0 - (level * 1.5))
for i in sequence:
leds[i].on()
utime.sleep(delay)
leds[i].off()
utime.sleep(delay)
# Función para recibir los datos de los botones
def Memoria(length):
input_sequence = []
while len(input_sequence) < length:
for i, button in enumerate(buttons):
if not button.value(): # botón presionado
leds[i].on()
utime.sleep(0.3)
leds[i].off()
input_sequence.append(i)
utime.sleep(0.3)
return input_sequence
# Juego
def Jugar():
sequence = []
level = 1
Mode = "play"
Error.value(0)
utime.sleep(1)
while True:
if Mode == "play":
mostrar_numero(level)
print("Nivel:", level)
sequence.append(random.randint(0, len(leds) - 1))
Secuencia(sequence, level)
player_input = Memoria(len(sequence))
if player_input != sequence:
Error.value(1)
Alarma(100, 1)
print("Como que así no era...")
Mode = "pause"
else:
Alarma(1000, 0.5)
print("Excelente, ahora el que sigue")
level += 1
if level > 9:
level = 9
print("FELICIDADES, ALCANZASTE EL NIVEL MÁXIMO!!!")
Mode = "pause"
elif Mode == "pause":
while Again.value():
utime.sleep(0.1)
while not Again.value():
utime.sleep(0.1)
utime.sleep(0.2)
Mode = "play"
Error.value(0)
sequence = []
level = 1
mostrar_numero(level)
utime.sleep(0.5)
while True:
Jugar()