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.