How to code the Game Of Life in Python without Pygame

A must do game for every programmer

Pedro Alvarado
6 min readMay 14, 2022

The Game Of Life, devised by the mathematician John Horton Conway, shows that simple rules can make complex and beautiful behaviors. I think that is a must do game for every programmer because it’s strikingly to build something that drives itself, in other words, a cellular automaton.

The Game Of Life, Python implementation without Pygame

We can see The Game Of Life as a world confined in to a two-dimensional grid. Each cell in the grid could be dead or alive. Here the rules enter, depending of the number of neighbors we change the state of a cell:

  • A dead cell with exactly 3 live neighbors comes back to life.
  • A living cell with 2 or 3 living neighbors will continue to live.
  • Otherwise, it will die from loneliness or overpopulation.

There are a lot of ways of building The Game Of Life. For example, you can build this game with Javascript and its canvas, but also you can build it without its canvas, which was the implementation that I built. I did it with divs and flexbox. You can see the result below.

The Game Of Life with Javascript without canvas

If you want to build it with Python you could use Pygame, which certainly makes things easier. But here, I want to show you how to build it without Pygame, in other words, we are going to code the game of life and render it in the terminal.

First steps

First, we need some Python libraries to make things a bit easier. The module “os” and “time” are already included with Python. You need to install “numpy” and “termcolor”.

  • Numpy. To create the grid.
  • Time. To use the function sleep a create a delay.
  • Os. To clear the terminal.
  • Termcolor. To give color to the cells.

Then, we are going to create a clean function, which are going to help us to clean the terminal. The function applies a different command depending on the operating system that you are in.

Then, we are going to already create the game. All the game will be engulfing in the entry point.

First we are going to create two variables, rows and cols, that indicates the number of rows and col that we want.

Second, we create the “gameState”, which is a numpy array, we use the function “random”, and then “randint”. To “randint” we indicate a range that the data can take. In this case from 0 to 2. We do this because we want the cell to have two states, alive or dead. So we want the grid to only have values of 0 (dead) or 1 (alive). We put the highest value as 2, since this value is not inclusive. And then we indicate the size for this numpy array, which is to indicate the rows and columns.

Third, we create a “render” variable, which is a triple quote string, this is due to we are going to print multiple lines. A then, we execute our clean function for the first time.

Number of neighbors

Then, we create a while loop, which you can see that is infinite, since the expression that we pass it is “True”. For this reason, we need to create a delay in order to let it refresh and to see changes every certain time. We do this with “time.sleep”. But before creating the delay we need to copy the “gameState” in to a new variable called “newGameState”. In this step we use the numpy copy function.

Why we copy the “gameState”? Because based on “gameState” we are going to get the number of neighbors and the value of the cells. In this part of the game, “gameState” is read only. The changes will be apply to “newGameState”. The reason for that is that we want to change the “gameState” every single iteration of the while loop, not every single iteration of the cells.

Then we create two nested for loops. The first one is walking the rows, and the second one is walking the cols. In the first loop we create a variable called “newRow”, here is going to be all the cells that are contain in the row.

In the second loop, the first thing we do is to get the number of neighbors of a certain cell. Below we a see an alive cell, surrounded with its 8 neighbors and their respective positions.

Now that we have the position of every neighbor, we access it and get its value. And because every cell can only have 0 or 1 as value, if a cell is alive it sums a one to our count of neighbors, and if a cell is dead it doesn’t sums nothing because it’s zero.

But, wait a second, why we add the modulo operator? Well, imagine if we have a simple python list.

We all agree on how indexes work. And we all know what would happen if I tried to access index 5, which doesn’t exist.

But, when we reach to the end of the list, we want to return to the start. How can we do this? The modulo operator enters here.

And that’s why we use the modulo operator, we can say that is a way of dealing with the edges of the game.

Final details

Now that we have the number of neighbors we need to apply the rules.

  • The first one is that is a cell is dead and it have 3 alive neighbors, comes back to life.
  • The second one is that if the cell is alive, and have less than two alive neighbors, it will die from loneliness.
  • The third one is that if the cell is alive, and have more than three alive neighbors, it will die from overpopulation.

Note here that the rules are apply in “newGameState” and the state of the cells is read from “gameState”.

Now that we have change the state, we need to add the information to the string of the row (“newRow”). If the cell is dead we are going to use a square with borders. If the cell is alive we are going to use a full square. Here, we are using “colored” a function of termcolor. First we specify the string, and then the color.

Now that we are out from the for loop of cols, in the for loop of rows, we add the row to “render”.

Once out of the two nested for loops, first, we clean the terminal, second, print the render, third, update the “gameState” by copy the “newGameState”, and finally, set “render” to an empty string.

The result of running the game is the next:

I recommend you to run the code in the native terminal of your computer, because if you run it in the terminal of for example VSCode, it doesn’t see too good. In the gif above I ran it in the terminal of Mac.

You have done an amazing game. You already know how to create Conway’s Game Of Life. As I said above, you can create complex and beautiful behaviors in this game. In the page of Wikipedia of the game, you can found different forms of arranging the cells to create interesting automatons. Congrats, You have create a very primitive version of a simulated universe.

Thank you for reading me and see you next!

--

--