Galactic Unicorn - Animated Pacman

Just a simple demo for a dark afternoon/evening. Can you add the yellow monster? Then make him give chase? (Must be more fun than all this football!)

# PacMan
# Tony Goodhew - TonyGo2 - 26th November 2022
# Pimoroni Galactic Unicorn

from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN
import time

# create a PicoGraphics framebuffer to draw into
graphics = PicoGraphics(display=DISPLAY_GALACTIC_UNICORN)

# create our GalacticUnicorn object
gu = GalacticUnicorn()

#Define some colours
BLACK = graphics.create_pen(0, 0, 0)
RED =  graphics.create_pen(255, 0, 0)
RED2 =  graphics.create_pen(128, 0, 0)
YELLOW = graphics.create_pen(255, 255, 0)
GREEN = graphics.create_pen(0, 255, 0)
GREEN2 = graphics.create_pen(0, 128, 0)
CYAN =  graphics.create_pen(0, 255, 255)
BLUE =  graphics.create_pen(0, 0, 255)
BLUE2 =  graphics.create_pen(0, 0, 128)
MAGENTA =  graphics.create_pen(255, 0, 255)
WHITE =  graphics.create_pen(200, 200, 200)
GREY =  graphics.create_pen(100, 100, 100)
DRKGRY =  graphics.create_pen(20, 20, 20)
PINK =  graphics.create_pen(40, 10, 0)

# PacMan
pman = [
    0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
    0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,
    0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,
    0,1,1,1,2,2,1,1,1,1,2,2,1,1,1,0,
    1,1,1,2,2,2,2,1,1,2,2,2,2,1,1,1,
    1,1,1,2,2,2,2,1,1,2,2,2,2,1,1,1,
    1,1,1,1,2,2,1,1,1,1,2,2,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,
    0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0
    ]

def eye(xx,yy):
    graphics.set_pen(BLUE2)
    graphics.pixel(xx,yy)
    graphics.pixel(xx+1,yy)
    graphics.pixel(xx,yy+1)
    graphics.pixel(xx+1,yy+1)
        
def show_man(c,xx,yy,xoff,yoff):
    p = 0
    for row in range(11):
        for col in range(16):            
            v = pman[p]
            if v ==0:graphics.set_pen(BLACK)
            if v ==1:graphics.set_pen(c)
            if v ==2:graphics.set_pen(WHITE)
            graphics.pixel(xx+col,yy+row)
            p = p + 1
    eye(xx+4+xoff,yy++4+yoff)
    eye(xx+10+xoff,yy++4+yoff)

show_man(RED,5,0,0,0)
gu.update(graphics)
time.sleep(1)
show_man(GREEN,5,0,1,0)
gu.update(graphics)
time.sleep(1)
show_man(PINK,5,0,-1,0)
gu.update(graphics)
time.sleep(1)
show_man(GREY,5,0,0,-1)
gu.update(graphics)
time.sleep(1)
show_man(CYAN,5,0,0,1)
gu.update(graphics)
time.sleep(1)
show_man(DRKGRY,5,0,0,0)
gu.update(graphics)
time.sleep(2)
graphics.set_pen(BLACK)
graphics.clear()

for i in range(-16,54,1):
    show_man(RED2,i,0,1,0)
    gu.update(graphics)
    time.sleep(0.08)
    graphics.set_pen(BLACK)
    graphics.clear()
 
for i in range(54,-17,-1):
    show_man(RED2,i,0,-1,0)
    gu.update(graphics)
    time.sleep(0.08)
    graphics.set_pen(BLACK)
    graphics.clear()

2 Likes

Managed to get the monster’s mouth working

# PacMan + Monster Muncher
# Tony Goodhew - TonyGo2 - 26th November 2022
# Pimoroni Galactic Unicorn

from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN
import time

# create a PicoGraphics framebuffer to draw into
graphics = PicoGraphics(display=DISPLAY_GALACTIC_UNICORN)

# create our GalacticUnicorn object
gu = GalacticUnicorn()

