Implementing the Game of Life

What Was Used

To implement GOL I used, a Python framework based on Processing, a notorious framework amongst the generative art community. I chose this because it was the easiest way I could implement GOL.

Implementation — Part 1

First, it would make sense to define class which outlines what a cell would look like. A cell has a state (alive or dead), and a set number of neighbors (something we’ll tackle later on). Also, each cell exists in a column and a row, since we they are going to be in a grid. This is what our cell class would look like:

class Cell:
def __init__(self, col, row):
self.col = col
self.row = row
# all cells are initially dead, we input the alive ones.
self.state = False
class Grid:
def __init__(self, cell_width, width_, height_):
self.width_ = width_
self.height_ = height_
self.cell_width = cell_width
self.cols = self.width_ // self.cell_width
self.rows = self.height_ // self.cell_width

“Creating” the Grid.

There are a couple of methods we need to implement in order for the code to be simpler later. The first method is the create() method, which actually fills in the empty list self.grid with cells. I’m going to skip directly to showing the code, and I’m going to elaborate on what it is doing later on.

grid = [column1, column2, column3] # as an example
column1 = [cell1, cell2, cell3]
column2 = [cell4, cell5, cell6]
column3 = [cell7, cell8, cell9]
grid = [
[cell1, cell2, cell3], # used to be column1
[cell4, cell5, cell6], # used to be column2
[cell7, cell8, cell9] # used to be column3

Showing the Grid

We need to have a method that reveals, or shows, the cells in self.grid. This is reasonably easy to do. All you have to do is loop over each column in the grid (self.grid):

for column in self.grid:
for column in self.grid:
for cell in column:
for column in self.grid:
for cell in column:
For some reason while writing the code, I decided to use the name `col` instead of `column`.

Erasing the Grid

We will want to be able to erase the components of the array self.grid for reasons that will become clear later. Usually how clearing a list in Python works is by using the clear() method (e.g., lst.clear()). However, in, the framework I’m using, this didn’t work (probably because clear() is a special function that is defined globally).

Getting the Neighbors of a Cell

We will want to be able to get the neighbors of a cell later. To make the code look less messy, we might as well define a method that will do that.

return [cell.state for cell in neighbors]

Default Configurations

It might be easier to test this implementation if we already have some initial configurations to choose from instead of manually inputting the initial configuration into the grid. That’s why I created some methods that allowed the user to, with a press of a button, initialize the grid to either a Gosper glider gun, a glider, a pulsar, or a line configuration. You can Google what these are to get an idea of what I’m saying.

Implementation — Part 2

Alright, we’ve created the Grid and Cell classes and their methods, let’s put them to use.

  1. Create a grid, called reading_grid.
  2. Create another empty grid, called writing_grid. (By empty, I mean a grid where all cells are initialized to being dead.)
  3. Based on reading_grid, fill in writing_grid.
  4. Make writing_grid the reading_grid of the next generation.
  5. Show reading_grid on the screen.
  6. Erase the elements of writing_grid.
reading_grid = Grid(cell_width, *DIM)
writing_grid = Grid(cell_width, *DIM)
def setup():
size(*DIM) # unpack the values of DIM
def keyPressed():
global running
if key == ENTER:
running = not running
def keyPressed():
global running
if key == ENTER:
running = not running
if not running:
if key == "c":


Some of you who might already tried this code out noticed that the program started to become slower once there were too many cells in the grid. That’s because of the way this thing works. We are constantly looping over every cell. Say we have a grid of 20 cells-by-20 cells. We are looping over 400 cells. And that’s just to show the grid, and not to actually do the evaluation and updating processes.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmed Addous

Ahmed Addous

Interested in programming. I write some articles from time to time.