I'm trying to make multiple platforms (blocks) on screen on which player can jump and land on it. When I just keep only one block, player jumps on it and land easily. But when I create other platform and player jumps, it gets hit by the block and goes below on the map. But if I try to jump over other platform, it lands on the surface easily.

What I tried so far,

I forgot to add these lines,

        # Checking Collision of Platform and Hero
    self.obstacleHit_or_not(self.hero, self.platform, 460)
    self.obstacleHit_or_not(self.hero, self.platform1, 460)
    self.obstacleHit_or_not(self.hero, self.platform2, 460)

    # This function works for Blocks only
def obstacleHit_or_not(self, hero, platform, position):
    hit = pygame.sprite.collide_rect(hero, platform)

    if hit:
        #print("ITS HITTING")
        hero.y = position
        print("hero.y - ", hero.y)

    elif hero.y == position:
        #print("HITTING INSIDE OF 450")

        while True:
            hero.y += 5
            if hero.y >= 625:
  • \$\begingroup\$ Iterate over each platform, if player hits at least one then stop. Also, your obstacleHit_or_not function does not only check if player hits the platform - it also moves him. Take the movement code outside, leave only collision checking here. \$\endgroup\$
    – crueltear
    Jun 18, 2019 at 10:44
  • \$\begingroup\$ Can you show me by doing changes in my code please? \$\endgroup\$
    – hotstepper
    Jun 18, 2019 at 10:54
  • \$\begingroup\$ check this - pastebin.com/2q8mADuM - its my code in case you need more info \$\endgroup\$
    – hotstepper
    Jun 18, 2019 at 11:00

1 Answer 1


First, instead of storing each platform as a member in class, do it like this:

self.platforms=[Platform(200, 550), Platform(600, 550), Platform(1000, 550)]

It's the list of all platforms in the game. By storing them in the list, you'll be able to easily add or remove them. Do the same for each object that's going to be more than one in the game (coins, enemies, etc).

Now, for the collision checking. Let's change the obstacleHit_or_not method to return True if any of the platform present in self.platforms is hit, False otherwise.

def obstacleHit_or_not(self, hero, platforms):
    for platform in platforms:
        hit = pygame.sprite.collide_rect(hero, platform)
        if hit:
            return True
    return False

Change other collision checking functions to look like this one. If you need the information about which platform has been hit, you can "return platform" if hit, "return None" if not.

Now, depending on what your collision function returns, you'll have to update the player's position/velocity accordingly.

Generally the game loop looks like this:

  1. Get system events (keyboard, mouse, etc)
  2. Get collision information for all moving objects
  3. Update velocity of each moving object, according to collision information
  4. Update the position of each moving object,
  5. Draw the frame

Collision checking should do only that - check if the object is colliding. It's not its job to tell the player to stop phasing through the platform d;

  • \$\begingroup\$ ok boss thanks for helping me out \$\endgroup\$
    – hotstepper
    Jun 18, 2019 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .