INSTALLATION
To start writing code with Python and Pygame, it’s necessary to install several pieces of software. First is the Python interpreter itself, then we add the Pygame library, and finally we want a decent editor that makes writing the code easier. In this tutorial I’ll be using a lightweight IDE called Geany, but you’re free to choose whichever editor you prefer.
The way to install and setup each of these applications varies depending on which operating system you use, so choose the relevant section below according to your OS.
LINUX
Most Linux distributions come with Python already installed, but if it isn’t then you can search the software center or package manager (depending on your distro) in order to install it. This method can also be used to install Geany.
To get the Pygame library – after Python is installed you can type this command into a terminal:
python -m pip install -U pygame –user
(Sometimes the command needed is ‘python3’ instead of ‘python’)
Alternatively, if you don’t have the Python package manager PIP, then this command will work on Debian based distros including Ubuntu:
sudo apt-get install python3-pygame
Usually installing pygame will also fetch some example programs. You can use these to check the install worked by typing in the following line and it should start an example game (press the escape key to exit.)
python -m pygame.examples.aliens
WINDOWS
Note: We’ll be using the latest version of Python and it no longer supports Windows 7 or earlier.
Visit the Python web page at www.python.org and click on Downloads/Windows for the latest Python 3 release. Look for the recommended file for your operating system under the ‘Files’ section, most Windows users will want the ‘Windows installer (64-bit.)’ Download this file and execute it, to begin the installation. When the installer first starts there’s a checkbox option to Add Python to the path be sure to tick this or else it won’t work properly with the Geany editor (if you forgot to do this, just reinstall again.)
For the path variable to take effect it’s necessary to restart Windows. After the reboot you should now able to install Pygame, by typing the following line into a command prompt (or press the Windows key and R) :
python -m pip install -U pygame –user
To check if it worked type in this line and it should start an example game (press the escape key to exit.)
python -m pygame.examples.aliens
To install the Geany editor, visit the homepage at www.geany.org and select the Download/Releases menu. Most Windows users will want the ‘Windows 64-bit’ exe file.