首页 > 解决方案 > 如何在pygame中只杀死自己的精灵?

问题描述

下午好,

我正在用 Python 编写一个小程序,使用 Pygame,如果 Cell 精灵与食物精灵碰撞,我希望它被杀死。目前,当两个精灵碰撞时,两个精灵都会被杀死。我如何错误地使用 kill 功能?我告诉细胞精灵只是为了杀死它自己。我知道当 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()

标签: pythonpygamespritekill

解决方案


从您使用的方法的文档中:

dokill 参数是一个布尔值。如果设置为 True,所有发生碰撞的 Sprite 将从组中移除。

您是否尝试将 bool 设置为False

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

参考文档


推荐阅读