Python Pygame Cheat Sheet
A one-page printable reference for Pygame setup, game loop, drawing, images, sound, collision and common game patterns. Pin it next to your monitor!
A one-page printable reference for Pygame setup, game loop, drawing, images, sound, collision and common game patterns. Pin it next to your monitor!
import pygame, sys pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() font = pygame.font.Font(None, 36) while True: # game loop here clock.tick(60) pygame.display.flip()
while running: # 1. Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: jump() # 2. Update state player_x += dx player_y += dy # 3. Draw screen.fill((30, 30, 46)) pygame.draw.rect(screen, (255,255,255), (player_x, player_y, 32, 32)) clock.tick(60) pygame.display.flip() pygame.quit() sys.exit()
| screen.fill(color) | Clear screen with RGB tuple |
| pygame.draw.rect() | Draw filled/outlined rectangle |
| pygame.draw.circle() | Draw filled/outlined circle |
| pygame.draw.line() | Draw a line between two points |
| pygame.draw.polygon() | Draw a filled/outlined polygon |
| screen.blit(source, dest) | Draw a surface at position |
| pygame.display.flip() | Update the full display |
| pygame.display.update() | Update changed regions only |
img = pygame.image.load("sprite.png").convert_alpha() # resize img = pygame.transform.scale(img, (64, 64)) # flip horizontally / vertically img = pygame.transform.flip(img, True, False) # rotate (degrees counter-clockwise) img = pygame.transform.rotate(img, 45) # get positioning rect rect = img.get_rect(center=(400, 300)) screen.blit(img, rect) pygame.display.flip()
# render text to a surface text_surface = font.render( "Score: " + str(score), True, # antialias (255, 255, 255) # color ) # get text dimensions w, h = font.size("Score: 0") # draw it screen.blit(text_surface, (10, 10))
| pygame.key.get_pressed() | Array of all key states |
| pygame.key.name(key) | Get string name of a key |
| pygame.mouse.get_pos() | Current (x, y) mouse position |
| pygame.mouse.get_pressed() | Tuple of mouse button states |
| K_LEFT / K_RIGHT | Arrow keys |
| K_w / K_a / K_s / K_d | WASD keys |
| K_SPACE / K_RETURN | Space / Enter |
| K_ESCAPE | Escape key |
keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x -= 5
# sound effects (short clips) sfx = pygame.mixer.Sound("jump.wav") sfx.play() sfx.set_volume(0.5) # background music (longer files) pygame.mixer.music.load("bgm.ogg") pygame.mixer.music.play(-1) # loop forever pygame.mixer.music.set_volume(0.3) pygame.mixer.music.stop()
# rect-rect collision if rect_a.colliderect(rect_b): print("Hit!") # point inside rect if rect.collidepoint(mouse_pos): print("Clicked!") # check against list of rects idx = rect.collidelist(rect_list) if idx != -1: # collided with rect_list[idx]
| math.sqrt(n) | Square root |
| math.atan2(dy, dx) | Angle from direction |
| math.degrees(rad) | Convert radians โ degrees |
| math.radians(deg) | Convert degrees โ radians |
| random.randint(a, b) | Random int in [a, b] |
| random.choice(seq) | Pick random item from list |
| abs(n) | Absolute value |
| min(a, b) / max(a, b) | Minimum / Maximum |
import math, random angle = math.atan2(dy, dx) dist = math.sqrt(dx**2 + dy**2) n = random.randint(1, 10)
โโ PLAYER MOVEMENT โโ speed = 5 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x -= speed if keys[pygame.K_RIGHT]: player_x += speed if keys[pygame.K_UP]: player_y -= speed if keys[pygame.K_DOWN]: player_y += speed โโ SPRITE ANIMATION โโ frames = [pygame.image.load(f"frame_{i}.png") for i in range(4)] anim_index = 0 anim_speed = 0.15 anim_index = (anim_index + anim_speed) % len(frames) screen.blit(frames[int(anim_index)], player_pos) โโ AABB COLLISION DETECTION โโ def aabb_collide(a, b): return (a.x < b.x + b.width and a.x + a.width > b.x and a.y < b.y + b.height and a.y + a.height > b.y) โโ SCROLLING BACKGROUND โโ bg_x = 0 while running: bg_x -= 2 if bg_x <= -screen.get_width(): bg_x = 0 screen.blit(bg, (bg_x, 0)) screen.blit(bg, (bg_x + bg.get_width(), 0)) โโ PARTICLE SYSTEM โโ particles = [] def spawn_particles(x, y, count=10): for _ in range(count): particles.append({ "x": x, "y": y, "vx": random.uniform(-3, 3), "vy": random.uniform(-4, 0), "life": 30 }) # update: p["x"] += p["vx"], p["life"] -= 1
Follow one of our free step-by-step workshops in the Workshop hub.
๐ง Browse Workshops โ