Create Conway's Game of Life With Python (Overview)
Wouldn’t it be cool to build a Python game that only requires initial user input and then seems to take on a mind of its own, creating mesmerizing patterns along the way? You can do exactly that with Conway’s Game of Life, which is about the evolution of cells in a life grid.
Implementing the Game of Life algorithm is a good exercise with many interesting challenges that you’ll have to figure out. Specifically, you’ll need to build the life grid and find a way to apply the game’s rules to all the cells on the grid so that they evolve through several generations.
In this video course, you’ll:
- Implement Conway’s Game of Life algorithm with Python
- Build a
curses
view to display the Game of Life grid - Create an
argparse
command-line interface for the game - Set up the game app for installation and execution
To get the most out of this video course, you should know the basics of writing object-oriented code in Python, creating command-line interface (CLI) apps with argparse
, and setting up a Python project.
00:00 Welcome to Create Conway’s Game of Life with Python. My name is Christopher and I will be your guide.
00:08
In this course, you will learn about cellular automaton, Conway’s Game of Life, game loops, curses
, and command line scripting.
00:19 The code in this course was tested using Python 3.12. There is nothing here particularly modern, though. Pretty much any Python 3 should work.
00:30 This is a build-it course. Rather than teach a specific concept, it incrementally walks you through the steps to create an application, and that application is Conway’s Game of Life.
00:41 Whether you’ve heard the name or not before, you’ve probably seen it or something inspired by it. Conway’s Game of Life is more a simulation than a game.
00:51 It consists of an animated grid with marked locations. Each frame in the animation is based on the previous frame with state rules applied based on the currently marked locations.
01:03
I’ll walk you through a few different ideas about how to store the data for the game. Then show you how to use the curses
library to create text-based animations on the terminal.
01:13
Once the basic simulation is running, I’ll add features like command line arguments and the use of a pyproject.toml
file, so you can run the program without having to type python
on the command line.
01:26 Insert your own joke about 42 here. Actually, this is a Python course. It should be an oblique reference to machines that go ping or the Galaxy Song instead.
Bartosz Zaczyński RP Team on Feb. 19, 2024
@Eric You might have more luck with rich, which uses the ANSI escape codes under the hood.
Christopher Trudeau RP Team on Feb. 19, 2024
Hi @Eric,
I haven’t used it myself, but the documentation:
docs.python.org/3/howto/curses.html
mentions a Window port:
that is supposed to be a drop-in replacement.
Eric on Feb. 19, 2024
Hi Christopher and Bartosz, thank you for your replies. I hacked my way through it using a TextView
using printf()
; not pretty but let me get on to the more interesting part about packaging and distribution.
I’ve taken a quick look at unicurses
and think its worth sharing the following.
-
unicurses
: The link to the UniCurses port isn’t hosted on PyPI and seems very out of date: the download links to source-forge with a 2010 release date and only has a few 1* reviews, but helpfully point toWindows-Curses
. -
Windows-Curses
(pypi.org/project/windows-curses/) appears significantly more active, is installable via pip, and was last released in October 2023. It’s imported viacurses
so should only need a new (platform-specific) line in thepyproject.toml:[project]:dependencies
lines. It seems to drop in nicely to theCursesView
implemented in this video.
I’ll look at rich
next!
Christopher Trudeau RP Team on Feb. 20, 2024
Hi @Eric,
With rich you’ll be able to create the project, but the commands for the display will be different. Likewise with another library that may work for you: asciimatics. If you’re comfy playing with different screen display code, you’ll be good, but the approach will be different.
The code is designed around having multiple visualization implementations, with the curses code going in the “views.py” file. In your case, you’d be implementing a different view, but if you use the same interface, the rest of the code should work. Good luck with it, and please leave a comment when you’ve got your solution, others will be interested as well.
bixiorimoldi on March 28, 2024
Hi, I am fairly new to Python and Object Oriented Coding and I found this article to be very instructive. Thank you! I have a question about the structure, namely the splitting into patterns.py and grid.py. With the latter we define an object of class LifeGrid that contains the pattern as a single attribute and has the methods to evolve, and display the pattern. To me it would seem natural to associate these methods directly to the Pattern class and get rid of the grid.py module. I wonder if this is just a matter of preference, or if I am missing something important aout this separation. Thank you for your help.
Christopher Trudeau RP Team on March 28, 2024
Hi bixiorimoldi,
The code in this course is based on the original tutorial:
realpython.com/conway-game-of-life-python/
Which is by Leodanis Pozo Ramos, and being lazy, I went with his coding style :)
For code this small, I agree, he’s used more modules and classes than I might have… but keep in mind that you’re on a code teaching site. My guess is Leodanis has done this on purpose to slyly teach the idea of separation here. If you were to start building a lot of different Conway interfaces on different platforms, his breakdown would be useful.
Another side-effect of writing it this way, especially for teaching it, is it makes it very clear the different purposes of the different structures and how they work together.
Personally, I tend to start small and then refactor if it turns out it is needed, but a lot of coders prefer a longer vision. The bigger the program, the more important the separation is, so getting some practice on smaller projects builds the skills you can use when you get to larger ones.
bixiorimoldi on March 29, 2024
Hi Christopher, thank you for your prompt and clear answer.
Become a Member to join the conversation.
Eric on Feb. 18, 2024
Is there a more cross-platform friendly alternative to
curses
? This tutorial relies oncurses
for terminal text display, but this isn’t available on Windows platforms.