This is an example implementation of the Conway's Game of Life.
The main purpose of this code is illustration of a good object-oriented design. The code is organized as follows:
life.pyis the main file to run. It reads the game parameters from the command line (see below) and createsBoardandDisplayclass instances.model.pyholds theBoardclass that is responsible for game logic (model).view_mpl.pyandview_pygame.pyare hold classes responsible for display of the board and user interaction (views). They use different engines to demonstrate that the good code organization allows to easily change the engine. Only one of these files is imported, depending on the parameters.- All the
*.txtfiles contain sample patterns.
Open the command line console and type
python life.py [OPTIONS]
where [OPTIONS] are optional arguments and can be:
-hor--helpshow help message and exit,-r RULEor--rule RULEgame rule (see below),-b WIDTH HEIGHTor--board WIDTH HEIGHTsize of the board,-p PATTERNor--pattern PATTERNfilename of the pattern,-d DENSITYor--density DENSITYdensity of live cells in a random board,-s STEPSor--steps STEPSnumber of color steps showing cell age,-f FPSor--fps FPSnumber of animation frames per second,-c COLORMAPor--colormap COLORMAPdisplay color map,-v ENGINEor--view ENGINEview engine (can bemplorpg2,pg3, orgl),-For--fullscreenstart in fullscreen mode.
The default Game of life rule set is B3/S23, which means that a new cell is born if it has exactly 3 neighbors and any alive
cell survives only if it has 2 or 3 neighbors.
However, different rules are possible. Some particularly interesting behaviors are summarized below:
| Rule | Description |
|---|---|
B45678/S2345 |
walled cities |
B345/S4567 |
diamond shape patterns |