It is a shame about the low level of documentation at the moment. We just have to post our findings when we work something out.
Thank you for this code. I picked up a great deal from reading it.
I bought an Explorer and it was very easy to port it to the Explorer screen with 48 x 48 cells displayed.
I’ve put the Explorer version below to help those users. I hope this is OK with you.
It will be very easy to port to Display as well.
# Conway's Game of Life
# Converted by Tony Goodhew from Steve Baine's post for Pico Unicorn Pack
# https://forums.pimoroni.com/t/pico-unicorn-pack-not-working-in-micropython/15997/4
# 13 Feb 2021 Tested with Pimoroni UF2 Version 0.0.7
import picoexplorer as display
import time, random
width = display.get_width()
height = display.get_height()
display_buffer = bytearray(width * height * 2)
display.init(display_buffer)
w = 48 # Horizontal cell
h = 48 # Vertical cells
class Cells:
def __init__(self):
self.cells = [[0]*h for i in range(w)]
def clear_all(self):
for x in range(w):
for y in range(h):
self.cells[x][y] = 0
def set_random_cells_to_value(self, prob, value):
for x in range(w):
for y in range(h):
if random.random() < prob:
self.cells[x][y] = value
def is_alive(self,x,y):
x = x % w
y = y % h
return self.cells[x][y] == 255
def get_num_live_neighbours(self, x, y):
num = 0
num += (1 if self.is_alive(x-1,y-1) else 0)
num += (1 if self.is_alive(x ,y-1) else 0)
num += (1 if self.is_alive(x+1,y-1) else 0)
num += (1 if self.is_alive(x-1,y) else 0)
num += (1 if self.is_alive(x+1,y) else 0)
num += (1 if self.is_alive(x-1,y+1) else 0)
num += (1 if self.is_alive(x ,y+1) else 0)
num += (1 if self.is_alive(x+1,y+1) else 0)
return num
def iterate_from(self, fromCells):
for x in range(w):
for y in range(h):
num_live_nbrs = fromCells.get_num_live_neighbours(x,y)
is_alive = fromCells.is_alive(x,y)
if is_alive and (num_live_nbrs < 2 or num_live_nbrs > 3):
self.cells[x][y] = 0 # Died
elif not is_alive and num_live_nbrs == 3:
self.cells[x][y] = 255 # Born
else:
self.cells[x][y] = fromCells.cells[x][y] # Unchanged state
def ExportToScreen(cells):
for x in range(w):
for y in range(h):
value = cells[x][y]
display.set_pen(value,value,0)
xx = x * 5
yy = y * 5
for xr in range(5):
for yr in range(5):
display.pixel(xx+xr,yy+yr)
display.update()
def blk():
display.set_pen(0,0,0)
display.clear()
display.update()
def title(msg,r,g,b):
blk()
display.set_pen(r,g,b)
display.text(msg, 20, 70, 200, 4)
display.update()
time.sleep(2)
title("Conway's game of life",0,0,255)
display.set_pen(120,120,120)
display.text(" Hold button Y to halt", 5, 190, 230, 2)
display.text(" Button A to re-seed", 5, 210, 230, 2)
display.update()
time.sleep(3)
cellsA = Cells()
cellsB = Cells()
start = True
display.set_pen(0,0,0)
display.update()
running = True
while running:
if start:
cellsA.clear_all()
cellsA.set_random_cells_to_value(0.2, 255)
blk()
display.set_pen(200,200,200)
display.text("New Colony", 40, 120, 230, 3)
time.sleep(0.5)
display.update()
start = False
ExportToScreen(cellsA.cells)
cellsB.iterate_from(cellsA)
(cellsA, cellsB) = (cellsB, cellsA)
start = display.is_pressed(display.BUTTON_A)
if display.is_pressed(3): # Y button is pressed ?
running = False
blk()
print("Finished")
Video added here:
Conway’s Game of Life - Demonstration With Code - Instructables