Created
November 2, 2025 19:28
-
-
Save deathweaselx86/b0b4dcc60d8d58fb29ea5f5f7b9f526f to your computer and use it in GitHub Desktop.
"Conway's Game of Life"-style simulation for the Pimoroni Galactic Unicorn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from galactic import GalacticUnicorn | |
| from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY | |
| import random | |
| import time | |
| gu = GalacticUnicorn() | |
| graphics = PicoGraphics(DISPLAY) | |
| width = GalacticUnicorn.WIDTH # 53 | |
| height = GalacticUnicorn.HEIGHT # 11 | |
| current_gen = [[0 for y in range(height)] for x in range(width)] | |
| next_gen = [[0 for y in range(height)] for x in range(width)] | |
| pens = { | |
| 0: graphics.create_pen(0,0,0), | |
| 1: graphics.create_pen(255,255,255), | |
| 2: graphics.create_pen(0,255,255), | |
| 3: graphics.create_pen(255,0,0) | |
| } | |
| def count_neighbors(x, y): | |
| """Count live neighbors around cell (x,y)""" | |
| count = 0 | |
| for dx in [-1, 0, 1]: | |
| for dy in [-1, 0, 1]: | |
| if dx == 0 and dy == 0: | |
| continue # Skip the cell itself | |
| nx, ny = (x + dx) % width, (y + dy) % height # Wrap edges | |
| if current_gen[nx][ny] > 1: | |
| count += 1 | |
| return count | |
| def update_generation(): | |
| for x in range(width): | |
| for y in range(height): | |
| neighbors = count_neighbors(x, y) | |
| alive = current_gen[x][y] > 0 | |
| if alive: | |
| if neighbors < 2 or neighbors > 4: # Underpopulation or overpopulation | |
| next_gen[x][y] = 0 | |
| else: | |
| if neighbors == 3 or neighbors == 0: # Reproduction | |
| next_gen[x][y] = random.randint(0,3) | |
| current_gen[:] = [row[:] for row in next_gen] | |
| def draw(): | |
| """Render the current generation to the display""" | |
| graphics.set_pen(pens[0]) # Black background | |
| graphics.clear() | |
| graphics.set_pen(graphics.create_pen(255, 255, 255)) # White for alive | |
| for x in range(width): | |
| for y in range(height): | |
| if current_gen[x][y] > 1: | |
| graphics.set_pen(pens[current_gen[x][y]]) | |
| graphics.pixel(x, y) | |
| gu.update(graphics) | |
| def seed_random(): | |
| for _ in range(100): # Start with 100 random live cells | |
| x = random.randint(0, width - 1) | |
| y = random.randint(0, height - 1) | |
| current_gen[x][y] = random.randint(0, 3) | |
| # Main loop | |
| seed_random() | |
| while True: | |
| draw() | |
| update_generation() | |
| time.sleep(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment