如何在 pygame 中只杀死自己的精灵?

How to kill only self sprite in pygame?

下午好,

我正在 Python 中使用 Pygame 编写一个小程序,并希望 Cell 精灵在与 Food 精灵碰撞时被杀死。目前,当两个精灵碰撞时,两个精灵都会被杀死。我如何错误地使用 kill 功能?我只是告诉 Cell 子画面杀死它自己。我知道让 Cell 在与 Food 碰撞时死亡是违反直觉的,我只是想学习 Pygame 的功能。代码如下:

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()

来自您使用的方法的文档:

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

您是否尝试过将布尔值设置为 False

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

参考 documentation