Search code examples
pythonpygamecollision-detection

how can I wok my hitbox when it collides with something on top of it


Im making a game which is meant to be complete chaos and one of the things I did was put falling lava. but My problem is with the collision detection.

if Falling_Lava.y - Falling_Lava.width < man.hitbox[1] + man.hitbox[3] and Falling_Lava.y + Falling_Lava.width > man.hitbox[1]:
        if Falling_Lava.x + Falling_Lava.width > Falling_Lava.x and Falling_Lava.width < man.hitbox[0] + man.hitbox[2]:
            pygame.time.delay(10)
            points -= 5
            man.hit()


if Falling_Lava1.y - Falling_Lava1.width < man.hitbox[1] + man.hitbox[3] and Falling_Lava1.y + Falling_Lava1.width > man.hitbox[1]:
        if Falling_Lava1.x + Falling_Lava1.width > Falling_Lava1.x and Falling_Lava1.width < man.hitbox[0] + man.hitbox[2]:
            pygame.time.delay(10)
            points -= 5
            man.hit()

What Happens is when the falling lava gets to the to the top of your y coordinate it starts diminishing your health even if your x coordinte is not equal to the falling lava's x

What I said explained

and I don't think its anything wrong with my player hitbox because it works perfectly when you get hit by the zombie

def __init__(self, x, y, width, height):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.velo = 8
    self.left = False
    self.right = False
    self.walkcount = 0
    self.Idlecount = 0
    self.guncount = 0
    self.gunisfired = False
    self.isIdle = False
    self.standing = True
    self.punchislaunched = False
    self.punchcount = 0
    self.hitbox = (self.x + 20, self.y, 28, 60)
    self.health = 10

Can someone explain this please

In case you need the full code, go to my GitHub


Solution

  • It looks like your collision checks are a bit messed up. In the x-value if check, you put Falling_Lava.x + Falling_Lava.width > Falling_Lava.x and Falling_Lava.width < man.hitbox[0] + man.hitbox[2], where you compare Falling_Lava to itself. try replacing Falling_Lava.x with man.hitbox[0]. I'm not exactly sure if that's the right fix, but I hope this helps.

    Edit:

    Here is the code you wrote. Everything looks fine except for the 2nd line's first comparison.

    if Falling_Lava.y - Falling_Lava.width < man.hitbox[1] + man.hitbox[3] and Falling_Lava.y + Falling_Lava.width > man.hitbox[1]:
        if Falling_Lava.x + Falling_Lava.width > Falling_Lava.x and Falling_Lava.width < man.hitbox[0] + man.hitbox[2]:
            pygame.time.delay(10)
            points -= 5
            man.hit()
    

    Corrected example, changing Falling_Lava.x to man.hitbox[0] (remove the arrows in the actual code, just to point out where...):

    if Falling_Lava.y - Falling_Lava.width < man.hitbox[1] + man.hitbox[3] and Falling_Lava.y + Falling_Lava.width > man.hitbox[1]:
        # Try replacing this line!
        if Falling_Lava.x + Falling_Lava.width > --> man.hitbox[0] <-- and Falling_Lava.width < man.hitbox[0] + man.hitbox[2]:
            pygame.time.delay(10)
            points -= 5
            man.hit()