0

I am currently creating a top down shooter arcade game in Pygame and my player (the spaceship) is clipping through the corners of both my borders (where both boarders intersect). I have listed my code below and an image of the issue as well as the codes that I tried that didn't work.

Thanks.

Image of Problem Image of problem Here

#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

I have tried...

if playerhitbox.colliderect(boarder1) and playerhitbox.colliderect(boarder2):
    #do something

and 

if playerhitbox.colliderect(boarder1):
    if playerhitbox.colliderect(boarder2):
        #do something

Which didn't work

Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • When that screen-shot was taken, what was the content of `display_scroll`? I'm thinking that there's some difference between the "on-screen" pixel coordinates and the "scrolled" coordinates. FWIW, I would collect the key-press position changes into day a `dx`, `dy`, and then zero the appropriate ones on collision. It would simplify the code. – Kingsley Sep 19 '22 at 01:09
  • Hmm, the `pygame.draw.rec()` manual says it returns: "*a rect bounding the changed pixels*". Does drawing a *transparent* box **change** the pixels? What do you see if you print the content of `playerhitbox` ? Please log the content of the 3 Rects at the time of mis-collision. Add these stats to the question. Maybe the `playerhitbox` will have a zero width & length. – Kingsley Sep 19 '22 at 01:15
  • When I print playerhitbox it shows a red outline of a square around the spaceship. I did draw a transparent rectangle but the pixels inside the rectangle are still apart of the original rect. I am unsure of what you mean by logging the content of the 3 Rects at the time of mis-collision? – Maddox Styles Sep 19 '22 at 02:33
  • I mean print the `x`, `y`, `width`, `height` of each Rect. E.g.: `print( "playerhitbox="+str( playerhitbox) )`. There doesn't seem to be anything obviously wrong with your collision code, so something in the rects *must* be wrong. – Kingsley Sep 19 '22 at 03:47

0 Answers0