How to do something if three different objects (rectangles) collide in pygame?

Viewed 43

I am currently creating a top down shooter game in pygame and my player is clipping through the corners of both my borders. I have listed my code below and an image of the issue.

Image of problem

#The playerhitbox, boarder 1 and boarder 2

    playerhitbox = pygame.draw.rect(display, (TRANSPARENT), (375, 270, 100, 100), 2)
        boarder1 = pygame.draw.rect(display, (255, 255, 255), (-475-display_scroll[0], 875-display_scroll[1], 1750, 5))
        boarder2 = pygame.draw.rect(display, (255, 255, 255), (-475-display_scroll[0], -675-display_scroll[1], 5, 1550))


#If playerhitbox collides with boarder 's' key has no effect
    if playerhitbox.colliderect(boarder1):
    
                if keys[pygame.K_a]:
                    display_scroll[0] -=5
    
                    player.moving_left = True
    
                    for bullet in player_bullets:
                        bullet.x -= 5
    
                if keys[pygame.K_d]:
                    display_scroll[0] +=5
    
                    player.moving_right = True
    
                    for bullet in player_bullets:
                        bullet.x += 5
    
                if keys[pygame.K_w]:
                    display_scroll[1] -=5
    
                    for bullet in player_bullets:
                        bullet.x -= 5
    
                if keys[pygame.K_s]:
                    display_scroll[1] +=0
    
                    for bullet in player_bullets:
                        bullet.x += 5

#If playerhitbox collides with boarder 'a' key has no effect

    if playerhitbox.colliderect(boarder2):
    
                if keys[pygame.K_a]:
                    display_scroll[0] -=5
    
                    player.moving_left = True
    
                    for bullet in player_bullets:
                        bullet.x -= 5
    
                if keys[pygame.K_d]:
                    display_scroll[0] +=5
    
                    player.moving_right = True
    
                    for bullet in player_bullets:
                        bullet.x += 5
    
                if keys[pygame.K_w]:
                    display_scroll[1] -=5
    
                    for bullet in player_bullets:
                        bullet.x -= 5
    
                if keys[pygame.K_s]:
                    display_scroll[1] +=5
    
                    for bullet in player_bullets:
                        bullet.x += 5

#If there is no collision between boarder and playerhitbox all keys (W, S, A, D) do something.

    else:

            if keys[pygame.K_a]:
                display_scroll[0] -=5

                player.moving_left = True

                for bullet in player_bullets:
                    bullet.x -= 5

            if keys[pygame.K_d]:
                display_scroll[0] +=5

                player.moving_right = True

                for bullet in player_bullets:
                    bullet.x += 5

            if keys[pygame.K_w]:
                display_scroll[1] -=5

                for bullet in player_bullets:
                    bullet.x -= 5

            if keys[pygame.K_s]:
                display_scroll[1] +=5

                for bullet in player_bullets:
                    bullet.x += 5

            if keys[pygame.K_p]:
                pause()
            if keys[pygame.K_m]:
                main_menu()
0 Answers
Related