0
votes
def checkCollision(self):
        for entity in self.Entities:
            ox = entity.x
            oy = entity.y
            for block in self.Blocks:

                if block.y < entity.y and entity.y < block.y + block.size:
                    if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x):
                        print ("Hit Bottom")
                        entity.tpEntity(ox, oy+0.5)
                elif entity.y < block.y and block.y < entity.y+entity.height:
                    if (entity.x < block.x and block.x < entity.x+entity.width) or (entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x):
                        print ("Hit Top")
                        entity.tpEntity(ox, oy-self.gravity)
                elif entity.x < block.x and block.x < entity.x+entity.width:
                    if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height):
                        print("Hit Left")
                        entity.tpEntity(ox-0.5,oy)
                elif entity.x+entity.width > block.x+block.size and block.x+block.size > entity.x:
                    if (block.y < entity.y and entity.y < block.y + block.size) or (entity.y < block.y and block.y < entity.y+entity.height):
                        print("Hit Right")
                        entity.tpEntity(ox+0.5,oy)

i have this odd bug, if i walk to a wall, it always registers it as if its on top of it. can anyone explain to me why this happens, and how i should/could fix it? Thank you :)

EDIT: The self.gravity variable is the gravity on the planet/map, as its supposed to be diffrent on each planet/map. By default(here) its on 0.2.

1
It would be a bit difficult for me to chase down the bug since it is a logic error. A good strategy for hunting this bug down would be printing all of the x and y values within each conditional block. This would help you understand what block.x, entity.x, block.y, etc. are equal to when you get that error. So when you walk to a wall, you can check whether or not they satisfy the if/elif statement that was intended.Kevin Welch
All the things like item.x + item.width can be simplified and made much more readable and understandable so you can easily identify your problem. any rect object has attributes for topleft, topright, bottomleft, etc. Check out the rect page pygame.org/docs/ref/rect.htmlKeatinge
I STRONGLY suggest you use pygame.sprite.collideany method right here. It will do this entire chunk of code in 1 simple line.David Jay Brady

1 Answers

0
votes

I am assuming Entities and Blocks are classes? If you set each one with a pygame.Rect object, and update it when the Entities move, you can simplify this by doing:

def checkCollision(self):
    for entity in self.Entities:
        ox = entity.x
        oy = entity.y
        for block in self.Blocks:
            if entity.rect.colliderect(block.rect):

and then test the x and y positions to determine reflection.