0

I'm making a "Breakout"-style game and I thought it would be fun to be able to handle multiple balls. This is the run_logic() function in my game class:

def run_logic(self):

    if not self.game_over:
        self.all_sprites_list.update()

        if self.ball.y > SCREEN_HEIGHT:
            self.ball_list.remove(self.ball)
            self.all_sprites_list.remove(self.ball)

            if len(self.ball_list) == 0:
                self.game_over = True

        bounce_balls = pygame.sprite.spritecollide(self.player, self.ball_list, False)

        if len(bounce_balls) > 0:

            for b in bounce_balls:
                diff = (self.player.rect.x + self.player.width/2) - (b.rect.x + b.width/2)
                b.rect.y = SCREEN_HEIGHT - 50 - b.rect.height
                b.bounce(diff)

        for ball in self.ball_list:

            dead_blocks = pygame.sprite.spritecollide(ball, self.block_list, True)

            if len(dead_blocks) > 0:
                ball.bounce(0)

                power1 = random.randrange(0, 10)
                if power1 == 1:
                    self.ball = Ball(self.level)
                    self.ball_list.add(self.ball)
                    self.all_sprites_list.add(self.ball)

            if len(self.block_list) == 0:
                self.game_over = True
                self.level += 1

And here is the __init__() function, if it helps:

def __init__(self):

    self.game_over = False

    self.block_list = pygame.sprite.Group()
    self.ball_list = pygame.sprite.Group()
    self.all_sprites_list = pygame.sprite.Group()

    top = 40
    block_height = 30

    for row in range(4):

        for column in range(0, 1):  # 29
            block = Block(BLOCK_COLOUR_LIST[self.level], column * 65 + 6, top)
            self.block_list.add(block)
            self.all_sprites_list.add(block)

        top += block_height + 10

    self.player = Player(self.level)
    self.all_sprites_list.add(self.player)

    self.ball = Ball(self.level)
    self.ball_list.add(self.ball)
    self.all_sprites_list.add(self.ball)

It works fine when there is just one ball, when it hits the ground the game quits. But as soon as multiple balls are spawned (randomly by hitting the blocks), it doesn't matter if all of them hit the ground, I still can't die. I Couldn't find a similiar question posted here before, and I have no idea why this doesn't work so I would really appreciate some help.

pppery
  • 3,731
  • 22
  • 33
  • 46
  • 1
    You add the same object (self.ball) multiple times to self.ball_list but only remove it once if the ball hits bottom. Your lose condition will not be true. – Schore May 15 '16 at 17:52
  • I'm sorry, I don't understand. This is how I want it to work: I have one ball to begin with, and I add one roughly every 10th block I hit. Whenever one of those balls hit the floor, the game first removes that ball and then checks to see if it was the last one (if the list was empty). If it was, then the game is over. Is this not wat is happening? – Jesper Vines May 15 '16 at 18:47
  • yes, but then you are spawning more balls, so the list never becomes empty as they are added to the ball group. – marienbad May 15 '16 at 23:51

1 Answers1

0

The issue is the following, as already pointed out in the comments:

You only check if the ball stored in self.ball should be removed. If you add more balls to the list, you never check if those have to be removed as well, as they are not stored in self.ball .

To fix it, update your game logic in a loop, in which you go over every ball in the list.

xXliolauXx
  • 1,273
  • 1
  • 11
  • 25