PyGame Tutorial 1 - Setting up PyGame
The first thing you would want to do to get started with PyGame is, of course, initializing it and creating a window. It’s pretty simple.
Create File
First create a new text file and name it “tut_01.py". Next open this file in a text editor (preferably one with source code highlighting.) Now we are ready to start programming!
Shebang
I’m going to begin my Python program with a simple little comment. You don’t have to worry too much about how it works. Because it is a comment the line is ignored by Windows, but it is used by Linux to know how this file should be run. The line looks like this:
#!/usr/bin/env python
Import
Now we will import the modules we need. For this first tutorial all we need is the PyGame module. This line will look like this:
import pygame
Initialize
Before PyGame can be used it should be initialized. This tells python to prepare to be used. This is simply done by calling the init method in the pygame module:
pygame.init()
Create the Display Window
All of the game’s action will take place in a display window. This is where the graphics will be drawn to. Creating a display window is easy -- just call the pygame.display.set_mode method with a tuple specifying the width and the height of the display area, like this:
pygame.display.set_mode((480, 360))
This will create a window that is 480 pixels wide and 360 pixels tall. You can specify any resolution you want as long as it will fit on the screen. Higher resolution screens need to draw more pixels so they can be a little slow. This set_mode method has a few other options, but for now we’ll just be using the defaults.
Main Game Loop
The game will be updated several times each second. Each time the game is updated we will be moving things around just a little bit. This will make it look like objects in the game are moving smoothly around the screen.
At the end of this tutorial we will simply have a blank window with nothing in it. This is just the starting point of making our game.
The main loop will begin by creating a variable that is used to check if we should keep cycling through this loop updating our game:
keep_going = True
Next we will start the loop. We will by using Python’s “while” keyword. What while does is it loops through what’s in the indented block of code as long as a specified condition is true. For example, in our program the while statement will look like this:
while keep_going:
As long as the variable keep_going is True this part of the program will keep looping and updating. When keep_going is set to False the program will stop looping and continue after the indented block of code.
Processing Events
Events are little messages sent to the program. We will be looping through these events and responding to them. We start this loop with Python’s “for” and “in" keywords. What for does is it steps through each item in a list and uses this current item in the indented block of code. Then it goes to the next item in the list and uses that. Remember this will be inside the previous while loop, so it needs to start at the proper indentation level. We’ll start the for loop like this:
for event in pygame.event.get():
This will go through each item from the method pygame.event.get() and put the current item in the variable called “event”.
Now we will use this event variable. PyGame events are objects with properties. The property we are interested in right now is called “type”. There are different types of events, but for now we are only going to check for the type that is passed to the program when the close button is pressed at the corner of the Window. We will check the event type with the “if" keyword. If the conditional statement is True it will do what’s in the indentation block, but if it is False it will skip past it. Remember we are in both the main loop and this event loop, so we need two indentations.
if event.type == pygame.QUIT:
keep_going = False
This will set the variable keep_going to False if the quit event was passed to the program. Remember the main loop will only keep looping if keep_going is True, so if it is False the main loop will end and the program will do what’s after the end of the loop. The while loop checks this variable each time the program returns to the while statement. Think of the program as stepping through one instruction at a time.
Flipping the Display
The action takes place on what is called a back-buffer. The graphics are drawn there so that you will not see any flickering from graphics being drawn layer on top of each other. After the graphics are drawn to the back buffer we will want to flip the display so that the back-buffer is now shown on the screen and there is a new back-buffer to draw onto. This is simply done by calling pygame.display.flip(). Remember, we are now outside of the event loop, but still in the main loop -- we want this to be done once each time the game is updated. The line will look like this:
pygame.display.flip()
The End of the Program
We will now handle what happens when the main loop is done looping. We will be calling the quit method in the pygame module. This will tell PyGame that we are done and it will close the window and shutdown PyGame. We are outside the while loop, so there will be no indentation.
pygame.quit()
There We Have It
We now have the basic framework for writing a game with PyGame. The complete code will look like this:
#!/usr/bin/env python
import pygame
pygame.init()
display = pygame.display.set_mode((480, 360))
keep_going = True
while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
pygame.display.flip()
pygame.quit()



