Skip to content Skip to sidebar Skip to footer

How To Use Sprite Collide In Pygame

I am making a very simple game where the bird (player) has to dodge the rock and if it gets hit by the rock you lose. I am trying to use pygame.sprite.collide_rect() to tell if the

Solution 1:

def checkCollision(sprite1, sprite2):
    col = pygame.sprite.collide_rect(sprite1, sprite2)
    if col == True:
        sys.exit()

should be

def checkCollision(self, sprite1, sprite2):
    col = pygame.sprite.collide_rect(sprite1, sprite2)
    if col == True:
        sys.exit()

since it's a method bound to an object.

Solution 2:

Change

defcheckCollision(sprite1, sprite2):

To

defcheckCollision(self, sprite1, sprite2):

And you don't have to check the collision on every event, reduce the indent of rock.checkCollision(bird.image_b, rock.image_b) by 1.

Solution 3:

You have this:

col = pygame.sprite.collide_rect(sprite1, sprite2)

But an easier way to do this would be to simply use colliderect which is a function of rect. It might be easier to try this:

col=sprite1.rect.colliderect(sprite2.rect)

Post a Comment for "How To Use Sprite Collide In Pygame"