A collision occurs when two objects overlap in space. Detecting it is essential for: taking damage, picking up items, blocking movement, shooting, and more.
Pygame offers several levels of precision, with a clear trade-off:
Faster ←──────────────────────────────→ More Precise
Rect-Rect Circle-Circle Pixel-perfect
(AABB) (distance) (mask)
Rect-Rect Collision (AABB)
AABB = Axis-Aligned Bounding Box. The fastest and most commonly used method.
# Method 1: rect.colliderect()if player.rect.colliderect(enemy.rect):print("Collision!")# Method 2: rect.collidepoint() — point inside rectif player.rect.collidepoint(pygame.mouse.get_pos()):print("Mouse is over the player")# Method 3: rect.collidelist() — against a list of rectsindex = player.rect.collidelist([rect1, rect2, rect3])# Returns the index of the first collided rect, or -1
Rect-Rect Collision (AABB)
AABB = Axis-Aligned Bounding Box. The fastest and most commonly used method.
# Method 1: rect.colliderect()if player.rect.colliderect(enemy.rect):print("Collision!")# Method 2: rect.collidepoint() — point inside rectif player.rect.collidepoint(pygame.mouse.get_pos()):print("Mouse is over the player")# Method 3: rect.collidelist() — against a list of rectsindex = player.rect.collidelist([rect1, rect2, rect3])# Returns the index of the first collided rect, or -1
Manual AABB Implementation
AABB Collision — tests:
=======================================================
Partially overlapping → ✅ COLLISION
Separated horizontally → ❌ no collision
B fully inside A → ✅ COLLISION
Just touching (edge) → ✅ COLLISION
Separated by 1px → ❌ no collision
Circle-Circle Collision
More precise for round objects (bullets, coins, circular characters). The logic: two circles collide if the distance between their centers is less than the sum of their radii.
A B
(●) (●)
←── d ──→
If d < rA + rB → collision
Circle-Circle Collision
More precise for round objects (bullets, coins, circular characters). The logic: two circles collide if the distance between their centers is less than the sum of their radii.
This is the most powerful collision function in Pygame.
# Detect collision of ONE sprite against a GROUPhits = pygame.sprite.spritecollide( player, # individual sprite bullets, # group to check againstTrue# True = remove colliding sprites from the group)# hits is a list of sprites that collidedif hits: health -=len(hits)# Detect collisions between TWO GROUPSclashes = pygame.sprite.groupcollide( bullets, # group 1 enemies, # group 2True, # remove from group 1True# remove from group 2)# clashes is a dict: {bullet: [enemies_hit], ...}# Using circular collision (more precise)hits = pygame.sprite.spritecollide( player, bullets, True, collided=pygame.sprite.collide_circle)# Using pixel masks (maximum precision, slowest)hits = pygame.sprite.spritecollide( player, bullets, True, collided=pygame.sprite.collide_mask)
Collision Response
Detecting the collision is only half the job. The other half is what to do when it happens.
Collision Response
Detecting the collision is only half the job. The other half is what to do when it happens.
Pattern 1 — Separation (platforms, walls)
# Move first, then correctplayer.rect.x += vel_xfor platform in platforms:if player.rect.colliderect(platform.rect):if vel_x >0: # Moving right player.rect.right = platform.rect.leftelif vel_x <0: # Moving left player.rect.left = platform.rect.right vel_x =0
Collision Response
Detecting the collision is only half the job. The other half is what to do when it happens.
Pattern 1 — Separation (platforms, walls)
# Move first, then correctplayer.rect.x += vel_xfor platform in platforms:if player.rect.colliderect(platform.rect):if vel_x >0: # Moving right player.rect.right = platform.rect.leftelif vel_x <0: # Moving left player.rect.left = platform.rect.right vel_x =0
Collision method comparison
============================================================
Method Cost Precision When to use
--------------------------------------------------------------------------------
AABB (Rect-Rect) O(1) Low — invisible box Platforms, items, most objects
Circle-Circle O(1) Medium — circular Bullets, coins, round characters
collide_rect_ratio O(1) Medium — shrunk rect Hitbox smaller than the sprite
collide_mask O(n) Maximum — pixel by pixel Bosses, irregular shapes (use sparingly)
General rule: start with AABB. Switch only when the inaccuracy
is noticeable. Reserve collide_mask for very few sprites.
Collisions — Summary
AABB (colliderect) = fastest, sufficient for most cases
spritecollide(sprite, group, kill) = one vs. many collision
groupcollide(g1, g2, k1, k2) = group vs. group collision
Response depends on type: separate, bounce, or destroy