Autobahn/Frogger graphic demo for Stellar Unicorn

I’ve adapted my previous graphical demo from the Galactic Unicorn and the Cosmic Unicorn for my new Stellar Unicorn.

You can overclock it, but it gets rather overwhelming. I’m also not sure if it’s safe for someone sensitive to flashing images

import utime
import sys
#import machine
import random
from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY

# standard clock speed
#machine.freq(125_000_000)

# overclock to 200Mhz
#machine.freq(200_000_000)

# create stellar object and graphics surface for drawing
su = StellarUnicorn()
graphics = PicoGraphics(DISPLAY)

width = StellarUnicorn.WIDTH
height = StellarUnicorn.HEIGHT

# define buffer handling elements
buffer = PicoGraphics(DISPLAY)
cu = StellarUnicorn()
displaywidth = StellarUnicorn.WIDTH
displayheight = StellarUnicorn.HEIGHT

cu.set_brightness(1.0)

def set_color():
    return random.randrange(0, 256)

class Row():
    def __init__(self, i):
        self.rowno = i
        self.update_values()

    def update_values(self):
        self.red = set_color()
        self.green = set_color()
        self.blue = set_color()
# leave a trail?
        self.trail = random.choice((False, False, False, False, True))
        self.direction = random.choice((0, displaywidth - 1))
        self.currpos = self.direction
        self.delay = random.randint(1, 10)
        self.timeleft = self.delay
#        print(self)

    def __repr__(self):
        return f"Row(rowno = '{self.rowno}', RGB = ('{self.red}', '{self.green}', '{self.blue}'),
            leave a trail = '{self.trail}', direction = {self.direction},
            delay = {self.delay}, current position = {self.currpos}"

r = []
    
for i in range(displayheight):
    r.append(Row(i))

# define colours and font
bg = buffer.create_pen(0,0,0)

def bump_row(pix_row):
    reinitialize = False
    fg = buffer.create_pen(pix_row.red, pix_row.green, pix_row.blue)
    buffer.set_pen(fg)
    cu.update(buffer)
    if pix_row.direction == 0:
        buffer.pixel(pix_row.currpos, pix_row.rowno)
        if pix_row.currpos > 1:
            if not pix_row.trail:
                buffer.set_pen(bg)
                buffer.pixel(pix_row.currpos - 1, pix_row.rowno)
                buffer.set_pen(fg)
    else:
        buffer.pixel(pix_row.currpos, pix_row.rowno)
        if pix_row.currpos < displaywidth - 2:
            if not pix_row.trail:
                buffer.set_pen(bg)
                buffer.pixel(pix_row.currpos + 1, pix_row.rowno)
                buffer.set_pen(fg)
    cu.update(buffer)

utime.sleep(5.0) # time to get the video ready

while (True):
    for j in range(displayheight):
        r[j].timeleft -= 1
        if r[j].timeleft <= 0:
            bump_row(r[j]) # processes one row
            if r[j].direction == 0:
                r[j].currpos += 1
                if r[j].currpos >= displaywidth:
                    r[j].update_values()
            else:
                r[j].currpos -= 1
                if r[j].currpos < 0:
                    r[j].update_values()
            r[j].timeleft = r[j].delay
            
        if cu.is_pressed(StellarUnicorn.SWITCH_A): # exits on press of A
            cu.clear()
            sys.exit()

1 Like

Here’s the rest of the code.

while (True):
    for j in range(displayheight):
        r[j].timeleft -= 1
        if r[j].timeleft <= 0:
            bump_row(r[j]) # processes one row
            if r[j].direction == 0:
                r[j].currpos += 1
                if r[j].currpos >= displaywidth:
                    r[j].update_values()
            else:
                r[j].currpos -= 1
                if r[j].currpos < 0:
                    r[j].update_values()
            r[j].timeleft = r[j].delay
            
        if cu.is_pressed(StellarUnicorn.SWITCH_A): # exits on press of A
            cu.clear()
            sys.exit()
1 Like