Loading video player…

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.

Download

Sample Code (.zip)

22.0 KB
Download

Course Slides (.pdf)

1.1 MB
Avatar image for Eric

Eric on Feb. 18, 2024

Is there a more cross-platform friendly alternative to curses? This tutorial relies on curses for terminal text display, but this isn’t available on Windows platforms.

Avatar image for Bartosz Zaczyński

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.

Avatar image for Christopher Trudeau

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:

pypi.org/project/UniCurses

that is supposed to be a drop-in replacement.

Avatar image for Eric

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 to Windows-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 via curses so should only need a new (platform-specific) line in the pyproject.toml:[project]:dependencies lines. It seems to drop in nicely to the CursesView implemented in this video.

I’ll look at rich next!

Avatar image for Christopher Trudeau

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.

Avatar image for bixiorimoldi

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.

Avatar image for Christopher Trudeau

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.

Avatar image for bixiorimoldi

bixiorimoldi on March 29, 2024

Hi Christopher, thank you for your prompt and clear answer.

Become a Member to join the conversation.