Packaging Your Game - Beginning Python Games Development With Pygame (2015)

Beginning Python Games Development With Pygame (2015)

APPENDIX B

image

Packaging Your Game

If you have gone to the effort of writing a game with Pygame, you will likely want to share your masterpiece with others. The simplest way to distribute your game is to bundle your Python code and data as a compressed archive file, such as ZIP, TAR, or GZIP, and upload it to your website or send it via e-mail. The problem with this approach is that Python and any external modules you use must be installed before your game can be played, which makes code distributions suitable only for other Python programmers. To distribute your game to a wider, nontechnical audience, you will need to package your game in a familiar way for your chosen platform(s).

This appendix covers how to package your game into a format that allows nontechnical users to install and play it.

Creating Windows Packages

Installing a game on Windows generally involves double-clicking an EXE file, which launches an installer application. The installer is typically in the form of a wizard with several pages that display the license agreement and ask the user where to copy the game files and what icons to install. A Finish button on the final page begins copying the files and creating icons.

There are two steps required to create a user-friendly installer for your Pygame game on the Windows platform:

1. Turn your main Python file into an executable that runs without Python installed.

2. Use installer builder software to create a single EXE file that contains your game’s files.

Creating the installer makes your game accessible to the widest audience, and is essential if your game is intended to be commercial. You can skip the second step if your intended audience is technical enough to decompress a ZIP file and double-click an EXE file.

Using cx_Freeze

To turn a Python file into an executable file, you can use cx_Freeze, which is itself a Python module. cx_Freeze isn’t part of Python’s standard library, but it can easily be installed with the following command in cmd.exe / bash / terminal:

pip install cx_Freeze

Before creating an executable for your Python code, you need to write setup.py, which contains information about your project and launches cx_Freeze. Let’s create a setup.py (Listing B-1) for the Ant state machine listing in Chapter 7.

Listing B-1. Creating an Executable Python Project (setup.py)

import cx_Freeze

executables = [cx_Freeze.Executable("ants_game.py")]

cx_Freeze.setup(
name="Ant Game",
options={"build_exe": {"packages":["pygame"],
"include_files":["ant.png","leaf.png","spider.png",'gameobjects']}},
executables = executables
)

First we’re importing cx_Freeze, then we specify what we’re going to be creating an executable out of. Next, we set up the build with the parameters of name, options, and executables. Within the options, we include standard packages that we’re using. The gameobjectslibrary that we built won’t work in the packages, so instead we treat it like an included library, and include it with the include_files parameter. In here, you put music, images, and any other files that should come with your program. Then you define executables, which we have already done. Make sure you have the final version of our ants game, called ants_game.py, along with ant.png, leaf.png, and spider.png all in the same directory as the ants_game.py script as well as the setup.py script we’re writing now. You will also need the gameobjectslibrary in there.

It may look simple enough, but freezing packages to executables has been known to cause major headaches! It should not be attempted by the faint of heart. For help with packaging things with specific requirements, or to learn more about how the module works, visit http://cx-freeze.readthedocs.org/en/latest/index.html.

The setup.py script should be run with the following command in cmd.exe / bash / terminal:

python setup.py build

If you get the error about python not being a recognized command, then you can do:

C:/Python34/python setup.py build

This searches for and copies all the files used by the script to a folder called build. Inside build is another folder with some machine information, and then in there you find the ants_game.exe file, which launches the Ant state machine simulation, as well as other files necessary for it to run without first installing Python. If you zip this folder, you can share it with anyone, whether or not they have Python or the various modules installed, and they can run the game, but for a professional touch you should also build an installer.

Building the Installer

For a slightly more professional feel, you can also build an installer, which will give you a single installer for your users to work with. This way, they don’t need to deal with decompressing with some file extractor, and they can just double-click to install your application. To do this, you keep everything the same except when you go to build the file, do:

python setup.py bdist_msi

This creates a dist directory. Inside dist is the installer. Now you can distribute just this installer to your friends, and then they can install it. Keep in mind that if you have a 32-bit version of Windows, this makes a 32-bit installer for the game. 32-or 64-bit Windows can install and run this. If you are operating with a 64-bit version of windows, your distribution will be 64-bit, and will not be runnable by 32-bit machines.

Creating Packages for Linux

Creating packages for Linux is easier than for Windows, because most distributions come with Python installed by default and package managers can download the required version of Python, if it is not present. To create a Linux package, use the distutils module in the Python standard library, which can produce source archives (tarballs) or RPM files. It is also a good idea to include a description of the game’s requirements, just in case the Linux distribution is unable to provide them.

For more information on the distutils module, see the documentation online at https://docs.python.org/3.4/distutils/. You can also use cx_Freeze with Linux, by installing it with

pip install cx_Freeze

Then, you can create a distribution of your game with:

cxfreeze ant_game.py

Creating Packages for the Mac

You can also create Mac disk images with cx_Freeze. You need to install cx_Freeze with

pip install cx_Freeze

See Listing B-1 in the Windows section for creating the setup.py file, as well as information about the required files. The command to convert to a disk image is

python setup.py bdist_dmg