Click here to Skip to main content
15,794,850 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Describe the problemI'm having trouble with my python platformer collision logic, i've recently started following tutorials of a youtube creator for pygame platformers. And had no trouble in making a platformer until the collision logic. I tried to perfect my code but i'm still having issues;
For example:
1. When i move in any direction while on top of a tile i phase trough the tile vertically.
2. When i land on top of a tile the feet of my character get stuck a few pixels inside the tile.
3. (Not really an issue) I have no idea how to write jumping code, i tried changing the Y Velocity to -5 and then the game glitched out.

My goal with the collision logic was to stop the player when colliding with a block, to be more specific when colliding with a side the player stops.

Project File:

Movement code for the Player:

def move(self):

    self.RECT.x = self.POS[0]
    self.RECT.y = self.POS[1]
    self.POS[0] = self.RECT.x
    self.POS[1] = self.RECT.y

    self.POS[0] += self.VEL[0]

    self.VEL[0] = 0
    if self.DIR["left"]:
        self.VEL[0] = -5

    if self.DIR["right"]:
        self.VEL[0] = 5

    for tile in self.TILE.testCollision(self.RECT):

        if self.VEL[0] > 0:
            self.RECT.left = tile.right

        if self.VEL[0] < 0:
            self.RECT.right = tile.left

    self.POS[1] += self.VEL[1]
    self.VEL[1] += self.GRAVITY

    for tile in self.TILE.testCollision(self.RECT):

        if self.VEL[1] > 0:
            self.VEL[1] = 0
            self.RECT.bottom =

Collision Code for the tiles:

def testCollision(self, rect):

        self.RECT.x = self.POS[0]
        self.RECT.y = self.POS[1]

        hitlist = []

        for tile in self.TILES:
            if rect.colliderect(tile):
        return hitlist

What I have tried:

I have tried rewriting the code multiple times, i have tried watching the tutorials all over again, i have tried asking the web questions and researching.
Updated 27-Aug-20 10:12am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900