Skip to content Skip to sidebar Skip to footer

How To Detect Collision Of Two Images In Pygame

I was wondering how to do collision detection in pygame. My game doesn't use sprites, it just blits 2 images. How could I check if they are colliding? I know there is sprite.collid

Solution 1:

Use pygame.Rect and colliderect() to check for collision.

Create pygame.Rect objects, with the size of the image and the location, where you have blit the images. pygame.Surface.get_rect() creates a pygame.Rect object at position (0, 0), but the top left position can be set by the keyword argument topleft:

fishRect  = fishImg.get_rect(topleft = (x, y))
enemyRect = enemyImg.get_rect(topleft = (enemyX, enemyY))

if fishRect.colliderect(enemyRect):
    # [...] collision detected

Post a Comment for "How To Detect Collision Of Two Images In Pygame"