Part 1 Setting up Python and PyGame

Downloading the PyGame files and installing

PyGame Website

The PyGame website has the downloads for PyGame, as well as a tutorial on getting tarted, documentation for PyGame, and even a directory of games made with PyGame. Once you create a game, you can put your game on the directory, too!

The PyGame website is here: http://pygame.org/news


Windows

For Windows, you will download a version of PyGame from here: http://pygame.org/download.shtml

Look for the “Windows” header and download the version of PyGame that contains “py2.7″ in the title. At the time of writing, the latest version is pygame-1.9.1.win32-py2.7.msi

Note: Version 2 and Version 3 of Python have a lot of differences and are not always compatible between each other. This version of PyGame uses Python 2.7.


Linux

The Synaptic package manager, with

The Synaptic package manager, with “python-pygame” highlighted.

If you’re using Ubuntu, Linux Mint, or Debian, you should be able to install Python via the Synaptic Package Manager, or even the Software Manager.

Search for “pygame”, and you should download the python-pygame package. It should also install the Python dependencies at the same time.


Mac

Download PyGame from here: http://pygame.org/download.shtml Look for the “Macintosh” header and download the version for py2.7.

I don’t have a Mac right now so I can’t help out much here.


Downloading a text editor

Source code is all text, so you will need a decent text editor to write with. The Windows default Notepad is a terrible program, so you should download something else.

geany Geany

http://www.geany.org/Download/Releases

Geany is a free, cross-platform editor. You can install additional plugins into Geany to customize it.


Making sure everything installed properly

We should build an example program to make sure that everything is working properly.

First, create a directory on your computer for your Python projects, and create a folder for Part 1. We will start with a simple Python program, and then a PyGame program.

Python test

A Python program that says

A Python program that says “Hello, world!” and the numbers 1 through 9.

In Geany, create a new file, and save it to this directory as “simple_python.py“.

Add the following code in it:


print( "Hello, world!" )

for i in range( 1, 10 ):
    print( i )

In Geany (Linux?) you can run the Python program by hitting F5.

You can also run a Python program from the command-line if Geany isn’t set up to run with F5.

Opening the command-line in Windows

(TODO)

Opening the command-line in Linux

In many desktop environments, you can right-click within the folder you’re in to open up the terminal in that directory.

The right-click context menu is open, and

The right-click context menu is open, and “Open in Terminal” is highlighted.

Running from the command-line

From the terminal, you will type:

python simple_python.py

then hit ENTER and the program will run.

PyGame test

Next we will create a simple PyGame test to make sure we can get a window open. Create another source file named “simple_pygame.py“.

Paste the following code in:


import pygame, sys
from pygame.locals import *

# Global variables
screenWidth = 300
screenHeight = 300
timer = None
window = None
fps = 30
bgColor = pygame.Color( 50, 200, 255 )

# Function definitions
def InitPygame( screenWidth, screenHeight ):
    global window
    global timer
    pygame.init()
    timer = pygame.time.Clock()
    window = pygame.display.set_mode( ( screenWidth, screenHeight ) )
    pygame.display.set_caption( "Testing out PyGame!" )

# Program start
InitPygame( screenWidth, screenHeight )

# Game loop
while True:
    window.fill( bgColor )

    # Check input
    for event in pygame.event.get():
        if ( event.type == QUIT ):
            pygame.quit()
            sys.exit()

    # Update screen
    pygame.display.update()
    timer.tick( fps )

Don’t worry if it doesn’t make sense yet; we will go over this code next time.

Once run, it should pop up a little blue window, with the terminal window in the background.

A small window filled with light blue. Its title bar says

A small window filled with light blue. Its title bar says “Testing out PyGame!”

You can also download these sample programs from this lesson’s GitHub, under the Lessons folder:

https://github.com/Rachels-Courses/Intro-to-PyGame


| Next: Introduction, Class files, and Glossary >>

Back to main page


Download this lesson’s source code at https://github.com/Rachels-Courses/Intro-to-PyGame

 

Print Friendly