0

Collision bug in player movement — not sure what's wrong

Hey everyone I’ve been working on a small 2D platformer using Pygame — just a personal project for now. Right now I’m trying to implement basic movement and collision with platforms. Most things are working, but for some reason my player keeps “sinking” slightly into the platform before stopping. It’s not a full-through fall, just 1–2 pixels inside. Been staring at this too long, maybe someone can spot what I’m missing. import pygame pygame.init() WIDTH, HEIGHT = 800, 600 win = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() player = pygame.Rect(100, 100, 50, 50) platform = pygame.Rect(0, 500, 800, 50) velocity_y = 0 gravity = 0.5 jump = -10 on_ground = False run = True while run: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] and on_ground: velocity_y = jump velocity_y += gravity player.y += int(velocity_y) if player.colliderect(platform): player.bottom = platform.top velocity_y = 0 on_ground = True else: on_ground = False win.fill((30, 30, 30)) pygame.draw.rect(win, (0, 255, 0), platform) pygame.draw.rect(win, (255, 0, 0), player) pygame.display.update() Let me know if anything obvious jumps out!!!

16th Jul 2025, 11:16 PM
JohnSmith
3 Antworten
+ 2
Ur code seems right mate. And i dont see any overlay at all. But even if there is, its not much of an issue. The sprites for such games usually have a small overlay to prevent this as well as other weird things. It could be like grass or maybe fog or smth. Point is, once u put on sprites it shouldnt be visible. Check out Hollow Knight, even a very large overlay looks good there.
17th Jul 2025, 7:21 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
not sure. Looks ok to me. I don't see the problem you're describing. how about drawing the player before the platform? in the bottom of your code # reverse these: pygame.draw.rect(......., player) pygame draw.rect(........, platform) pygame.display.update() this way, the platform will cover the imperfections. perhaps it's an anti-aliasing issue.
17th Jul 2025, 7:22 AM
Bob_Li
Bob_Li - avatar
0
The sinking issue in your Pygame platformer is likely due to the order of operations in your game loop. You're updating the player's position (player.y += int(velocity_y)) before checking for collision and correcting the position (player.bottom = platform.top). This means the player has already moved 1-2 pixels into the platform before you detect the collision and snap them back. To fix this, you should check for collision and adjust the player's position before updating their Y-coordinate based on velocity, or, more commonly, handle the collision response immediately after the movement that causes the overlap, ensuring the player is perfectly aligned. Additionally, ensure that on_ground is only set to True when the player is actually on the platform and not just colliding in general (e.g., from the side).
17th Jul 2025, 8:47 AM
Shirley Richards