from time import sleep_ms, ticks_ms
from machine import Pin, UART
import random
import _thread
sleep_ms(50)
print("funciona")
#-------------------CONFIGURACION DE HILOS------------------------------------------------
semaforo=_thread.allocate_lock() #esto si lo uso
penalizar=False #creo que no uso esta mierda en ningun momento
#-------------------CONFIGURACION DE COMUNICACION ENTRE PLACAS-----------------------------------
#ESTO VA A ESTAR COMENTADO PORQUE EL WOKWI NO SIMULA SINO
#uart=UART(0,115200,tx=Pin(16),rx=Pin(17))
#uart.init(115200,bits=8, parity=None,stop=1)
print("asi es")
#recibo=b''
#estadoAnterior = ""
#def UART_lectura():
#global recibo, bloqueoJuego
#if uart.any() > 0:
#recibo = uart.readline()
#if recibo == b'on\n':
#bloqueoJuego = 1
#elif recibo == b'off\n':
#bloqueoJuego = 0
#def UART_estado():
#global t, estadoAnterior, bloqueoJuego
#if t <= 0 and estadoAnterior != 'murio':
#uart.write('murio\n')
#estadoAnterior = 'murio'
#elif bloqueoJuego == 1 and estadoAnterior != 'on':
#uart.write('on\n')
#estadoAnterior = 'on'
#elif bloqueoJuego == 0 and estadoAnterior != 'off':
#uart.write('off\n')
#estadoAnterior = 'off'
#-------------------CONFIGURACION JUEGO CABLES---------------------------------------------------
cable1=Pin(0,Pin.IN)#boton verde{si toca verde gana tocando azul}
cable2=Pin(1,Pin.IN)#boton amarillo {Si toca amarillo gana con verde}
cable3=Pin(2,Pin.IN)#boton negro{ si toca negro gana tocando amarillo}
cable4=Pin(3,Pin.IN)#boton azul {si toca azul gana tocando negro}
salida=Pin(28,Pin.OUT)
# 0, 1, 2, 3
ListaCables=[cable1,cable2,cable3,cable4]
B=len(ListaCables)
A=0
lvl1=0
lvl2=0
bloqueoJuego=0
#bloqueo=0
#configuracion Simon Dice
# Defino LEDs (pines 12 a 15) para que queden juntos en la placa
pinesSimon = range(12, 16) # pines 12, 13, 14, 15
PinSimon1=Pin(12,Pin.OUT)
PinSimon2=Pin(13,Pin.OUT)
PinSimon3=Pin(14,Pin.OUT)
PinSimon4=Pin(15,Pin.OUT)
ledsSimon=[PinSimon1,PinSimon2,PinSimon3,PinSimon4]
# Definir botones en pines fijos
botonesSimon = [
Pin(7, Pin.IN), # rojo
Pin(8, Pin.IN), # verde
Pin(9, Pin.IN), # azul
Pin(10, Pin.IN), # amarillo
]
# Lista de índices de colores según número de LEDs: [0, 1, 2, 3]
coloresSimon = list(range(len(ledsSimon)))
secuenciaSimon = []
nivelSimon = 1
JuegoSimon = True
nivelMaximoSimon = 10
esperaPrimeraSimon = True
correctaSimon = True # Controla si se añade nuevo color
#CODIGO DE CONTADOR--------------------------------------------------------------
Abcd=Pin(18,Pin.OUT)
Bbcd=Pin(19,Pin.OUT)
Cbcd=Pin(20,Pin.OUT)
Dbcd=Pin(21,Pin.OUT)
Tu=Pin(22,Pin.OUT)
Td=Pin(26,Pin.OUT)
Tc=Pin(27,Pin.OUT)
def bcd(n):
ListaNumeros=[[0,0,0,0],
[0,0,0,1],
[0,0,1,0],
[0,0,1,1],
[0,1,0,0],
[0,1,0,1],
[0,1,1,0],
[0,1,1,1],
[1,0,0,0],
[1,0,0,1]
]
Abcd.value(ListaNumeros[n][3])
Bbcd.value(ListaNumeros[n][2])
Cbcd.value(ListaNumeros[n][1])
Dbcd.value(ListaNumeros[n][0])
t=10
cmurio=0
#----------------------------------SEGUNDO NUCLEO---------------------------------------
def cmurioo():
global t
global cmurio
global lvl1
global lvl2
global A
global nivelSimon
global secuenciaSimon
global correctaSimon
global JuegoSimon
global treinicio
global JuegoSimon
if cmurio==1:
treinicio=20
print("AJAHSJHSJASHAJSH PERDISTE")
while treinicio>0:
print(treinicio)
treinicio=treinicio-1
sleep_ms(500)
cmurio=0
lvl1=0
lvl2=0
A=0
nivelSimon = 1
secuenciaSimon.clear()
esperaPrimeraSimon = True
correctaSimon = True
JuegoSimon = False
treinicio=20
t=180
JuegoSimon=True
print("ya podes jugar otra vez")
#aca van a estar las pulsaciones
#para que la segunda raspberry
#tambien reinicie
def contador():
global cmurio
global t
global cable1
global cable2
global cable3
global cable4
global B
global PinSimon1
global PinSimon2
global PinSimon3
global PinSimon4
listaEntradasCables=[cable1,cable2,cable3,cable4]
ListaSalidasSimon=[PinSimon1,PinSimon2,PinSimon3,PinSimon4]
global botonesSimon
global bloqueoJuego
#global bloqueo
#global uart
#global recibo
#estadoAnterior
while True:
#UART_estado
#UART_lectura
for x in range (B):
if listaEntradasCables[x].value()==0 or botonesSimon[x].value()==1:
sleep_ms(20)
if listaEntradasCables[x].value()==0 or botonesSimon[x].value()==1:
while t>0:
#UART_estado
#UART_lectura
centenas=t//100
decenas=(t//10)%10
unidades=t%10
tmultiplexado=1000
tciclos=15
ciclos=tmultiplexado//tciclos
for x in range(ciclos):
#habilito centenas
bcd(centenas)
Tc.value(1)
Td.value(0)
Tu.value(0)
sleep_ms(5)
#habilito decenas
bcd(decenas)
Tc.value(0)
Td.value(1)
Tu.value(0)
sleep_ms(5)
#habilito unidades
bcd(unidades)
Tc.value(0)
Td.value(0)
Tu.value(1)
sleep_ms(5)
Tc.value(0)
Td.value(0)
Tu.value(0)
print(t)
with semaforo:
t=t-1
cmurio=1
cmurioo()
def JuegoAbsolutoRpB1():
global cables1
global cables2
global cables3
global cables4
global salida
global B
global ListaCables
global A
global lvl1
global pinesSimon
global ledsSimon
global botonesSimon
global coloresSimon
global secuenciaSimon
global nivelSimon
global JuegoSimon
global nivelMaximoSimon
global esperaPrimeraSimon
global correctaSimon
global cmurio
global lvl2
global penalizar
global t
global bloqueoJuego
while True:
if cmurio==0: #and #bloqueo==0:
#INICIO DEL PRIMER NIVEL
for x in range(B):
if ListaCables[x].value()==0:
sleep_ms(20)
if ListaCables[x].value()==0 and lvl1==0:
print("entre al coso de cables")
bloqueoJuego=1
print("el bloqueo es:",bloqueoJuego)
if ListaCables[x]==ListaCables[0]:
while A==0:
for Y in range(B):
Yo=ListaCables[Y].value()
if Yo==0 and ListaCables[Y]!=ListaCables[3] and ListaCables[Y]!=ListaCables[0]:
print("pierde")
with semaforo:
if t>0:
t=t-2
print(t)
else:
cmurio=1
A=1
sleep_ms(100)
if ListaCables[3].value()==0:
sleep_ms(50)
if ListaCables[3].value()==0:
print("gana")
A=1
lvl1=1
bloqueoJuego=0
print("el bloqueo es:",bloqueoJuego)
if ListaCables[x]==ListaCables[1]:
while A==0:
for Y in range(B):
Yo=ListaCables[Y].value()
if Yo==0 and ListaCables[Y]!=ListaCables[0] and ListaCables[Y]!=ListaCables[1]:
print("pierde")
with semaforo:
if t>0:
t=t-2
print(t)
else:
cmurio=1
A=1
sleep_ms(100)
if ListaCables[0].value()==0:
sleep_ms(20)
if ListaCables[0].value()==0:
print("gana")
A=1
lvl1=1
bloqueoJuego=0
print("el bloqueo es:",bloqueoJuego)
if ListaCables[x]==ListaCables[2]:
while A==0:
A=1
lvl1=1
print("pierde todo el juego")
with semaforo:
t=0
print(t)
cmurio=1
if ListaCables[x]==ListaCables[3]:
while A==0:
for Y in range(B):
Yo=ListaCables[Y].value()
if Yo==0 and ListaCables[Y]!=ListaCables[2] and ListaCables[Y]!=ListaCables[3]:
print("pierde")
with semaforo:
t=t-2
if t>0:
print(t)
else:
cmurio=1
A=1
sleep_ms(100)
if ListaCables[2].value()==0:
sleep_ms(20)
if ListaCables[2].value()==0:
print("gana")
A=1
lvl1=1
bloqueoJuego=0
print("el bloqueo es:",bloqueoJuego)
for b in botonesSimon:
if b.value() == 1 and cmurio==0:
sleep_ms(20)
if b.value() == 1:
while b.value()==1:
pass
#ENTRADA AL SIMON DICE:
while JuegoSimon:
print("entre al simon")
bloqueoJuego=1
if nivelSimon > nivelMaximoSimon:
# Juego completado: secuencia aleatoria por 3s y el juego se bloquea
endTime = ticks_ms() + 3000
while ticks_ms() < endTime:
idx = random.choice(coloresSimon)
ledsSimon[idx].value(1)
sleep_ms(100)
ledsSimon[idx].value(0)
sleep_ms(50)
for led in ledsSimon:
led.value(1)
# Bloquear juego
JuegoSimon = False
break
# Agregar nuevo color
if correctaSimon:
secuenciaSimon.append(random.choice(coloresSimon))
# espera a que el jugador presione cualquier botón
if esperaPrimeraSimon:
esperandoSimon = True
while esperandoSimon:
for botonSimon in botonesSimon:
if botonSimon.value() == 1:
while botonSimon.value() == 1:
pass
sleep_ms(100)
esperandoSimon = False
break
sleep_ms(50)
esperaPrimeraSimon = False
# Mostrar la secuencia al jugador
for indiceSimon in secuenciaSimon:
ledsSimon[indiceSimon].value(1)
sleep_ms(250)
ledsSimon[indiceSimon].value(0)
sleep_ms(250)
correctaSimon = True
posicionSimon = 0
# Esperar la respuesta del jugador
while posicionSimon < len(secuenciaSimon) and correctaSimon:
for iSimon, botonSimon in enumerate(botonesSimon):
if botonSimon.value() == 1:
# Encender LED que toco
ledsSimon[iSimon].value(1)
sleep_ms(300)
ledsSimon[iSimon].value(0)
# me fijo si esra bien lo que toc o
if iSimon == secuenciaSimon[posicionSimon]:
posicionSimon += 1
else:
correctaSimon = False
print("equivocado")
with semaforo:
t=t-10
print(t)
if t<=0:
cmurio=1
JuegoSimon=False
break
while botonSimon.value() == 1:
pass
sleep_ms(400)
if correctaSimon:
# Pasar al siguiente nivel
nivelSimon += 1
sleep_ms(300)
else:
# Error: repite otra vezzz
for ledSimon in ledsSimon:
ledSimon.value(1)
sleep_ms(1000)
for ledSimon in ledsSimon:
ledSimon.value(0)
print("JuegoSimon terminado")
bloqueoJuego=0
print("el bloqueo es:",bloqueoJuego)
lvl2=1
#ACA IBA EL S CMURIO
cmurioo()
sleep_ms(50)
#_thread.start_new_thread(contador,())
contador()
#JuegoAbsolutoRpB1