Friday 23 January 2015

cx_Freeze and Pygame

I've just recently finished work on a game project for University as part of my Software Engineering module. The game is written in Python, using Pygame as a nice wrapper around SDL. Using Python and Pygame was a great choice, but distributing the project proved to be difficult. One of the requirements was that the project must run on the University computers, which have an old version of Python 2 and don't allow for the installation of Pygame or any other libraries.

In order to get around this, I remembered using a project called cx_Freeze. cx_Freeze allows you to distribute a Python program as an executable - removing the need for Python and libraries that your program requires. You can run cx_Freeze from the command line or by creating a distutils setup script. I looked around to find a suitable script for use with pygame, all of the scripts I tried resulted in the error:

pygame.error: File is not a Windows BMP file

This is because Pygame doesn't support loading PNG files without the use of SDL_image. With this in mind, it's important to tell cx_Freeze where your libSDL installation is. You can do this by assigning a path to bin_paths_include. I created my binaries using the following script:

To build the binaries, the script should be run as follows:

python2.7 cx_build.py build

This will create a new directory called build - inside of which will be another directory containing the created binary and required libraries.