Skip to content

Instantly share code, notes, and snippets.

@weskerfoot
Last active November 9, 2021 19:10
Show Gist options
  • Save weskerfoot/ec7299017af5f4e4b2202303dedf8d99 to your computer and use it in GitHub Desktop.
Save weskerfoot/ec7299017af5f4e4b2202303dedf8d99 to your computer and use it in GitHub Desktop.
Game of Life on the adafruit circuit playground for an 8x8 led matrix
import board
import lib.adafruit_ht16k33.matrix as M
i2c = board.I2C()
matrix = M.Matrix8x8(i2c)
matrix.fill(0)
matrix.brightness = 0.1
matrix.blink_rate = 0
# glider
matrix[2,2] = 1
matrix[2,3] = 1
matrix[2,4] = 1
matrix[1,2] = 1
matrix[0,3] = 1
def print_lifeboard(new_lives):
for y in range(0, 8):
for x in range(0, 8):
matrix[x, y] = 1 if (x,y) in new_lives else 0
def get_transition(lifeboard, x, y):
cell = lifeboard[(x,y)]
positions = [[x+1, y],[x+1, y+1],[x+1, y-1],[x, y+1],[x, y-1],[x-1, y+1],[x-1, y],[x-1, y-1]]
num_live = sum([matrix[x % 7, y % 7] for x,y in positions])
if cell == 1 and (num_live < 2 or num_live > 3): return 0
if cell == 1 and (num_live == 2 or num_live == 3): return 1
if cell == 0 and (num_live == 3): return 1
return 0
def iterate():
global matrix
while True:
next_lives = []
for x in range(0, 8):
for y in range(0, 8):
if get_transition(matrix, x, y):
next_lives.append((x,y))
print_lifeboard(next_lives)
iterate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment