Game of Life on the adafruit circuit playground for an 8x8 led matrix
This file contains 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
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