0

I have this collision detection code but I have problem when i try to call this function later in my program it works, but I don't get any collision.

def collisionTest(rect, tiles):
    collisionList = []
    for tile in tiles:
        if rect.colliderect(tile):
            collisionList.append(tile)
    return collisionList

def move(rect, movement, tiles):
    collisionAreas = {'top': False, 'bottom': False, 'right': False, 'left': False} # all the areas we are trying to find collision for
    rect.x += movement[0] # collision on the x axis
    collisionList = collisionTest(rect, tiles)
    for tile in collisionList:
        if movement[0] > 0:
            rect.right = tile.left # player on the right tile on the left
            collisionAreas['right'] = True # there is collision on the right of the player
        
        elif movement[0] < 0:
            rect.left = tile.right # player on the left tile on the right
            collisionAreas['left'] = True # there is collision on the right of the player
    rect.y += movement[1] # collision on the y axis
    collisionList = collisionTest(rect, tiles)
    
    for tile in collisionList:            
        if movement[1] > 0:
            rect.bottom = tile.top # bottom of the player collides with the top of the tile
            collisionAreas['bottom'] = True # there is collision on the bottom of the player
        
        elif movement[1] < 0:
            rect.top = tile.bottom # top of the player is colliding with the bottom of the tile
            collisionAreas['top'] = True # there is collision on the top of the player
    
    return rect, collisionAreas #return the player that's moving and the list of collisions that accured
 
  playerRect, collisions = move(playerRect, playerPos, tileRects) # calling the function
  display.blit(spriteSheet, (playerRect.x-scroll[0], playerRect.y-scroll[1]), playerIdle_Rect) # displaying my character
Kingsley
  • 14,398
  • 5
  • 31
  • 53
Karim
  • 1
  • 2

0 Answers0