The Game of Life for PyGame
Explanation
:
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if by loneliness.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any live cell with two or three live neighbours lives, unchanged, to the next generation.
- Any dead cell with exactly three live neighbours comes to life.
The initial pattern constitutes the 'seed' of the system and live/dead choices when filling the grid at game load should be handled by a random choice. The first generation is created by applying the above rules simultaneously to every cell in the seed — births and deaths happen simultaneously, and the discrete moment at which this happens is sometimes called a tick. This means that a copy of the board must be made at each pass to hold the new state; if you change the state "in place" then the life/death calculation of one cell will affect the next cell's calculation, which should not happen. The rules continue to be applied repeatedly to create further generations.
Additional rules
:
- Because we cannot do an infinite grid, yours will be limited to a size appropriate for the game window size.
- Pause the game using a 'p' keypress.
- Save the game state to a file with a 's' keypress. The user will not be asked for a filename, it will be a filename of your choosing.
- Load the game with an 'l' keypress and, like the save, no user selection happens, you load the same file that the save stage creates.
I would recommend using CirlceSprite or RectangleSprite for the cells of your grid.
Finally, a good test of whether your game is working is to use the examples on
Wikipedia. For example, first test a simple Glider and, once that works, move onto something like Gosper's Glider Gun.