import machine
import max7219
import time
import random
from machine import Pin, SPI
spi = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(4), mosi=Pin(2), miso=Pin(1))
ss = Pin(5, Pin.OUT)
display = max7219.Matrix8x8(spi, ss, 4)
Verde = Pin(3, Pin.IN, Pin.PULL_UP)
Azul = Pin(10, Pin.IN, Pin.PULL_UP)
FILAS = 8
COLUMNAS = 32
N = 10
matriz = [[0] * COLUMNAS for i in range(FILAS)]
cont = 0
while cont < N:
fila = random.randint(0, 7)
columna = random.randint(0, 31)
if matriz[fila][columna] == 0:
matriz[fila][columna] = 1
cont += 1
NADA = 0
ESPERA = 1
EVENTO = 2
IZQUIERDA = 0
ARRIBA = 1
DERECHA = 2
ABAJO = 3
def procesaEntrada(estadoActual, valorPin):
if estadoActual == NADA and valorPin == False:
return EVENTO
if estadoActual == EVENTO:
return ESPERA
if estadoActual == ESPERA and valorPin == False:
return ESPERA
return NADA
def calcularDireccion(direc, L, R):
if direc == IZQUIERDA and L == 2:
return ABAJO
if direc == IZQUIERDA and R == 2:
return ARRIBA
if direc == ARRIBA and L == 2:
return IZQUIERDA
if direc == ARRIBA and R == 2:
return DERECHA
if direc == DERECHA and L == 2:
return ARRIBA
if direc == DERECHA and R == 2:
return ABAJO
if direc == ABAJO and L == 2:
return IZQUIERDA
if direc == ABAJO and R == 2:
return DERECHA
return direc
cont = 0
x = 4
y = 0
L = NADA
R = NADA
direc = DERECHA
PUNTAJE = 0
while x < 8 and x > -1 and y < 32 and y > -1:
for i in range(FILAS):
for j in range(COLUMNAS):
if matriz[i][j] == 1:
if i == x and j == y:
matriz[i][j] = 0
PUNTAJE = PUNTAJE + 100
display.pixel(i, j, 1)
L = procesaEntrada(L, Azul.value())
R = procesaEntrada(R, Verde.value())
time.sleep_ms(5)
if L == EVENTO or R == EVENTO:
direc = calcularDireccion(direc, L, R)
cont = cont + 1
if cont == 3:
cont = 0
if direc == IZQUIERDA:
y = y - 1
if direc == ARRIBA:
x = x - 1
if direc == DERECHA:
y = y + 1
if direc == ABAJO:
x = x + 1
display.pixel(x, y, 1)
display.show()
display.fill(0)
display.show()
print(PUNTAJE)
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1