Setting Up the PyGame Library
The PyGame library is a package. You learned in Hour 13, “Working with Modules,” that a group of
modules can be put together in a package for use in Python scripts. The PyGame package is a
collection of modules and objects that will help you create games using Python.
Did You Know: Preinstalled PyGame Games
You’ll find several Python game scripts and their supporting files in the
/home/pi/python_games directory. You can play these games if you have the
PyGame library installed. These scripts make great learning tools, too.
PyGame for Python is really only for developing simple games, not graphical wonders. (You need
Panda3D for that kind of fancy stuff.) The real glory of PyGame is that it will help you learn more
script-writing principles while providing instantaneous positive feedback to you as a script writer.
Checking for PyGame
The PyGame package is typically not installed by default. To check your system, enter the Python
interactive shell and try importing PyGame, as shown in Listing 19.1.
LISTING 19.1 Checking for PyGame
Click here to view code image
>>>
>>> import pygame
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygame
>>>
If you receive an ImportError message as shown in Listing 19.1, then the PyGame package is not
installed. However, if you do not receive an error, you do have it and can skip the next section, on
installing PyGame.
Installing PyGame
Typically, to install a Python package, you use a variation of the command sudo apt-get
install. Unfortunately, at the time of this writing, the PyGame library is not easy to install for
Python3 on the Raspberry Pi. The steps in this section guide you through the process. You must follow
them very carefully and in the correct order if you want to successfully install PyGame.
By the Way: Checking for a Change
Hopefully, by this time there is an easier way to install the PyGame library on your
Raspberry Pi. Check elinux.org/RPi_Debian_Python3#PyGame_Module, in the
“PyGame Module” section, for potential updates.
Making Sure Your System Is Up to Date