Installing and Starting Python
To get started, two programs need to be installed: Python and Pygame. Python is the computer language we will program in, and Pygame is a library of commands that will help make writing games easier.
Windows Installation
If you are working with a computer that already has Python and Pygame set up on it, you can skip this step. But if you want to set up Python and Pygame on your own Windows computer, dont worry. It is very easy.
Run the Python installer downloaded from http://ProgramArcadeGames.com/python-3.4.3.msi
Run the Pygame installer downloaded from http://ProgramArcadeGames.com/pygame-1.9.2a0.win32-py3.4.msi
Once everything has been installed, start Python up by selecting the Integrated Development Environment (IDLE) as shown in the figure.
Starting Python
The files provided above come from the Python download page at http://www.python.org/download/ and the Pygame file originally comes from https://bitbucket.org/pygame/pygame/downloads .
Note
There are many versions of Python and Pygame. It can be complicated to get the correct versions and get them to work together. I recommend using the links on ProgramArcadeGames.com rather than downloading them from the Python and Pygame web sites.
If you must use a different version of Python than what is listed here, find a matching version of Pygame at this website: www.lfd.uci.edu/gohlke/pythonlibs/#pygame .
Mac Installation
The installation for the Mac is a bit involved, but it isnt too bad. Here are the steps.
Open up a terminal window. Click on Finder then Applications and then open Utilities.
Starting a terminal window
Double-click on Terminal.
Starting a terminal window
We can issue commands to the Mac in the old-school style by typing them rather than pointing and clicking. We are going to start by typing in a command you probably dont have yet. This command is gcc . Type this and hit the Enter key. Your Mac will recognize that you dont have this command and offer to install it for you. Go ahead and do this. (If instead it says error: no input files you already have gcc, so go on to the next step.)
Starting a terminal window
Install XQuartz from: http://xquartz.macosforge.org .
Line by line, copy and paste the following items into your terminal window:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ master/install)"
sudo brew doctor
brew update
brew install python3
brew install sdl sdl_image sdl_mixer sdl_ttf portmidi mercurial
If you want support for MP3s and movies, you can try adding smpeg . Ive found support for this to be kind of spotty, so my recommendation is to skip this and use Ogg Vorbis files instead. But if youd like to try, use these commands:
brew install --HEAD https://raw.github.com/Homebrew/homebrew- headonly/master/smpeg.rb
Now you have all the supporting libraries. Lets finally install Pygame. Replace YourName with your account name. If you dont know what your account name is, type ls /Users to see all the user accounts on your computer.
cd /Users/YourName/Downloads
hg clone https://bitbucket.org/pygame/pygame
cd pygame
cd src
pip3 install /Users/YourName/Downloads/pygame
At this point, Pygame and Python should be up and running on your system. Python does not come with a way to edit files, so you will need to download an IDE like Wing IDE ( http://wingware.com/downloads ) or PyCharm ( https://www.jetbrains.com/pycharm/download/ ), or some other editor.
Unix Installation
Unix and Unix-like distributions may come with a Pygame package or the ability to easily get one. If you want to compile from source, this is what Ive used on Linux Mint ( http://www.linuxmint.com/ ):
# Load required packages
sudo apt-get install mercurial libsdl1.2-dev
sudo apt-get install libasound2-doc libglib2.0-doc python3-dev
sudo apt-get install libsdl-ttf2.0-dev libsdl-image1.2-dev
sudo apt-get install libsdl-mixer1.2-dev libportmidi-dev
sudo apt-get install libavformat-dev libswscale-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install libsmpeg-dev
# Use mercurial to clone current code
hg clone https://bitbucket.org/pygame/pygame
# Build and install
cd pygame
sudo python3 setup.py
The biggest risk on UNIX platforms is that your default Python version might be in the 2.x series, and that code wont work with the code examples here in the book. Make sure you have and are using Python 3.x.
Optional Wing IDE
Python comes with an editor and an environment to develop code in. Unfortunately it isnt very good. Here are two issues you might run into when using Pythons default editor:
Issue 1. When working with multiple files it is difficult to keep track of the all the open files. It is easy to forget to save a file before running the program. When this happens the program runs with the old code that was saved rather than the new code. This is very confusing.
Issue 2. If there is an error in a program that does graphics the Python program will crash and hang. Once the program has crashed it is difficult to shut down. The error message that describes why it crashed is often buried and difficult to find. See the following figure.
Python Program Hanging in IDLE
The Wing editor solves issue 1 by using an editor with a tab for each file. It will also prompt to save all files before running a program. A program run under the Wing debugger does not hang as described in issue 2; instead the editor will immediately take the user to the line of code that caused the error. See the following figure.
Python Program Hanging in Wing IDE
Therefore, while it is yet a third thing to install, I recommend using the Wing editor. There is a free version called Wing IDE 101 at wingware.com/downloads/wingide-101/ .