简体   繁体   English

为什么我的精灵不能用 kill() function 正确删除?

[英]Why won't my sprites delete properly with the kill() function?

I am creating a game where you shoot enemies in a top-down format.我正在创建一个游戏,您可以在其中以自上而下的格式射击敌人。 My bullet and enemy function properly in every aspect except where it has to delete itself.我的子弹和敌人 function 在各个方面都正确,除非它必须自行删除。 I have hooked it up to where if the bullet collides with the enemy or 5 seconds have passed, the bullet would delete itself.我已经将它连接到如果子弹与敌人相撞或经过 5 秒,子弹会自行删除的位置。 It does do that, except the code continually iterates to create a sprite and kill it as the bullet moves for some reason, and still shows the bullet on the screen as if nothing is happening to it.它确实这样做了,除了代码不断迭代以创建一个精灵并在子弹由于某种原因移动时杀死它,并且仍然在屏幕上显示子弹,就好像它什么也没发生一样。 Can someone tell me why the code is iterating the creation of a bullet and the deletion of it so many times?有人能告诉我为什么代码会重复创建子弹并删除它这么多次吗?

-------- CODE -------- - - - - 代码 - - - -

method in player class播放器 class 中的方法

def shoot(self):
    pos = pygame.mouse.get_pos()
    pos += camera_group.offset
    click = pygame.mouse.get_pressed()
    if click[0] and not self.shooting:
        self.shooting = True
        # create bullet instance if player shoots
        bullet = Bullet(self.rect.centerx, self.rect.centery, "bullet", pos, camera_group)
        bullet_group.add(bullet)

    if not click[0]:
        self.shooting = False

bullet class子弹 class

class Bullet(Entity):
def __init__(self, x, y, image, pos, group):
    super().__init__(x, y, image, group)
    pygame.sprite.Sprite.__init__(self)
    # creating and scaling image
    self.image = pygame.transform.flip(self.image, True, False)
    self.image = pygame.transform.scale(self.image, (32, 16))
    # creating rect
    self.x = x
    self.y = y
    self.speed = 8
    self.mousepos = pos
    # calculating angle of bullet
    self.angle = math.atan2(pos[1] - y, pos[0] - x)
    self.angle_deg = (self.angle * (180 / math.pi))
    self.image = pygame.transform.rotate(self.image, self.angle_deg * -1)
    # calculating vel_x/vel_y
    dx = self.mousepos[0] - player.rect.centerx
    dy = self.mousepos[1] - player.rect.centery
    total = abs(dx) + abs(dy)
    self.vel_x = (self.speed * (dx / total))
    self.vel_y = (self.speed * (dy / total))
    # setting variable to store time of bullet creation
    self.start_time = pygame.time.get_ticks()

def update(self):
    if pygame.time.get_ticks() - self.start_time > 50 and len(bullet_group) >= 1:
        bullet_group.sprites()[0].kill()

    elif self.rect.colliderect(enemy):
        bullet_group.sprites()[0].kill()
        enemy.kill()

    else:
        # movement
        self.x += self.vel_x
        self.y += self.vel_y
        self.rect.x = int(self.x)
        self.rect.y = int(self.y)

Don't kill the first bullet in the Group , just delete the bullet object itself:不要杀死Group中的第一个子弹,只删除子弹 object 本身:

bullet_group.sprites()[0].kill()

self.kill()

method update in class Bullet : class Bullet中的方法update

class Bullet(Entity):
    # [...]

    def update(self):
        if pygame.time.get_ticks() - self.start_time > 50 and len(bullet_group) >= 1:
            self.kill()

        elif self.rect.colliderect(enemy):
            self.kill()
            enemy.kill()

        else:
            # movement
            self.x += self.vel_x
            self.y += self.vel_y
            self.rect.x = int(self.x)
            self.rect.y = int(self.y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM