From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search


Pygame is python library for creating self-contained graphical interfaces with python. It can indeed be for writing games, but has many other possible uses. Pygame allows you to use python to create a window, optionally making it fullscreen, read and respond to input from the keyboard, mouse, or a joystick, draw graphics quickly to the screen, play and mix (fragments of) digital audio files. Pygame could be used to, for instance:

  • make a custom slideshow program,
  • an interactive drawing program,
  • a webcam viewer,
  • a generative teleprompter,
  • or an alternative web browser,
  • or, make a game!

Since you program in python, you have all the functionality provided by (built in) libraries in Python like working with mail servers, reading webpages, or RSS feeds, and of course text processing with RegularExpressions.

Pygame is built on top of SDL, and is cross-platform, available for Linux, Mac, and Windows.

When not to use Pygame

Pygame's support for displaying video is limited (and seems buggy?!). Applications using (live) video might consider something like Python and GStreamer, or perhaps a combination of the two.

Pygame's sound support is basically limited to opening and playing fragments from OGG or WAV format audio files. If you need to do realtime sound sythesis, consider combining with, or using instead something more suited to the task (like Puredata).

Pygame will not solve an existential crisis (and may in certain circumstances only exacerbate the problem).

Sample Code

import pygame
from pygame.locals import *
from sys import exit

screen = pygame.display.set_mode((640, 480), 0, 32)

bg = pygame.Surface(screen.get_size())
bg = bg.convert()
bg.fill((0, 255, 0))

while True:
	for event in pygame.event.get():
		if event.type == QUIT:
		elif event.type == KEYDOWN and event.key == K_ESCAPE:

	screen.blit(bg, (0, 0))

//to create a rectangle

import pygame
from pygame.locals import *
from sys import exit
from random import *

width = 1024
height = 768
screen = pygame.display.set_mode((width, height), FULLSCREEN, 32)

while True:
	for event in pygame.event.get():
		if event.type == QUIT:
		elif event.type == KEYDOWN and event.key == K_ESCAPE:
	for count in range(10):
		random_color = (randint(0,255), randint(0, 255), randint(0,255))
		random_pos = (randint(0,width-1), randint(0, height-1))
		random_size = (width-1-randint(random_pos[0],width-1), height-1-\
			randint (random_pos[1], height-1))
		pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))


Missing examples

  • Basic PyGame framework code:
  • Random Image Slideshow
  • Arduino based drawing

Resources / Examples

  • "Newbie Guide"BR
  • Screencasts!BR

Installing on Gentoo

emerge pygame

<!> On some systems, you may need to set some use flags for libsdl, for the pygame install to work. Edit /etc/portage/package.use to include the line:

media-libs/libsdl X alsa arts opengl oss xv


emerge libsdl
emerge pygame

Installing on Debian/Ubuntu

apt-get install python-pygame

Installing on OS X / Windows

<!> As of April 2008, PyGame 1.8 is available and includes custom installers for Mac and windows. See the website.

Older information

Installing on a G3 iBook running OSX 10.4 (Tiger)

PyGame needs at least Python version 2.4. The version of Python that comes with 10.4/Tiger is 2.3.5, so you need to first install Python 2.4.

Using pre-compiled packages from:

1. Installed "Universal Python 2.4"

At the time of writing this was:

Notes: This package will modify your PATH (on my system via .bash_profile) -- so if you restart a terminal and type python, you will be running the new version of Python. NB: The install doesn't change /usr/bin/python. This is a good thing, as any old python stuff on your system won"t break. For instance, any CGI's will still be using the built-in Python (2.3.5). /usr/bin/python is just a link, so you can always to decide to have it point to any version of Python you want... or maybe make a "python2.4" link in /usr/bin and point it to the new python.

2. Download and install PyGame module (same page as above)

Now when I try to start python, and import pygame I get:

[[ImportError]]: [[PyObjC]] 1.2 or later is required to use pygame on Mac OS X

Ummm, ok...

3. Download and install PyObjC (same page)...

It works!


  • PyCam -- using camera input and the OpenCV library, many interesting (future) possibilities