Skip to content

hnrkcode/game-of-life

Repository files navigation

Game of life

Python made-with-pygame Package manager: uv code style: ruff type checked: ty codecov License: MIT

Python implementation of Conway's game of life algorithm

Demonstration of game of life

Game of life is an algorithm invented by John Horton Conway in 1970. The game of life is set on a 2-dimensional grid made up by many small cells. Each cells is in one of two states, alive or dead. A cells state is decided by the algorithm, which has four simple rules:

  1. A cell dies if it has less than two living neighbors.
  2. A cell survives until the next generation if it has two or three neighbors.
  3. A cell with more than three neighbors dies.
  4. A dead cell with exactly three neighbors turns into a living cell.

Setup

This project uses uv for fast Python dependency management. All dependencies are defined in pyproject.toml.

  1. Create and activate a virtual environment (if not already active):

    uv venv
    source .venv/bin/activate

    (The .venv folder is created by default.)

  2. Install all dependencies (including dev dependencies):

    uv sync --all-groups
  3. Run the game:

    make start

Development

When adding new features and fixing bugs do it with a new branch and create a pull request because the changes will be automatically be included in the changelog in the next release. Other changes like refactoring and formatting can be done on the master branch.

Make sure bump-my-version is installed to use the release command, that will automatically bump the version, create a git tag and push it to GitHub were GitHub actions will create a new release and publish the game on itch.io.

There is a pre-commit hook that will only allow you to bump the version with bump-my-version if there are no lint, formatting, type checking or test errors.

make release

Controls

key description
P Pause algorithm
R Remove all cells from the board
F11 Toggle on/off fullscreen
ESC Quit program
Enter Run algorithm
LEFT MOUSE BUTTON Click or hold to draw new cells
RIGHT MOUSE BUTTON Click or hold to erase cells
UP, DOWN or SCROLL WHEEL Choose pattern from predefined patterns
Hold CTRL + click LEFT MOUSE BUTTON Paste chosen pattern onto the grid
CTRL + Q Rotate pattern 90° counter-clockwise
CTRL + E Rotate pattern 90° clockwise
CTRL + W Flip pattern vertically
CTRL + S Flip pattern horizontally

Commands

The following commands are available for development tasks:

Command Description
make start Run the game
make test Run all pytests with coverage
make lint Lint code with Ruff
make format Format code with Ruff
make typecheck Type check code with Ty

License

This project is licensed under the MIT License. See the LICENSE file for details.