Pygame 2.0 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 | time

pygame

The top-level pygame package.

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

class Color

A class representing a color.

Color(r, g, b)
Create a Color from red, green and blue components, each in the range from 0 to 255.
Color(name)
Create a Color from a color name such as 'red'.

class Rect

A rectangle.

Rect(left, top, width, height)
Create a Rect with the given upper-left corner and size.
r.colliderect(rect)
Return True if two rectangles overlap, otherwise false.

class Surface

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

s.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.
s.fill(color)
Fill a Surface with a solid color. color is a Color object or a triple representing an RGB color.
s.get_height()
Return the height of a Surface in pixels.
s.get_width()
Return the width of a Surface in pixels.

pygame.display

display.set_caption(title)
Set the title of the window containing the display surface.
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.
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 Color object or a triple representing an RGB color. Any parameter representing a point is a pair of ints.

draw.circle(surface, color, center, radius, width = 0)
Draw a circle. If width is 0, the circle will be filled.
draw.line(surface, color, start_pos, end_pos, width = 1)
Draw a line.
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.
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).

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

class EventType

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

pygame.font

class 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.
SysFont(name, size, bold = False, italic = False)
Load a font with the given name and attributes. 'name' can either be a specific font name such as 'times' or 'arial', or a generic name such as 'monospace', 'sans' or 'serif'. If 'name' is None or no font with the given name can be found, pygame loads the default pygame font instead.
The font name can also be a list of font names to try in order.
f.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 Color object or a triple representing an RGB color.

pygame.image

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.time

class Clock

An object that keeps track of time.

Clock()
Create a new Clock object.


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

pygame.transform

transform.smoothscale(image, new_size)
Return a copy of an image scaled to a new size, expressed as a pair of ints containing a width and height in pixels.