1

I'm creating a battle simulator of a game (a rip-off of Undertale) in Pygame, the 2D game is a heart that is able to choose stuff and run away from projectiles. I created a sprite class of a bullet. So a sequence of bullets is created without a name:

bullet_group = pygame.sprite.Group()
bullet_group.add(Bullet(blah, blah, blah))

-and has an initial x and y position and a velocity towards the end of the screen. When it touches the edge it gets killed, which is updated in the update function of the Bullet sprite class:

if self.direction == "right":
    self.rect.x += self.speed
    if self.rect.x >= WIDTH:
         pygame.sprite.Sprite.kill(self)

I have a separate counter and delay time between each bullet and a quantity, speed, and direction.

I tried to make a function using those values, however it is incomplete as I do not know how I can detect if the last sprite (of this input quantity of sprites) is killed. I need to know this because I want to go back to the battle menu (for choosing stuff) after the battle (running away from projectiles). How do I know if the last sprite is killed without using len(bullet_group). len(bullet_group) doesnt work at high projectile speeds, or long delays. There are other processes (like the heart dodging the projectiles), functions like time.sleep() doesn't work in this case.

3 Answers3

0

You can check if the last sprite of a group was killed by checking if the group is empty. You can get the number of sprites in a group with len. e.g.:

if len(your_group) == 0:
    print('group is empty')
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

use the sprites() method of the group class:

for i in range(quantity):
        bullet_group.add(Bullet(x_pos, y_pos, speed, direction))
        time.sleep(delay)
    while True:
        all_dead = all(not sprite.alive() for sprite in bullet_group.sprites())
        if all_dead:
            break
        else:
            pygame.time.Clock().tick(60)

he while loop checks if all sprites in the group are dead by using a list comprehension to check the alive() method of each sprite, and the all() function to check if they are all False. If they are all dead, it breaks out of the loop and returns to the battle menu

Abdulmajeed
  • 1,502
  • 2
  • 10
  • 13
  • I've tried implementing this but my program just buffers when it reaches that battle stage. I think it might be due to the time.sleep(delay) function preventing other things from updating (like the heart running away from the projectiles). I have a counter and a delay, so is there a way I could use those here, this way other processes will be unhindered. – Python Undertale Coder Mar 11 '23 at 23:59
0
# Variables for atk function
atk_counter = 0
first_bullet = False
all_dead = False
is_first_bullet_alive = False
def atk(qty, delay, direction, speed):
    global atk_counter, bullet_group, first_bullet, all_dead, is_first_bullet_alive
    atk_counter += 1
    for i in range(1, qty+1):
        if atk_counter == delay*i:
            print("bullet")
            bullet_group.add(Bullet(direction, speed))
        all_dead = all(not sprite.alive() for sprite in bullet_group.sprites())
        if not is_first_bullet_alive:
            try:
                first_bullet = bullet_group.sprites()[1].alive()
                is_first_bullet_alive = True
            except IndexError:
                first_bullet = False
    if all_dead and is_first_bullet_alive:
        atk_counter = 0
        first_bullet = False
        all_dead = False
        is_first_bullet_alive = False
        board.battle = False
        board.level += 1
        fight_button.active = True
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 13 '23 at 00:06