1

UPDATE :

I'm making the brick breakout game. However, when the ball touches a brick, the brick disappears but instantly reappears. Here is the code related to the issue :

def brick_manager():
    bricks_positions = [] # brick list
    # adding the bricks' coordinates to the list
    for row in range(10):
        for col in range(7):
            bricks_positions.append((6 + col*75, 44 + row*20))
    for pos in bricks_positions :
        # drawing the bricks from the coordinates in the list
        pygame.draw.rect(screen1, brick_colors[7], (pos[0], pos[1], 70, 12), border_radius=2)
        if pos[0] <= ball_PX <= pos[0] + 62 and pos[1] -15 <= ball_PY <= pos[1] + 5:
            bricks_positions.remove(pos) 
        

FIX :

So what i did to solve the issue was moving the first part of the brick_manager() function outside of it and before the while loop. Then everything worked well. In fact, the problem was that everytime that function is called, the bricks' list resets to empty and the bricks themselves are redrawn that's why they were reappearing after the collision. Here is my code after solving the issue:

pygame.init()
...
bricks_positions = [] # brick list
for row in range(10):
    for col in range(7):
        bricks_positions.append((6 + col*75, 44 + row*20))

def brick_manager():
    for pos in bricks_positions :
        # drawing the bricks from the coordinates in the list
        pygame.draw.rect(screen1, brick_colors[7], (pos[0], pos[1], 70, 12),  
        border_radius=2)
        if pos[0] <= ball_PX <= pos[0] + 62 and pos[1] -15 <= ball_PY <= pos[1] + 5:
        bricks_positions.remove(pos)

...
while running:
    ...
    brick_manager()
    ...
pygame.quit()
Salem
  • 257
  • 3
  • 13
  • 1
    Please edit your question to include a [mcve] so it's possible to assist you. If you're seeing lag, it may be due to unnecessary calculations. It's also undesirable to modify an iterable during its iteration. – import random Mar 15 '21 at 12:50
  • @importrandom , done, and that's my best – Salem Mar 15 '21 at 13:10
  • Long story short, i want the bricks to disappear when collided with the ball in my brick breakout game – Salem Mar 15 '21 at 13:10
  • 3
    Looks like you are re-adding all bricks every time you call the `brick_manager` function. You probably need to split this function into two and call them at different times. – 0x5453 Mar 15 '21 at 13:16
  • 1
    @importrandom Thank you a lot !! i have just split that function and moved the first part of it outside of it but not in the while loopand it worked for me! now the bricks are being removed when collided – Salem Mar 15 '21 at 13:32

0 Answers0