Tuesday 14 April 2015

Game of Life

Game of Life (gol)

gol is a simple python (with pygame) implementation of famous scouser John Horton Conway's cellular automaton known as 'Game of Life' or 'Life'. Well, between you and me, it's not actually a game. But it really is a facinating programming exercise with some surprising results. It is amazing how much variation can emerge from a small rule set.

The Results

Here is a screen shot of a Gosper Glider Gun creating 'gliders'.


See the Run the Game of Life section below for a description of how to set-up and fire the glider gun.

The Rules of the Game and Life Patterns


Download the Game of Life

The gol project files are stored in the gol repository on github - see the github bootcamp instructions for an introduction to github. To download gol:

git clone https://github.com/terry.dolan/gol.git project-gol

This will create a project-gol directory with all the key files. 

S cd project-gol

Run the Game of Life

To run gol:

$ python gol.py

On start-up the program displays the initialisation screen. This screen is used to set-up the start conditions of each cell in the grid.

The short-cut options are summarised on the window title. The screen makes it easy to set-up some interesting life patterns:

    • Type r to create a random pattern. 
    • Or you can use the mouse to create your own pattern.

      Type RETURN to apply the game of life rules to each cell or ESC to exit.

      Live or Die

      Watch each cell live or die, subject to the rules of the game.

      Type RETURN to re-run the initialisation screen or ESC to exit.

      Code Structure

      Hopefully the python code in gol.py is easy to follow. The gol main() function sets up the pygame clock and display surface, initialises the grid, and starts the main game loop. The main game loop handles any events from the user, updates the window title to include the generation number, increments the generation number, applies the 'live or die' rules of the game to each cell, redraws the grid and cells, updates the display and adds a tick to the clock.


      The key data structure is the cells, a dictionary of cell co-ordinates each with a boolean value indicating if the cell is alive or dead. For example, cells[(0, 0)] = True would bring the cell in the top left-hand corner of the grid to life; and cells[(3, 10)] = False would kill the cell with an x value of 3 and a y value of 10. The live cells are shown in red, though this is configurable using the LIVECELL_COLOUR tuple.

      The game rules are defined in the live_or_die() function. The function takes the current cells data structure as input, applies the rules of the game and outputs the next generation of cells.

      The initialise_grid() function sets the starting condition (alive or dead) for each cell. The function starts by killing all of the cells, giving a blank canvass for the user's creation (cue the manic laugh) and then runs a loop to set-up the cells in the grid. The initialisation loop handles any events from the user and sets up the grid accordingly. When the user types RETURN the action begins.

      As part of the initialisation the user can type in a keyboard short-cut to set-up well known life patterns. These short-cuts are summarised on the title window. For example type d to load the diehard pattern. The pattern files are stored with an extension of .lif in the project-gol directory. The files are sourced from the LifeWiki pattern catalogue and use the Life 1.06 file format. Why not hack and add your own!

      Pygame

      Pygame is a simple and powerful set of python modules designed for writing games. It takes some getting used to. The pygame.org documentation and tutorials are a good place to start. I also found Al Sweigart's Invent with Python online book and Richard Jones' PyCon 2014 introduction to game programming very helpful.

      Acknowledgements

      The links in the body of this article (and the code) highlight just some of the excellent resources that have helped me along the way.

      No comments:

      Post a Comment