collision-detection pygame pygame-surface pygame2 python

How do I detect collision in pygame?


I have made a list of bullets and a list of sprites using the classes below. How do I detect if a bullet collides with a sprite and then delete that sprite and the bullet?

#Define the sprite class
class Sprite:

    def __init__(self,x,y, name):


        self.image = pygame.image.load(name)

        self.rect = self.image.get_rect()

    def render(self):
        window.blit(self.image, (self.x,self.y))

# Define the bullet class to create bullets          
class Bullet:

    def __init__(self,x,y):
        self.x = x + 23
        self.y = y
        self.bullet = pygame.image.load("user_bullet.BMP")
        self.rect = self.bullet.get_rect()

    def render(self):
        window.blit(self.bullet, (self.x, self.y))


  • I would notes that there is a Sprite Class in pygame – I am not sure redefining it in your code is a good idea. Besides are they really targets (for want of a better word), as a sprite is simply an object with a graphical representation on screen (and therefore your Bullet is a sprite too).

    Apr 15, 2015 at 16:29


From what I understand of pygame you just need to check if the two rectangles overlap using the colliderect method. One way to do it is to have a method in your Bullet class that checks for collisions:

def is_collided_with(self, sprite):
    return self.rect.colliderect(sprite.rect)

Then you can call it like:

sprite = Sprite(10, 10, 'my_sprite')
bullet = Bullet(20, 10)
if bullet.is_collided_with(sprite):


  • 6

    Note that if the speed of bullet relative to the target is more than target’s width per tick, the bullet can just ‘teleport’ to the other side of the target without hitting. If this might be the case, you might need to check against a rectangle that represents the trajectory of the bullet from the previous frame to the current frame, not just the bullet itself.

    – 9000

    Apr 15, 2015 at 4:17


There is a very simple method for what you are trying to do using built in methods.

here is an example.

import pygame
import sys

class Sprite(pygame.sprite.Sprite):
    def __init__(self, pos):
        self.image = pygame.Surface([20, 20])
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect() = pos

def main():
    clock = pygame.time.Clock()
    fps = 50
    bg = [255, 255, 255]
    size =[200, 200]

    screen = pygame.display.set_mode(size)

    player = Sprite([40, 50])
    player.move = [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]
    player.vx = 5
    player.vy = 5

    wall = Sprite([100, 60])

    wall_group = pygame.sprite.Group()

    player_group = pygame.sprite.Group()

    # I added loop for a better exit from the game
    loop = 1
    while loop:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                loop = 0

        key = pygame.key.get_pressed()

        for i in range(2):
            if key[player.move[i]]:
                player.rect.x += player.vx * [-1, 1][i]

        for i in range(2):
            if key[player.move[2:4][i]]:
                player.rect.y += player.vy * [-1, 1][i]


        # first parameter takes a single sprite
        # second parameter takes sprite groups
        # third parameter is a do kill command if true
        # all group objects colliding with the first parameter object will be
        # destroyed. The first parameter could be bullets and the second one
        # targets although the bullet is not destroyed but can be done with
        # simple trick bellow
        hit = pygame.sprite.spritecollide(player, wall_group, True)

        if hit:
            # if collision is detected call a function in your case destroy
            # bullet
            player.image.fill((255, 255, 255))



    # sys.exit

if __name__ == '__main__':