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.
Topics: colors | rectangles
Modules: pygame | display
| draw | event
| font | image
| time | transform
colors
In Pygame functions, an argument describing a color can be any of
the following:
A string with a color name such as 'red'. Here is a complete
list of named
colors that Pygame supports.
A string in the format '#rrggbb'
, where rr, gg,
and bb are two-digit hex numbers representing red, green, and blue
components.
A tuple (R, G, B), where R, G, and B are integers from 0 to
255 representing red, green, and blue components.
A Color object.
rectangles
In Pygame functions, an argument describing a rectangle can be any
of the following:
A tuple (left, top, width, height).
A tuple ((left, top), (width, height)).
A Rect object.
pygame
The top-level pygame module.
-
- 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 class representing 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. Use
the image.load() function to load a Surface from a file.
-
- s.blit(source,
dest, area = None)
-
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.
-
If area is specified, it is a rectangle representing the
portion of the source Surface to draw.
-
s.fill(color)
-
Fill a Surface with a solid
color.
-
s.get_height()
-
Return the height of a Surface in pixels.
-
s.get_width()
-
Return the width of a Surface in pixels.
-
s.subsurface(rect)
-
Given a rectangle representing part of the area of this surface,
return a subsurface that references the pixels in that area. The
original surface and the subsurface share the pixels: a change in
one will be reflected in the other.
pygame.display
-
- display.flip()
-
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.
-
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.
pygame.draw
This module contains functions that draw simple shapes such as
lines, rectangles, and circles.
In all functions below, 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. 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.
Each type of event has a
certain set of attributes. Event types (which are defined in the
pygame module) and their attributes include
KEYDOWN
(key) – the user has pressed a key
KEYUP
(key) – the user has released a key
MOUSEBUTTONDOWN
(pos, button) – the user has pressed a mouse button
MOUSEBUTTONUP
(pos, button) – the user has released a mouse button
MOUSEMOTION
(pos) – the user has moved the mouse
QUIT – the user
has closed the window
For
KEYDOWN and KEYUP events, the key
attribute is an integer ID
representing the key that was pressed. Key constants are defined in
the pygame module, and include
- K_SPACE
- space
-
K_UP, K_DOWN, K_LEFT,
K_RIGHT – the arrow keys
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 the empty
string 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.
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.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.scale2x(image)
-
Return a copy of an image scaled up by a factor of 2, using a
scaling algorithm that works well for low-resolution pixellated
images.
-
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.