The issue you're facing is due to the way the drawing and collision detection is structured in your code. When the ball is not colliding with the brick, the brick is being drawn again on the screen, which is why it reappears.
To fix this, you should separate the collision detection logic from the drawing logic. Here's a suggested approach:
Update the Collision Detection:
Check for collisions and update the bricks_list
accordingly. Once a brick is removed from the bricks_list
, it won't be drawn again.
Draw the Bricks:
After updating the bricks_list
for collisions, loop through the bricks_list
and draw all the bricks.
Here's how you can modify your code:
# Assuming you have initialized bricks_list like this:
bricks_list = [Brick(356, 188, 69, 25, 6), Brick(426, 188, 69, 25, 6)]
# Collision detection
for BRICK in bricks_list[:]: # Using a slice to avoid issues while removing items during iteration
brick_rect = pygame.Rect(BRICK.x, BRICK.y, BRICK.width, BRICK.height)
if ball_rect.colliderect(brick_rect):
bricks_list.remove(BRICK)
# Drawing bricks
for BRICK in bricks_list:
brick_rect = pygame.Rect(BRICK.x, BRICK.y, BRICK.width, BRICK.height)
brick_rectangle = pygame.draw.rect(screen, LEVELS[BRICK.level], brick_rect)
By separating the collision detection from the drawing logic, the bricks that have been removed due to collisions won't be drawn again, and they will disappear permanently.