Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I only remove sprites that have collided?

These are my classes and the part of the main loop that should create the sprites and then remove them when they collide. Right now both bullets and zombies are being removed with each mouse click, although mouse clicks should just add bullets to the group. Maybe someone has an idea on what I am doing wrong.

class PlayerBullet(pygame.sprite.Sprite):
    def __init__(self, x, y, mouse_x, mouse_y, ):
        pygame.sprite.Sprite.__init__(self, player_bullets)
        self.x = x
        self.y = y
        self.mouse_x = mouse_x
        self.mouse_y = mouse_y
        self.speed = 20
        self.angle = math.atan2(y-mouse_y, x-mouse_x)
        self.image = circle_image


        self.rect = self.image.get_rect()

    def update(self):
        self.x_vel = math.cos(self.angle) * self.speed
        self.y_vel = math.sin(self.angle) * self.speed
        self.x -= int(self.x_vel)
        self.y -= int(self.y_vel)


    def main(self, display):
        self.update()
        display.blit(self.image, (self.x-8, self.y-7))


class Zombie(pygame.sprite.Sprite):
    def __init__(self, x, y, speed):
        pygame.sprite.Sprite.__init__(self, list_zombies)
        self.x = x
        self.y = y
        self.speed = zombie_speed
        self.image = pygame.image.load("Zombie.png").convert()
        self.rect = self.image.get_rect()

    def update(self):
        self.y += 1

    def main(self, display):
        self.update()

        display.blit(zombie_image_copy, (self.x, self.y))

main loop:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:

            pygame.display.quit()
            pygame.QUIT
            sys.exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                player_bullets.add(PlayerBullet(player.x, player.y, mouse_x, mouse_y))

if not list_zombies:
        max_zombie_count += 1
        zombie_speed += 0.5
        while zombie_count < max_zombie_count:
            spawnpoint_x = random.randint(50,1400)
            spawnpoint_y = 0

            list_zombies.add(Zombie(spawnpoint_x, spawnpoint_y, zombie_speed))
            zombie_count += 1
            #zombie += 1

    for bullet in player_bullets:
        bullet.main(display)


    for zombie in list_zombies:
        zombie.main(display)

    for zombie in list_zombies:
        for bullet in player_bullets:
            if pygame.sprite.collide_rect(bullet, zombie):
                bullet.kill()
                zombie.kill()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen.
Therefore all the rectangles have the same position (0, 0) and the collision test evaluates True for all objects. You have to update the position of the rectangle before the collision test:

for zombie in list_zombies:
     zombie.rect.topleft = (zombie.x, zombie.y)
​     for bullet in player_bullets:
         bullet.rect.topleft = (bullet.x, bullet.y)
         ​if pygame.sprite.collide_rect(bullet, zombie):
               ​bullet.kill()
               ​zombie.kill()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading