-2

Here's my code:

brick := Brick(356, 188, 69,25,6),
brick := Brick(426, 188, 69,25,6)]
    


    for BRICK in bricks_list:
        brick_rect = pygame.Rect(BRICK.x, BRICK.y, brick.width, BRICK.height)
        if not ball_rect.colliderect(brick_rect):
             brick_rectangle = pygame.draw.rect(screen, LEVELS[BRICK.level], brick_rect)
         else:
             bricks_list.remove(BRICK)

My problem is, the brick disappears ONLY and ONLY at the moments when the ball is colliding with it. If the ball isn't colliding, the brick re appears. But I want it to disappear definitely

LGM
  • 9
  • 2
  • Do you have a multiple accounts? The code in the following question looks almost identical: https://stackoverflow.com/questions/76984172/make-collide-a-rectangle-and-objects-of-a-list-pygame, https://stackoverflow.com/questions/76984934/pygame-rectangle-in-list-not-vanishing . Note that you are not allowed to create a second account to ask a question just to get around the fact that you are currently not allowed to ask new questions with your first account. – Rabbid76 Aug 28 '23 at 04:46

1 Answers1

1

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:

  1. 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.

  2. 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.

Joaquin
  • 452
  • 7
  • I don't know why, but it still doesn't work! Everytime the ball doesn't collide with the brick, the brick still re appears! BY THE WAY, I've printed the length of bricks_list, and it seems like everytime the ball isn't colliding, the list is returning to it's original length ! (if ball collides, list's length -1, if not, it's the initial length) – LGM Aug 27 '23 at 22:15
  • The behavior you're describing suggests that somewhere in your code, bricks_list is being reset to its original state on each frame or iteration of your game loop. This would cause removed bricks to reappear because the list is being repopulated. – Joaquin Aug 27 '23 at 22:25
  • Initialization of bricks_list: Ensure that the initialization of bricks_list (where you define the bricks) is outside of your main game loop. If it's inside the loop, it will reset on every iteration. Other Modifications: Check if there are any other parts of your code that modify bricks_list. Look for any code that might be re-adding bricks to the list. Game State Resets: If you have any logic to reset the game state (e.g., after a game over or level completion), ensure that this logic isn't inadvertently being triggered. – Joaquin Aug 27 '23 at 22:26
  • 1
    Holy Jesus. Oh my god. How can I be so dumb ! YESS ! Turns out I've put the list in the game loop (for some reason) T-T Thank you for your answer I can finally rest in peace – LGM Aug 27 '23 at 22:29