โœฆ JVDesignStudio ยท No Sign-Up Required

Python & Pygame Cheat Sheet

A one-page printable reference for variables, Pygame setup, drawing, event handling, collision detection and the most-used patterns for building games with Python. Pin it next to your monitor!

๐Ÿ“ฆ Variables & Types

health = 100            # int
name = "Pip"             # str
speed = 4.5              # float
is_alive = True          # bool

msg = f"Score: {score}"  # f-string
print(type(health))

Python is dynamically typed no need to declare a type, it's inferred from the value.

๐ŸŽฎ Pygame Setup

import pygame
pygame.init()

screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

๐Ÿ–Œ๏ธ Drawing

screen.fill((20, 20, 30))

pygame.draw.rect(screen, (75,139,190), (x, y, 32, 32))
pygame.draw.circle(screen, (247,201,72), (x, y), 16)

player_img = pygame.image.load("player.png")
screen.blit(player_img, (x, y))

๐Ÿ•น๏ธ Event Handling

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            player.jump()

keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
    player.x += speed

๐Ÿ“ Collision Detection

player_rect = pygame.Rect(x, y, 32, 32)
enemy_rect = pygame.Rect(ex, ey, 32, 32)

if player_rect.colliderect(enemy_rect):
    player.hp -= 10

# Sprite groups
hits = pygame.sprite.spritecollide(player, enemy_group, False)

Rect.colliderect() checks rectangle overlap; sprite groups handle many-vs-many collisions easily.

๐Ÿ” Loops & Conditionals

for enemy in enemies:
    enemy.update()

while ammo > 0:
    ammo -= 1

if health <= 0:
    game_over()
elif health < 20:
    print("Low HP!")
else:
    pass

alive = [e for e in enemies if e.hp > 0]

๐Ÿ—๏ธ Functions & Classes

class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.hp = 100

    def update(self, dt):
        self.x += self.vx * dt

    def draw(self, screen):
        pygame.draw.rect(screen, (75,139,190), (self.x, self.y, 32, 32))

๐ŸŽฏ Common Patterns

# A complete minimal Pygame game loop
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

player = pygame.Rect(50, 50, 32, 32)
speed = 200
running = True

while running:
    dt = clock.tick(60) / 1000

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        player.x += speed * dt
    if keys[pygame.K_LEFT]:
        player.x -= speed * dt

    screen.fill((20, 20, 30))
    pygame.draw.rect(screen, (75,139,190), player)
    pygame.display.flip()

pygame.quit()

Every Pygame loop follows the same shape: handle events, read input, update positions with deltaTime, then fill + draw + flip.

Want to build a whole game?

Follow one of our free step-by-step workshops in the Workshop hub.

๐Ÿ”ง Browse Workshops โ†’