# Balls demo for Galactic Unicorn

**URL:** <https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745>\
**Category:** Projects\
**Created:** [November 18, 2022, 12:20pm UTC](https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745 "2022-11-18T12:20:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tonygo2](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/tonygo2/32/7814_2.png) [@Tonygo2](https://forums.pimoroni.com/u/Tonygo2)\
**Post date:** [November 18, 2022, 12:20pm UTC](https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745/1 "2022-11-18T12:20:23Z")

</div>

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

```auto
# 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)

```

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [November 18, 2022, 7:44pm UTC](https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745/2 "2022-11-18T19:44:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pirate\_Zaphod](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@Pirate\_Zaphod](https://forums.pimoroni.com/u/Pirate_Zaphod)\
**Post date:** [March 13, 2023, 2:02pm UTC](https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745/3 "2023-03-13T14:02:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pixelchris](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/pixelchris/32/9755_2.png) [@pixelchris](https://forums.pimoroni.com/u/pixelchris)\
**Post date:** [April 11, 2023, 9:19am UTC](https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745/4 "2023-04-11T09:19:58Z")

</div>

Love this demo. Great work Tony!

---

<div class="post-metadata">

**Author:** ![Tonygo2](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/tonygo2/32/7814_2.png) [@Tonygo2](https://forums.pimoroni.com/u/Tonygo2)\
**Post date:** [April 11, 2023, 2:09pm UTC](https://forums.pimoroni.com/t/balls-demo-for-galactic-unicorn/20745/5 "2023-04-11T14:09:41Z")

</div>

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