Installing pygame

pygame is installed a little differently on each operating system. The next section of this chapter contains the instructions to install pygame on Windows, Mac, Linux, and Raspberry Pi systems. You can skip to the section that has instructions on how to install pygame on your operating system, and if you are not 100% sure of what you are doing, go ahead and get some help for this section. Remember that you will need an Internet connection to install pygame, and some parts of the installation may take time.

Installing pygame – Windows

To install pygame on Windows, you will need to go to http://www.pygame.org/hifi.html. If you do not know where the Windows version of pygame is, type download in the search bar and go to the Downloads page. You should see a screen with this information:

Installing pygame – Windows

At the bottom of that screenshot, you will find the instructions for most Windows computers. Follow these instructions:

  1. Visit the pygame website.
  2. Download this version of pygame: pygame-1.9.2a0.win32-py2.7.msi.
  3. Go to your Downloads folder.
  4. Double-click on the pygame-1.9.2a0.win32-py2.7.msi file.
  5. Choose Run:
    Installing pygame – Windows
  6. Choose install Python from registry option:
    Installing pygame – Windows
  7. Allow the installation to complete.

Finally, everything should be in place. To test whether the installation worked, open your Python shell, and type this:

import pygame

If you have no error message, then your installation worked! Congratulations! If it did not work, review your steps, and don't be afraid to ask for some help.

Installing pygame – Mac

We need to do some preparatory steps to make pygame work before we actually install it on Mac. pygame requires a few dependencies or other programs to work on a Mac:

  • Xcode (free, available on the App Store)
  • XQuartz (free, open source)
  • Homebrew (free, open source)
  • An Internet connection

You may also want to get an adult to help you with the installation, especially if you are not 100% comfortable with the terminal. There are some brief instructions on the pygame Mac Wiki, located at http://pygame.org/wiki/macintosh.

Installing Xcode

To start, open your terminal. Go to the directory where you first installed Python (refer to Chapter 1, Welcome! Let's Get Started, if you have forgotten how to get to your home directory). Once you are in your Python directory, you will install Xcode. Xcode is a developer tool that has a lot of power, far beyond what we will do in this book. If you are curious about Xcode, you can find the documentation at https://developer.apple.com/xcode/.

For now, we will install Xcode by typing this into the terminal/command prompt:

xcode-select --install

If your computer already has Xcode installed, you will get an error message that says it is already installed. If not, then Xcode will begin installing. Once Xcode is installed, you can move on to the next step. Be patient, as Xcode takes some time to install. To test whether the install worked, try entering the install command again. You will see that it is already installed:

Installing Xcode

Installing Homebrew

The next step is installing a package management system called Homebrew. This sounds complicated, but all it means is that you are going to be able to get cool stuff much more easily. Python has something called pip, and this installs Python packages. We are going to install another system called homebrew. Homebrew is used to manage many packages of different kinds, and it can also be used to troubleshoot.

Here is how to install homebrew using the curl command:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install

The preceding code should be typed in one command. The text will wrap on its own if it needs more room in your terminal. Once you type that command, the homebrew installation will begin. Homebrew asks you questions and gives good installation suggestions along every step, so pay attention and it should work well. For more information, you can go to the homebrew website for instructions:

brew.sh

Installing programs with homebrew

Once homebrew is installed, you can use it to install the rest of the dependencies needed to install pygame. We need to have access to Mercurial and Git. Both of these are version control systems, so every time code is changed, they keep track:

  brew install mercurial
  brew install git
  brew install sdl sdl_image sdl_mixer sdl_ttf portmidi

These packages will take a few minutes to install, and that's okay. Once they have completed installing, then you will finally be able to install pygame. The command to install pygame uses something called sudo at the beginning, and you will need to know your computer's administrative password to use it.

Tip

If you do not know the password to your computer, find a person who does.

sudo pip3 install hg+ http://bitbucket.org/pygame/pygame

Once this is installed, you should be ready to use pygame. Before we go any further, let's test it out. Open a Python shell, and in the Python shell, type the following:

import pygame

If you notice import error: no module named pygame, on your screen, then something has gone wrong with your installation. Check your installation, and don't be afraid to ask for some help if you need to. If nothing happens when you hit Enter, then the installation of pygame is correct!

Installing pygame – Ubuntu Linux

These installation instructions are for the newest version of Ubuntu Linux at the time of writing this, which is version 15.04. First, you will want to install the pip package manager, if it is not installed already:

sudo apt-get install python-pip

You will notice that sudo is used again, and this means that you will need the administrative password for your computer. Next, we will use apt-get to install pygame:

sudo apt-get install python-pygame

Now, to test and check whether pygame is installed correctly, you will open a Python shell and type this command:

import pygame

If there is an error message, it means that something about your installation is not correct. Reread the installation instructions and try again. Don't be afraid to ask for help if you need to. If you have an empty line following the import pygame, it means that everything is working and you are ready to move on to the next section!

Installing pygame – Raspberry Pi

If you are working with Raspberry Pi and using one of the operating systems for the Pi, you are all set! Python and pygame are preinstalled on these systems. You can learn the basic pygame functions and modules by reading the rest of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset