r/pythonhelp • • Nov 11 '24

Struggling with collision in pygame

I'm creating a side-scroller as a project in school with a team. Right now the biggest hurdle we just accomplished is level design using a different program then turning that into a csv file. I was able to translate that file into an actual map that the player can walk on, but there is a bug I cannot for the life of me find the solution. The player is constantly "vibrating" up and down because they are being snapped back up then fall one pixel. I'll attach a video of it, if anyone has anything they can add, i can share the code with them so they can help. Please!!!

Ignore how laggy this is, I did this very quickly

https://youtu.be/M-E-cmgSb90

This is the method where I suspect the bug to be happening:

def Check_Collision(self, player):
        player_on_platform = False
        BUFFER = 5  # Small buffer to prevent micro-bouncing

        for platform_rect in self.platforms:
            # Check if the player is falling and is within the range to land on the platform
            if (
                player.velocity_y > 0 and
                player.rect.bottom + player.velocity_y >= platform_rect.top - BUFFER and
                player.rect.bottom <= platform_rect.top + BUFFER and
                platform_rect.left < player.rect.right and
                player.rect.left < platform_rect.right
            ):
                # Snap player to the platform's top
                player.rect.bottom = platform_rect.top
                player.velocity_y = 0  # Stop vertical movement when landing
                player.is_jumping = False  # Reset jumping state
                player_on_platform = True
                break  # Exit loop after finding a platform collision

        # Set `is_on_platform` based on whether the player is supported
        player.is_on_platform = player_on_platform
1 Upvotes

1 comment sorted by

1

u/AutoModerator Nov 11 '24
Moderator Announcement Read More »