Skip to content Skip to sidebar Skip to footer

Collisions Aren't Being Detected In Pygame

I don't know why but collisions aren't being detected in my game. Its an asteroids style game and I want the bullets to destroy the asteroids, and the game to end when the ship get

Solution 1:

I couldn't test code but usually problem is that collision's function use values from self.rect but you don't use them but self.x and self.y to keep position.

You should do in __init__

self.rect = self.image.get_rect()

self.rect.center = (x, y)

and later you can add to self.rect.x and self.rect.y and it will automatically calculate position for

blit(self.image, self.rect)

and for collision with other object

self.rect.colliderect(other.rect)

or with mouse in event.MOUSEBUTTONDOWN

self.rect.collidepoint(event.pos)

to test if object was clicked (ie. button).


If you want to use pygame Group

asteroids = pygame.sprite.Group()
bullets = pygame.sprite.Group()

then you shouldn't create them inside collisions() but at start instead of lists

asteroids = []
bullets = []

and you should add objects to groups instead of appending to lists.

asteroids.add(Asteroid())

and you can even run function for all objects without using for-loop

asteroids.draw()

Post a Comment for "Collisions Aren't Being Detected In Pygame"