#Define some colours
BLACK = graphics.create_pen(0, 0, 0)
RED =  graphics.create_pen(255, 0, 0)
RED2 =  graphics.create_pen(128, 0, 0)
YELLOW = graphics.create_pen(255, 255, 0)
GREEN = graphics.create_pen(0, 255, 0)
GREEN2 = graphics.create_pen(0, 128, 0)
CYAN =  graphics.create_pen(0, 255, 255)
BLUE =  graphics.create_pen(0, 0, 255)
BLUE2 =  graphics.create_pen(0, 0, 128)
MAGENTA =  graphics.create_pen(255, 0, 255)
WHITE =  graphics.create_pen(200, 200, 200)
GREY =  graphics.create_pen(100, 100, 100)
DRKGRY =  graphics.create_pen(20, 20, 20)
PINK =  graphics.create_pen(40, 10, 0)

# PacMan
pman = [
    0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
    0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,
    0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,
    0,1,1,1,2,2,1,1,1,1,2,2,1,1,1,0,
    1,1,1,2,2,2,2,1,1,2,2,2,2,1,1,1,
    1,1,1,2,2,2,2,1,1,2,2,2,2,1,1,1,
    1,1,1,1,2,2,1,1,1,1,2,2,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,
    0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0
    ]

def eye(xx,yy):
    graphics.set_pen(BLUE2)
    graphics.pixel(xx,yy)
    graphics.pixel(xx+1,yy)
    graphics.pixel(xx,yy+1)
    graphics.pixel(xx+1,yy+1)
        
def show_man(c,xx,yy,xoff,yoff):
    p = 0
    for row in range(11):
        for col in range(16):            
            v = pman[p]
            if v ==0:graphics.set_pen(BLACK)
            if v ==1:graphics.set_pen(c)
            if v ==2:graphics.set_pen(WHITE)
            graphics.pixel(xx+col,yy+row)
            p = p + 1
    eye(xx+4+xoff,yy++4+yoff)
    eye(xx+10+xoff,yy++4+yoff)

def munch0(xx):
    graphics.set_pen(BLACK)
    graphics.clear()
    graphics.set_pen(YELLOW)
    graphics.circle(xx,5,5)

def munch1(xx):
    munch0(xx)
    graphics.set_pen(BLACK)
    for x in range(xx,xx+6,1):
        graphics.pixel(x,5)        

def munch2(xx):
    munch1(xx)
    graphics.set_pen(BLACK)
    for x in range(xx+2,xx+6,1):
        graphics.pixel(x,4)
        graphics.pixel(x,6)
    
def munch3(xx):
    munch2(xx)
    graphics.set_pen(BLACK)
    for x in range(xx+4,xx+6,1):
        graphics.pixel(x,3)
        graphics.pixel(x,7)

d = 0.2
for z in range(-17,53+7,1):
    seq = z % 6
    if seq == 0: munch0(z)
    if seq == 1: munch1(z)
    if seq == 2: munch2(z)
    if seq == 3: munch3(z)
    if seq == 4: munch2(z)
    if seq == 5: munch1(z)
    show_man(RED2,z + 15,0,1,0)
    gu.update(graphics)
    time.sleep(d)

Video here: PacMan - YouTube

3 Likes

Thanks for sharing your code. This looks amazing. Would like to use it for my picade max but as a loop together with the rainbow example. But I don’t know how to modify your code for a endless loop.

Best regards,

Mike

d = 0.2

while True:
    for z in range(-17,53+7,1):
        seq = z % 6
        if seq == 0: munch0(z)
        if seq == 1: munch1(z)
        if seq == 2: munch2(z)
        if seq == 3: munch3(z)
        if seq == 4: munch2(z)
        if seq == 5: munch1(z)
        show_man(RED2,z + 15,0,1,0)
        gu.update(graphics)
        time.sleep(d)

Glad you found it useful. Just change the end as shown above

Thank you. I tried to extend the code with help of ChatGPT for example to have more objects with different speed and moving from left to right and right to left. But the AI does not work and creates bad code which results in a crash.

Please, learn to code yourself and do not rely on AI. There is plenty of online help to get you started.