Conways Game of Life in a 8×8 LED Matrix

So, I wrote some python functions and brought them together in the “Game of Life”-Animation running on the small SPI-interfaced 8 by 8 LED matrix with max7219 controler. My set of python functions can easily be used to create further funny code.
The basic libraries were already written Richard Hull and reused.

Have fun.

#!/usr/bin/env python

import max7219.led as led
import max7219.canvas as canvas
# import max7219.transitions as transitions
import time
from random import randrange

initgol = [0, 0b100, 0b100, 0b110, 0, 0b01110011, 0b00010101, 0b110]

def buf2mat(buffer):
    matrix = [[0 for i in range(8)] for j in range(len(buffer))]
    for col in range(len(buffer)): 
        for row in range(8):
            matrix[col][row] = buffer[col] & pow(2, row) # bitwise eval
    return matrix

def mat2buf(matrix):
    buffer = [0 for i in range(len(matrix))] 
    for col in range(len(matrix)):
        for row in range(len(matrix[col])):
            buffer[col] = buffer[col] | (pow(2, row) * matrix[col][row])
    return buffer

def countneighbors(matrix, wrap):
    neighbors = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
    for col in range(len(matrix)):
    for row in range(len(matrix[col])):
        for c, r in [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]]:
            if wrap:
                neighbors[col][row] += matrix[(col + c) % len(matrix)][(row + r) % len(matrix[col])]
            if not wrap:
                if not ( (col + c) < 0 or (row + r) < 0 or (col + c) >= len(matrix) or (row + r) >= len(matrix[col]) ):
                neighbors[col][row] += matrix[col + c][row + r]
    return neighbors

def randbit(probability):
    if randrange(100) < probability:
        return 1
    else:
        return 0

def randmatrix(probability, rows, cols):
    matrix = [[0 for i in range(rows)] for j in range(cols)]
    for col in range(cols):
        for row in range(rows):
            matrix[col][row] = randbit(probability)
    return matrix

def golrules(matrix):
    neighbors = countneighbors(matrix, 0)
    nextgen = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]

    for col in range(len(matrix)):
        for row in range(len(matrix[0])):
            if matrix[col][row] == 0:
                if neighbors[col][row] == 3:
                    nextgen[col][row] = 1
                else:
                    nextgen[col][row] = 0
            elif matrix[col][row] == 1:
                                if neighbors[col][row] < 2:
                                        nextgen[col][row] = 0
                                elif neighbors[col][row] > 3:
                                        nextgen[col][row] = 0
                else:
                    nextgen[col][row] = 1
        return nextgen

def text2buf(text):
    buffer = []
    for i in range(len(text)):
        if text[i].upper() in font5x3:
            buffer = buffer + font5x3[text[i].upper()]
        buffer = buffer + [0]
    return buffer

def drawbuf(buf, up, left):
    buffer = [0,0,0,0,0,0,0,0]
    for i in range(8):
        buffer[i] = buf[(i + left) % len(buf)] >> abs(up)
    canvas.gfxbuf = buffer
    return buffer

led.init()
horz = 0
vert = 0
speed = 4
led.brightness(0)
i = 0

while True: 
    m = randmatrix(20, 8, 8)
    for cycle in range(15):
        drawbuf(mat2buf(m), 0, 0)
        canvas.render()
        time.sleep( 1.0 / speed )
        m = golrules(m)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s