Pygame Cheatsheet

Here is a reference of popular commands needed for Pygame. Be advised that some of the more difficult concepts will be spread out in the various sections of the game code. See The parts of a game for more info. In the examples below, I do my best to indicate what parts of the example code go into what section of the overall game code (initialization, event handling, game updates, and drawing) indicated my all caps comments, E.g., # DRAWING.

Note

It is highly recommended that you create your own cheatsheet for concepts you personally want to remember and written in a way that you will easily understand it.

Drawing Shapes

# DRAWING
# Circle
# pygame.draw.circle(screen, color: Tuple[int, int, int], position: Tuple[int, int], radius: int)
pygame.draw.circle(screen, (0, 0, 255), (100, 100), 30)

Drawing Text

# way up after pygame.init()
pygame.font.init()

## INITIALIZATION
some_font = pygame.font.SysFont('Arial', 50)

# DRAWING (in loop)
some_text = some_font.render('Hello', False, (0, 0, 0))
screen.blit(some_text, (0, 0))

Note

Is is possible to “render” the text in the INITALIZATION section if the text will never change. If you need the text to change, like when displaying a score, then rendering must be done in the loop.

Mouse button down

This code should print out for you the x and y location of the mouse in the terminal.

# add MOUSEBUTTONDOWN to your pygame.locals imports
from pygame.locals import MOUSEBUTTONDOWN

# EVENT HANDLING
# In the game loop's event loop, add to existing event if structure
for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            running = False
    elif event.type == QUIT:
        running = False
    elif event.type == MOUSEBUTTONDOWN:
        mouse_x, mouse_y = event.pos
        print(mouse_x, mouse_y)

Scene switch

# INITIALIZATION
current_screen = "menu"

# inside loop
if current_screen == "menu":
    # do menu screen stuff
elif current_screen == "instructions":
    # do instructions screen stuff
...

Note

Each if section may have its own event handling, game updates and drawing sections.

pygame.Rect objects

There is a lot of built in functionality for pygame.Rect s. Here are a couple useful properties you can use.

#                  x   y   width height
box1 = pygame.Rect(50, 50, 100, 100)
box1.center  # (75, 75)
box1.center = (0, 0)  # move the rect to (0, 0)
box1.left = 100  # moves the rect to (100, 0)
box1.width  # 100
box1.width = 300  # can modify the width

Here are some methods for collision detection.

box1 = pygame.Rect(50, 50, 100, 100)
box2 = pygame.Rect(100, 50, 100, 100)

# collision between rects
box1.colliderect(box2)  # True (they overlap)

# collision with mouse
# assume mouse at (75, 75)
mouse_pos = pygame.mouse.get_pos()
box1.collidepoint(mouse_pos)  # True
box2.collidepoint(mouse_pos)  # False

Dragging

box = pygame.Rect(50, 50, 100, 100)
box_dragging = False

...
# in game loop
    for event in pygame.event.get():
        ...
        elif event.type == MOUSEBUTTONDOWN:  # don't forget to import this
            if box.collidepoint(event.pos):
                box_dragging = True
        elif event.type == MOUSEBUTTONUP:
            box_dragging = False
    ...

    # game updates
    if box_dragging is True:
        mouse_pos = pygame.mouse.get_pos()
        box.center = mouse_pos

    # drawing
    pygame.draw.rect(screen, (0, 0, 0), box)

Importing Images

# load the image OUTSIDE THE GAME LOOP
fish_img = pygame.image.load("fishGreen.png")

# Blit the image to the screen
# Game loop: Drawing section
screen.blit(fish_img, (0, 0))

Transforming Images

# you need an original image
fish_img = pygame.image.load("fishGreen.png")

# applying transforms DOES NOT modify the original image,
# but, returns a new copy of the image.
flipped_img = pygame.transform.flip(fish_img, True, True)  # (image, flip_x?, flip_y?)
scaled_img = pygame.transform.scale(fish_img, (50, 50))  # make it 50x50 pixels
rotated_img = pygame.transform.rotate(fish_img, 45)  # 45 degrees