Balls demo for Galactic Unicorn

I liked the Balls demo which came with the Pico Display so have modified it for the GU.

# Simple graphics with Pimoroni Galactic Unicorn - GU Balls
# Brightness control with LUX + & -
#  === Multiple Bouncing Blobs ===
#       Tony Goodhew 18th November 2022
# @TonyGo2@mastodon.social @tonygo09066653
# Based on the Pimoroni Pico Display Demo

from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN
import time
from machine import Pin, I2C
import random

# 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)
YELLOW = graphics.create_pen(255, 255, 0)
GREEN = graphics.create_pen(0, 255, 0)
CYAN =  graphics.create_pen(0, 255, 255)
BLUE =  graphics.create_pen(0, 0, 255)
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)

colours =[RED,YELLOW,GREEN,CYAN,BLUE,MAGENTA,GREY] # Blob colours

graphics.set_pen(YELLOW)         # Title screen
msg = "Balls"
graphics.text(msg, 15, 1, scale=1)
gu.update(graphics)
time.sleep(2)
graphics.set_pen(BLACK)
graphics.clear()

WIDTH = 53
HEIGHT = 11

class Ball:
    def __init__(self, x, y, r, dx, dy, pen):
        self.x = x
        self.y = y
        self.r = r
        self.dx = dx
        self.dy = dy
        self.pen = pen

# initialise shapes
balls = []
for i in range(0, 20):          # Favour smaller balls 
    r = 0
    v = random.randint(0, 9)
    if (v <= 5):
        r = 1
    elif (v > 5) and (v <= 7):
        r = 2
    else:
        r = 3
    print(v, r)
    
    dx = random.randint(0, 1)
    if dx == 0:
        dx = -1
    dy = random.randint(0, 1)
    if dy == 0:
        dy = -1
    xx = random.randint(r, r + (WIDTH - 2 * r))
    yy = random.randint(r, r + (HEIGHT - 2 * r))
    balls.append(
        Ball(
            xx,
            yy,
            r,
            dx,
            dy,
            colours[i % 7]
        )        
    )
    graphics.set_pen(colours[i % 7])
    graphics.circle(int(xx), int(yy), int(r))
    gu.update(graphics)
    time.sleep(0.4)

while True:
    graphics.set_pen(BLACK)
    graphics.clear()

    for ball in balls:
        ball.x += ball.dx
        ball.y += ball.dy

        xmax = WIDTH - ball.r
        xmin = ball.r
        ymax = HEIGHT - ball.r
        ymin = ball.r

        if ball.x < xmin or ball.x > xmax:
            ball.dx *= -1

        if ball.y < ymin or ball.y > ymax:
            ball.dy *= -1

        graphics.set_pen(ball.pen)
        graphics.circle(int(ball.x), int(ball.y), int(ball.r))
        
        # Adjust brightness ?
        if gu.is_pressed(gu.SWITCH_BRIGHTNESS_UP):
            gu.adjust_brightness(+0.01)

        if gu.is_pressed(gu.SWITCH_BRIGHTNESS_DOWN):
            gu.adjust_brightness(-0.01)
            
    gu.update(graphics)
    time.sleep(0.07)


5 Likes

Just gave it a go, nice work. I was wondering how it would look. This being an LED matrix. I think that’s pretty dam good considering what its running on.
I’m not knocking the Galactic Unicorn, it’s an apple and oranges thing.

I just tried this also and its a great demo. many thanks.

Love this demo. Great work Tony!

Thank you. Graphics coding is so much fun. Enjoy the board.

1 Like