Lacking a better name for a random graphic demo for the Galactic Unicorn, I’ve just decided to call it the “Autobahn” graphic toy.
Comments welcome.
import utime
import sys
import random
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN
# define buffer handling elements
buffer = PicoGraphics(display=DISPLAY_GALACTIC_UNICORN)
gu = GalacticUnicorn()
displaywidth = GalacticUnicorn.WIDTH
displayheight = GalacticUnicorn.HEIGHT
gu.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()
self.trail = random.choice((False, False, False, False, True)) # leave a trail?
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)
gu.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)
gu.update(buffer)
utime.sleep(5.0) # time to get the video ready
# initial definitions
#for j in range(displayheight):
# print(r[j])
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 gu.is_pressed(GalacticUnicorn.SWITCH_A): # exits on press of A
gu.clear()
sys.exit()