from machine import Pin, SPI, I2C
import time
import ssd1306
import framebuf
from max7219 import Matrix8x8
spi = SPI(1, baudrate=10000000, polarity=0, phase=0, sck=Pin(18), mosi=Pin(23))
cs = Pin(5, Pin.OUT)
matrix = Matrix8x8(spi, cs, 4)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
pacman_pos = 0
food_pos = [3, 10, 20, 25, 30]
score = 0
pacman_icon = bytearray([0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C])
pacman_fb = framebuf.FrameBuffer(pacman_icon, 8, 8, framebuf.MONO_HLSB)
def draw_pacman(position):
matrix.fill(0)
matrix.framebuf.blit(pacman_fb, position, 0)
matrix.show()
def draw_food():
for pos in food_pos:
matrix.pixel(pos % 32, pos // 32, 1)
matrix.show()
def update_oled():
oled.fill(0)
oled.text(f"Comidos: {score}", 0, 0)
if score >= 5:
oled.text("Lleno!", 0, 20)
oled.show()
def show_countdown():
for i in range(5, -1, -1):
matrix.fill(0)
matrix.text(str(i), 12, 0, 1)
matrix.show()
oled.fill(0)
oled.text("Cuenta: " + str(i), 0, 20)
oled.show()
time.sleep(1)
oled.fill(0)
oled.text("Finalizado", 0, 20)
oled.show()
def game_loop():
global pacman_pos, score
draw_food()
while score < 5:
draw_pacman(pacman_pos)
if pacman_pos in food_pos:
food_pos.remove(pacman_pos)
score += 1
update_oled()
if score >= 5:
matrix.fill(0)
matrix.show()
break
pacman_pos += 1
if pacman_pos >= 32:
pacman_pos = 0
time.sleep(0.5)
show_countdown()
matrix.brightness(5)
update_oled()
game_loop()