Search code examples
pythonpygamespritekill

How to kill only self sprite in pygame?


Good Afternoon,

I am writing a small program in Python, using Pygame, and want a Cell sprite to be killed if it collides with a Food sprite. Currently, when the two sprites collide, BOTH sprites are killed. How am I using the kill function incorrectly? I am telling the Cell sprite only to kill its self. I know having the Cell die when it collides with Food is counter-intuitive, I am just trying to learn the functions of Pygame. Code below:

import os
import random

#define background color
BACKGROUND = (17, 109, 145)

#define framerate of simulation
FPS = 60

WIDTH, HEIGHT = 1000, 700
WIN = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Cell Evolver")
game_area = pg.Rect(30, 30, 940, 640)
game_area_color = pg.Color('aquamarine2')


# game loop, check collisions, etc...
def main():
    
    

    food_sprites = pg.sprite.Group()
    food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
    food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
    food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
    food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))

    cell_sprites = pg.sprite.Group()
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))
    cell_sprites.add(Cell(game_area.center, game_area,))

    clock = pg.time.Clock()

    run = True
    while run:

        for event in pg.event.get():
            if event.type ==pg.QUIT:
                run = False
        
        cell_sprites.update(food_sprites)

        WIN.fill(BACKGROUND)

        cell_sprites.draw(WIN)
        food_sprites.draw(WIN)
        pg.draw.rect(WIN, game_area_color, game_area, 1)

        
        pg.display.flip()
        clock.tick(FPS)
        
    pg.quit()

class Cell(pg.sprite.Sprite):
    def __init__(self, pos, game_area):
        super().__init__()
        self.image = pg.Surface((6, 6))
        self.image.fill(pg.Color('aquamarine2'))
        self.rect = self.image.get_rect(center=pos)
        self.game_area = game_area

    def update(self, food_sprites):
        randNumx = random.randint(-2,2)
        randNumy = random.randint(-2,2)
        self.rect.x +=randNumx
        self.rect.y +=randNumy
        if pg.sprite.spritecollide(self, food_sprites, True):
            self.kill()

class Food(pg.sprite.Sprite):
    def __init__(self, pos, game_area):
        super().__init__()
        
        self.image = pg.Surface((6, 6))
        self.image.fill(pg.Color('white'))
        self.rect = self.image.get_rect(center=pos)
        self.game_area = game_area
        

if __name__ == "__main__":
    pg.init()
    main()
    pg.quit()

Solution

  • From the documentation of the method that you use:

    The dokill argument is a bool. If set to True, all Sprites that collide will be removed from the Group.

    Have you tried setting the bool to False?

    if pg.sprite.spritecollide(self, food_sprites, False):
        self.kill()
    

    Ref documentation