Getting started with pygame
Pygame is a Python wrapper for SDL – a cross-platform C library for controlling multimedia –, written by Pete Shinners. This means, using pygame, you can write video games or other multimedia applications in Python that will run unaltered on any of SDL’s supported platforms (Windows, Unix, Mac, beOS and others).
This section provides an overview of what pygame is, and why a developer might want to use it.
It should also mention any large subjects within pygame, and link out to the related topics. Since the Documentation for pygame is new, you may need to create initial versions of those related topics.
|Version| ==============> |Release Date|
|Pygame 1.9.0| ========> |August 1, 2009|
|Pygame 1.8.1| ========>|July 30, 2008|
|Pygame 1.8.0| ========>|March 29, 2008|
|Pygame 1.7.1| ========>|August 16, 2005|
|Pygame 1.6.2| ========>|-|
|Pygame 1.6| =========> |October 23, 2003|
|Pygame 1.5| =========> |May 30, 2002|
|Pygame 1.4| =========> |Jan 30, 2002|
|Pygame 1.3| =========> |Dec 19, 2001|
|Pygame 1.2| =========> |Sep 4, 2001|
|Pygame 1.1| =========> |Jun 23, 2001|
|Pygame 1.0| =========> |Apr 5, 2001|
|Pygame 0.9| =========> |Feb 13, 2001|
|Pygame 0.5| =========> |Jan 6 14, 2001|
|Pygame 0.4| =========> |Dec 14, 2000|
|Pygame 0.3| =========> |Nov 20, 2000|
|Pygame 0.2| =========> |Nov 3, 2000|
|Pygame 0.1| =========> |Oct 28, 2000|
A simple 'game'
Import and initialize
Every module needs to be imported and pygame is no exception. Although we need to call the function
pygame.init() for all imported modules in pygame to be initialized properly. If we forget this some modules won't work. The function also returns a tuple of all successfully and failed initializations (it won't raise an error if a module fails to initialize).
We also need to create a display. Pygame have already created a (hidden) display, so all we need to do is to set the mode of the display (in this example we only set the resolution). It's also a good idea to create a clock to make sure our program updates at a fixed speed (otherwise it would run at different speed depending on how fast the computer is).
For a bit of readability later in our code we'll create two color constants, which represent a tuple of Red, Green and Blue (RGB). The values goes from 0 (no light) to 255 (full light).
In pygame we usually use a Surface to represent the appearance of an object, and a Rect (rectangle) to represent the position of an object. A Surface is like a blank sheet of paper which contain colors or images. If you're making a class you should name the attributes image and rect since many functions will look for and use those attributes. Such classes would benefit by inherit the
pygame.sprite.Sprite class for reasons you can read up on here.
The game loop
Now we have everything set for our game loop. This is a loop that will run for the entire game, where we handle events and updates the screen and positions of our objects.
First we'll make sure our loop executes at a given FPS. We defined the FPS and created our clock in the beginning of the program. The following code will make sure our program sleeps enough time to make our loop repeat the amount we defined our FPS to be. In this example, 60 times per second.
Then we'll handle events. An event is basically a user action, such as moving the mouse or pressing a key. Pygame will register all these events in a queue which we get by calling
pygame.event.get(). We can iterate over this and check if there's an event that we'd like to handle. Events have a type attribute which we can check against constants in the pygame module to determine what type of event it is.
We can also check
if event.type == pygame.KEYDOWN to see if the user has pressed a key down. In that case the event has an attribute key which we can check to see which key it represents.
Now we need to display our image. First we might want to clear our screen from previous rendering. We do so by filling our entire screen with black (remove the code to see why we want to clear it). Then we need to blit our image to the screen. Blitting essentially means copying the image to another surface (in our case, the screen). Lastly we flip or update the screen.
When we're blitting we're not actually displaying anything to the user. Imagine it as the computer on one side and the user on the other. The computer draws (blits) on it's side of the screen, flips it towards the user, and then repeats.
Now we have a basic game! Quite boring, yes, but the essentials are there! Combined this with your current Python knowledge and you should be able to create something awesome.
Slightly improved game mechanics
Note that the program checks for when we press the key and not for when we're holding the key down. To fix this we could introduce a velocity variable. We can create a player class to keep it more organized. To avoid frame dependent movement (if we would change the FPS to 30 the objects would move at half the speed) we introduce time dependent movement by passing the time between ticks to our movable objects.
Importing pygame and drawing on a display
You must do the following to get started with Pygame:
This opens a window of size 640,480, and stores it in a variable called screen.
Setting up a window name
Setting up a name for the pygame window requires the following syntax:
About the screen
- The point (0,0) is at the upper left hand corner of the screen.
- x coordinates increase from left to right, y coordinates increase from top to bottom.That is the right side coordinates on the Cartesian plane is positive and left side is negative.However , the upper side coordinates on the Cartesian plane is negative on top and positive on bottom.(Note: This is considered if the points are taken from the origin.)
Updating the Screen
Changes you make to the screen—e.g. filling it with color, or drawing on it, do not show up immediately!
So how to do it?
You have to call this function:
The coloring in pygame works on RGB mode.
The code for coloring is:
- R stands for red.
- G stands for green
- B stands for blue.
- All three should be integers between 0 and 255, with 255 being brightest, and 0 being darkest
To draw lines
To draw rectangle
To draw circle
Setting everything into a loop
To make a loop use the following code:
Drawing a rectangle on pygame window (code)
Download the appropriate pygame
.whlfile according to your installed python version. (The file is named something like
<pygame version> - <python version>
inside your terminal, bash or consol.
pipis not found in
PATHtry to run
python -m pip install your-pygame-package.whl
Check if you can import pygame as a python module
If you do not get an error, you have correctly installed pygame on your computer :)
Open your terminal and run
Note: This will install pygame for python2
Try to import pygame inside
If you do not get an error, you have correctly installed pygame on your linux system :)
There are two ways to install it on mac:
Go to the Pygame downloads page and download the mac installer. Run it, and it should install Pygame on your Mac.
Then use Homebrew to install Python 2.7.12 and Pygame:
Now run Python in your terminal and try
import pygame. If it does not say anything, it's installed successfully.