diff --git a/player.py b/player.py index b29e8e4..808e507 100644 --- a/player.py +++ b/player.py @@ -77,29 +77,26 @@ class Player: def handle_collisions(self, world): self.on_ground = False - # Horizontal collisions - for tile in world.get_nearby_tiles(self.rect): - if tile["rect"].colliderect(self.rect) and tile["solid"]: + # Get nearby tiles once + nearby_tiles = world.get_nearby_tiles(self.rect) + # Horizontal collisions + for tile in nearby_tiles: + if tile["rect"].colliderect(self.rect) and tile["solid"]: if self.velocity.x > 0: # Moving right self.rect.right = tile["rect"].left - elif self.velocity.x < 0: # Moving left self.rect.left = tile["rect"].right - self.velocity.x = 0 # Vertical collisions - for tile in world.get_nearby_tiles(self.rect): + for tile in nearby_tiles: if tile["rect"].colliderect(self.rect) and tile["solid"]: - if self.velocity.y > 0: # Falling self.rect.bottom = tile["rect"].top self.on_ground = True - elif self.velocity.y < 0: # Jumping up self.rect.top = tile["rect"].bottom - self.velocity.y = 0 # ==========================================================