Search code examples
pythonpygamerectangles

Python doesn't detect a colliderect for one of my objects


I have this code

if ball.colliderect(border_up_rect):
    border_up_collide = True
elif ball.colliderect(border_down_rect):
    border_down_collide = True
elif ball.colliderect(goal_attack_rect):
    my_score += 1
elif ball.colliderect(goal_def_rect):
    ai_score += 1


if border_up_collide == True:
    if where_did_it_come_from == True:
        ball_x -= 7
        ball_y += 2
        pboard_shoot1 = False
        pboard_shoot2 = False
        pboard_shoot3 = False
    elif where_did_it_come_from == False:
        ball_x -= 3
        ball_y += 2
        ai_shoot1 = False
        ai_shoot2 = False
        ai_shoot3 = False
elif border_down_collide == True:
    if where_did_it_come_from == True:
        ball_x -= 7
        ball_y -= 2
        pboard_shoot1 = False
        pboard_shoot2 = False
        pboard_shoot3 = False
    elif where_did_it_come_from == False:
        ball_x -= 3
        ball_y -= 2
        ai_shoot1 = False
        ai_shoot2 = False
        ai_shoot3 = False
elif goal_attack_collide == True:
    if where_did_it_come_from == True:
        ball_x = 600
        ball_y = 220
        pboard_shoot1 = False
        pboard_shoot2 = False
        pboard_shoot3 = False
    elif where_did_it_come_from == False:
        ball_x = 600
        ball_y = 220
        ai_shoot1 = False
        ai_shoot2 = False
        ai_shoot3 = False

Here it works fine with border_up_rect and also every other collide.rect, but it doesn't work with border_down_rect. It just lets the ball go through it.

How can I fix this?


Solution

  • The reason why border_down_rect isn't functioning is because border_up_rect is still true and until border_up_rect is true it won't toggle the elif statement.