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()