1 year ago

#347059

test-img

Karsonthefoxx

Pygame groupcollide and spritecollideany kills all sprites

Im making a top-down scrolling game where the player is a small ship that can shoot "falling" red balls to destroy them, i have individual collision working but when i try to use pygame.sprite.groupcollide or pygame.sprite.spritecollideany it starts deleting all bullets and/or red balls. ive looked at many sources and have even copied the code exactly but i get the same result

bullet class

class bulletOBJ(pygame.sprite.Sprite):
    def __init__(self, image, width, height, pos):

        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.blit(image, (0, 0))
        self.rect = self.image.get_rect()

        self.xpos, self.ypos = pos
        self.vel = 6

    def render(self, screen):
        screen.blit(self.image, (self.xpos, self.ypos))
    
    def move(self):
        self.ypos -= self.vel

enemy class

class enemyOBJ(pygame.sprite.Sprite):
    def __init__(self, image, width, height):
        super().__init__()
        self.image = pygame.Surface((width, height))
        self.image.blit(image, (0, 0))
        self.rect = self.image.get_rect()
        
        self.xpos = rng(10, 790)
        self.ypos = 10
        self.vel = rng(3, 10)
    def render(self, screen):
        screen.blit(pygame.transform.smoothscale(self.image, (30, 30)), (self.xpos, self.ypos))
    
    def move(self):
        self.ypos += self.vel

collision (with groupcollide)

pygame.sprite.groupcollide(player.bullets, enemies, True, True)

with spritecollideany

    for bullet in player.bullets:
        gets_hit = pygame.sprite.spritecollide(bullet, enemies, True)

python

pygame

collision-detection

0 Answers

Your Answer

Accepted video resources