📝 Worksheet← Lesson← Course
BitWithBite
Pygame Mastery · Quick Reference

Collision Detection Cheat Sheet

Pygame Mastery
In one line: Rect, circle and mask collision, plus hitboxes/hurtboxes and trigger zones for real game hit detection.

Key Ideas

1Rect Collision. rect.colliderect(other_rect) — fast, checks two bounding boxes for overlap. The default choice for most objects.
2Circle Collision. Compare the distance between centers to the sum of radii — ideal for round objects like balls or coins.
3Mask Collision. pygame.sprite.collide_mask checks pixel-perfect overlap — slower, use only when rect collision is too imprecise.
4Hitbox vs Hurtbox. A hitbox deals damage (the attack); a hurtbox receives damage (the vulnerable area) — often smaller than the sprite image.
5Trigger Zones. A Rect with no physical collision response — just detects overlap to fire an event (checkpoint, level transition).
6Optimization. Only check nearby objects (spatial partitioning/broad phase) instead of every pair every frame for large object counts.

Collision Snippets

if player.rect.colliderect(enemy.rect):
  take_damage()
dist = (a.pos - b.pos).length()
if dist < a.radius + b.radius:
  collided = True
pygame.sprite.spritecollide(
  player, enemy_group, False)
colliderect()distdist < r1+r2
Rects test bounding-box overlap; circles compare center distance to the sum of radii.