Put this together last week for CamJam. Very basic and just uses the built in buttons. No extra parts needed.
# Display2 MAZE
# For 240x320 pixel display - for CamJam Nov 23
# Tony Goodhew 24 November 2023
from machine import Pin
import picoexplorer as display
import random, math, time
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY_2, PEN_RGB565
from pimoroni import RGBLED
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_RGB565, rotate=0)
display.set_backlight(0.8)
display.set_font("bitmap8")
led = RGBLED(6, 7, 8)
width, height = display.get_bounds()
BLACK = display.create_pen(0, 0, 0)
WHITE = display.create_pen(255,255,255)
YELLOW = display.create_pen(200,200,0)
GREEN = display.create_pen(0,200,0)
BLUE = display.create_pen(0,0,200)
RED = display.create_pen(200,0,0)
def sq(xx,yy,s,c): # Draws a filled square
display.set_pen(c)
for y in range(s):
for x in range(s):
display.pixel(xx+x,yy+y)
led.set_rgb(0,0,0) # Turn off the RGB LED
display.set_pen(BLACK)
display.clear()
display.update()
display.set_pen(GREEN)
display.text("MAZE", 100, 80, scale=5)
display.set_pen(RED)
display.text("Tony Goodhew", 100, 180, scale=2)
display.update()
time.sleep(2)
display.set_pen(BLACK)
display.clear()
KEY_UP = Pin(12,Pin.IN,Pin.PULL_UP)
KEY_DOWN = Pin(13,Pin.IN,Pin.PULL_UP)
KEY_LEFT= Pin(14,Pin.IN,Pin.PULL_UP)
KEY_RIGHT= Pin(15,Pin.IN,Pin.PULL_UP)
grid =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,0,0,1,1,0,0,1,0,0,1,0,0,0,1,
1,0,1,0,0,0,1,0,0,1,0,0,1,0,1,1,0,1,0,1,
1,0,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,
1,1,0,0,1,0,0,0,0,1,0,0,1,1,0,0,0,1,0,1,
1,0,1,0,0,1,0,0,1,0,1,0,0,0,0,1,0,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,
1,0,1,1,1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,1,
1,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,1,0,1,
1,1,1,1,0,1,0,0,0,0,0,0,1,1,0,1,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,0,1,0,1,
1,0,0,0,0,0,1,0,1,0,1,0,0,0,1,1,0,1,1,1,
1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,
1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
def update(): # Draw the grid
p = 0
for y in range(15):
for x in range(20):
if grid[p] == 1:
sq(x*16,y*16,16,BLUE)
p = p + 1
display.update()
start = time.ticks_ms() # Start timing
for q in range(2): # Target counter
datahx = [1,18] # Starting positions
datahy = [1,13]
datatx = [18,2]
dataty = [1,13]
x = datahx[q] # Hunter
y = datahy[q]
tx = datatx[q] # Target
ty = dataty[q]
update()
display.update()
sq(x*16,y*16,16,YELLOW) # Draw hunter
sq(tx*16,ty*16,16,RED) # Draw target
update()
display.update()
# ==== INNER LOOP =========
running = True
while(running):
key_flag=1 # Button pressed?
if(key_flag and (KEY_UP.value()==0 or KEY_DOWN.value()==0 \
or KEY_LEFT.value()==0 or KEY_RIGHT.value()==0 )):
time.sleep(0.05) # Debounce delay
key_flag=0
m = x # Store current coordinates of hunter
n = y
# Get new hunter position from buttons
if(KEY_UP.value() == 0) and (grid[x+20*(y-1)] == 0): # UP
y=y-1 # Calculate new position
if(y<0):
y=0
if(KEY_DOWN.value() == 0) and (grid[x+20*(y+1)] == 0): # DOWN
y=y+1
if(y>14):
y=14
if(KEY_LEFT.value() == 0) and (grid[x+20*y -1] == 0): # Left
x=x-1
if(x<0):
x=0
if(KEY_RIGHT.value() == 0) and (grid[x+20*y +1] == 0): # Right
x=x+1
if(x>19):
x=19
sq(m*16,n*16,16,BLACK) # Remove old position
sq(x*16,y*16,16,YELLOW) # Draw new position
if (tx == x) and (ty == y): # Caught the target!
stop = time.ticks_ms() # Get current time since start
sq(tx*16,ty*16,16,GREEN) # Colour target green
display.update() # Show green target
time.sleep(1.0) # Wait a moment
sq(tx*16,ty*16,16,BLACK) # Remove old target
running = False # Stop inner looping - new target
display.update() # Update the screen
# Show time taken
secs = (stop - start)/1000.0
print(secs)
display.clear()
display.update()
ss = str(secs)
display.set_pen(RED)
display.text("TIME:", 100, 60, scale=5)
display.set_pen(YELLOW)
display.text(ss, 100, 120, scale=5)
display.update()
A button joystick instead of the buttons makes it easier to control.