Pygame Quick Reference

This is a summary of some of the most important modules, classes, methods, and functions in pygame. For more information, se the official documentation.

Index: pygame | display | draw | event | font | image | locals | Surface | time

pygame

The top-level pygame package.

pygame.init()
Initialize the pygame system. Call this function once at the beginning of your program.

pygame.display

pygame.display.set_caption(title)
Set the title of the window containing the display surface.

pygame.display.set_mode(size = (0, 0))
Create and return a display Surface with the given size, which must be a pair of integers representing a width and height. If size is not specified, the surface will be as large as the current screen resolution.

pygame.display.update()
Update the display by copying the display Surface to the physical display. Typically you will call this function once in each iteration of your main event loop.

pygame.draw

This module contains functions that draw simple shapes such as lines, rectangles, and circles.

In all functions below, color is a triple representing an RGB color. Any parameter representing a point is a pair of ints.

pygame.draw.circle(surface, color, center, radius, width = 0)
Draw a circle. If width is 0, the circle will be filled.

pygame.draw.line(surface, color, start_pos, end_pos, width = 1)
Draw a line.
pygame.draw.polygon(surface, color, points, width = 0)
Draw a polygon. points is a sequence of 3 or more (x, y) pairs representing the vertices of the polygon. If width is 0, the polygon will be filled.

pygame.draw.rect(surface, color, rect, width = 0)
Draw a rectangle. rect is a tuple (x, y, width, height), where (x, y) indicates the upper-left corner of the rectangle. If width is 0, the rectangle will be filled.

pygame.event

This module contains classes and functions for working with events and the event queue.

Every event is represented by an EventType object and has a type. Event types are defined in the pygame.locals module. Each type of event has a certain set of attributes. Event types and their attributes include


For KEYDOWN and KEYUP events, the
key attribute is an integer ID representing the key that was pressed. Key constants are defined in pygame.locals, and include
For mouse events, the pos attribute is a pair of integers indicating where the mouse was pressed, released, or moved. The button attribute is an integer indicating a button number, where 1 is the primary mouse button (typically the left button).

pygame.event.EventType
A pygame event. The type attribute is an integer representing a type of event.

pygame.event.get()
Retrieve all pending events from the event queue and return them as a Python sequence.
pygame.event.wait()
Return the next event from the queue, waiting for it if necessary.

pygame.font

pygame.font.Font
A object representing a font with a particular style and size.

Font(filename, size)
Load a font with the given filename, or a default font if filename is None. size is the height of the font in pixels.

render(text, antialias, color)
Return a Surface with the given text rendered on it. If antialias is True, the text will be drawn with antialiasing, i.e. edges will appear smooth. color is a triple representing an RGB color.

pygame.image

pygame.image.load(filename)
Load an image from a file, returning a Surface. pygame can read images in many different formats including BMP, GIF, JPG, PNG and TIF.

pygame.locals

This module contains various constants, including event types described in the pygame.event section above. Typically you will import all names from this module at the top of your program:

from pygame.locals import *

pygame.Surface

A Surface object represents an image with a fixed size and pixel format.

pygame.Surface.blit(source, dest)
Draw a source Surface onto this surface. dest is a pair of coordinates indicating where the upper-left corner of the source should be positioned.

pygame.Surface.fill(color)
Fill a Surface with a solid color. color is a triple representing an RGB color.

pygame.time

pygame.time.Clock
An object that keeps track of time.

Clock()
Create a new Clock object.

tick(framerate)
Wait until the current tick has elapsed. framerate is the number of ticks per second.