如何从许多精灵中杀死一个?

how to kill One from many sprites?

我正在创建一个简单的游戏,我在其中玩飞机并躲避火箭。 我想做可以用键盘激活的“障碍”。 当火箭撞到障碍物时,应该杀死这枚特殊的火箭。 我试过 kill(),但没有用,我也试过从 sprite 组中移除等等。

非常感谢你的帮助,因为我卡住了,再见

这是我的代码:

import pygame, random, time
from pygame.locals import(
    RLEACCEL,
    K_LEFT,
    K_DOWN,
    K_UP,
    K_RIGHT,
    K_ESCAPE,
    K_SPACE,
    K_w,
    K_a,
    K_s,
    K_d,
    KEYDOWN,
    QUIT,

)

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 700

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\jet.png').convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect()

    def update(self, pressed_keys):
        if pressed_keys[K_w]:
            self.rect.move_ip(0, -5)
        if pressed_keys[K_s]:
            self.rect.move_ip(0, 5)
        if pressed_keys[K_a]:
            self.rect.move_ip(-5, 0)
        if pressed_keys[K_d]:
            self.rect.move_ip(5, 0)

        if pressed_keys[K_UP]:
            self.rect.move_ip(0, -5)
        if pressed_keys[K_DOWN]:
            self.rect.move_ip(0, 5)
        if pressed_keys[K_LEFT]:
            self.rect.move_ip(-5, 0)
        if pressed_keys[K_RIGHT]:
            self.rect.move_ip(5, 0)

        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH
        if self.rect.top <= 0:
            self.rect.top = 0
        if self.rect.bottom >= SCREEN_HEIGHT:
            self.rect.bottom = SCREEN_HEIGHT

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super(Enemy, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\missile.png').convert()
        self.surf.set_colorkey((255, 255, 255), RLEACCEL)
        self.rect = self.surf.get_rect(
            center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
        self.speed = random.randint(5, 16)

    def update(self):
        self.rect.move_ip(-self.speed, 0)
        if self.rect.right < 0:
            self.kill()

class Cloud(pygame.sprite.Sprite):
    def __init__(self):
        super(Cloud, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\cloud.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)

        self.rect = self.surf.get_rect(
            center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )

    def update(self):
        self.rect.move_ip(-6, 0)
        if self.rect.right < 0:
            self.kill()

class Sun(pygame.sprite.Sprite):
    def __init__(self):
        super(Sun, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\sunnn.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)

        self.rect = self.surf.get_rect()

class Ballon(pygame.sprite.Sprite):
    def __init__(self):
        super(Ballon, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\gula.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect(

            center =(
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
                random.randint(0, SCREEN_HEIGHT),
            )
        )
    
    def update(self):
        self.rect.move_ip(-6, 0)
        if self.rect.right < 0:
            self.kill()

class Barrier(pygame.sprite.Sprite):
    def __init__(self):
        super(Barrier, self).__init__()
        self.surf = pygame.image.load('C:\Sprites\shield.png').convert()
        self.surf.set_colorkey((0, 0, 0), RLEACCEL)
        self.rect = self.surf.get_rect()
    
    def update(self):
        
        if pressed_keys[K_w]:
            self.rect.move_ip(0, -5)
        if pressed_keys[K_s]:
            self.rect.move_ip(0, 5)
        if pressed_keys[K_a]:
            self.rect.move_ip(-5, 0)
        if pressed_keys[K_d]:
            self.rect.move_ip(5, 0)

pygame.init()

pygame.display.set_caption('Simple game')

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)

ADDCLOUD = pygame.USEREVENT + 2
pygame.time.set_timer(ADDCLOUD, 3100)

ADDBALLON = pygame.USEREVENT + 3
pygame.time.set_timer(ADDBALLON, 4500)

enemy = Enemy()
player = Player()
barrier = Barrier()
sun = Sun()
ballon = Ballon()

enemies = pygame.sprite.Group()
clouds = pygame.sprite.Group()
ballons = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(sun)
all_sprites.add(enemy)

Time = 0

clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.USEREVENT:
            Time += 0.25

        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            
            elif event.key == K_SPACE:
                all_sprites.add(barrier)
                
        elif event.type == QUIT:
            running = False

        


        elif event.type == ADDENEMY:
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)
        elif event.type == ADDCLOUD:
            new_cloud = Cloud()
            clouds.add(new_cloud)
            all_sprites.add(new_cloud)

        elif event.type == ADDBALLON:
            new_ballon = Ballon()
            ballons.add(new_ballon)
            all_sprites.add(new_ballon)

    

    clock.tick(40)
    screen.blit(player.surf, player.rect)

    pressed_keys = pygame.key.get_pressed()
    player.update(pressed_keys)

    enemies.update()
    clouds.update()
    ballons.update()
    barrier.update()

    screen.fill((135, 206, 250))

    for entity in all_sprites:
        screen.blit(entity.surf, entity.rect)
        pygame.display.flip()

    if pygame.sprite.spritecollideany(player, enemies):
        player.kill()
        running = False


    counter, text = str(Time), '                       Score: ' + str(Time)
    pygame.time.set_timer(pygame.USEREVENT, 1000)
    font = pygame.font.SysFont('Consolas', 30)
    
    if pygame.sprite.spritecollideany(barrier, enemies):
        enemy.kill()

    

    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    pygame.display.flip()


pygame.quit()

你杀错了敌人。 enemy 变量包含一个随机敌人,但不包含正在碰撞的敌人。 pygame.sprite.spritecollideany() returns 碰撞的敌人Sprite

collided_enemy = pygame.sprite.spritecollideany(barrier, enemies):
if collided_enemy != None:
    collided_enemy .kill()

或者您可以使用 pygame.sprite.spritecollide() 并将 dokill 参数设置为 True:

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

pygame.sprite.spritecollide(barrier, enemies, True)