Pygame
2.6 Quick Reference
This is a summary of some of the most useful modules,
classes, methods, and functions in pygame.
For more information, see the official
documentation.
Topics: colors | keys
| rectangles
Modules: pygame | display
| draw | event
| font | image
| key | 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.
keys
Key constants are defined in the pygame module, and include
K_a, ..., K_z: letters
K_SPACE: space
K_UP, K_DOWN, K_LEFT, K_RIGHT: arrow keys
K_LCTRL, K_RCTRL: control keys
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. All attributes listed here are
read/write.
-
- Rect(left, top, width, height)
-
Create a Rect with the given upper-left corner and size.
-
r.bottom
-
The y-coordinate of the bottom of the rectangle.
-
r.center
-
A tuple (x, y) containing the center point of the rectangle.
-
r.left
-
The x-coordinate of the left side of the rectangle.
-
r.right
-
The x-coordinate of the right side of the rectangle.
-
r.top
-
The y-coordinate of the top of the rectangle.
-
r.collidepoint(x, y)
r.collidepoint((x, y))
r.collidepoint(vec) -
Return true if the given point or Vector2 is inside this rectangle.
-
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.get_rect()
-
Return a rectangle covering the entire surface.
-
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.
class Vector2
A two-dimensional vector. Numerical operations (e.g. +, -, *, /)
will work on Vector2 objects. vec * vec will compute the dot product
of two vectors.
-
- v.x
-
The x component.
-
v.y
-
The y component.
-
Vector2.from_polar((r, phi))
-
Create a Vector2 from the polar coordinates (r, phi), where r is the
distance from the origin and phi is an angle in degrees.
-
v.rotate(angle)
-
Return a Vector2 that has the same length as self and is rotated
clockwise by the given angle in degrees.
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 or
floats, or a Vector2 object.
-
- draw.circle(surface, color, center, radius, width = 0)
-
Draw a circle. If width is 0, the circle will be filled.
-
draw.ellipse(surface, color, rect, width = 0)
-
Draw an ellipse, given a rectangle representing its bounding box. If
width is 0, the ellipse 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 a
key constant
representing the key that was pressed.
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.key
-
- key.get_pressed()
-
Return a sequence of booleans representing whether each key is
currently pressed. You can use key constants as indexes into this
sequence. For example,
key.get_pressed()[K_a]
is true
if the A key is currently held down.
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.