Update player.py

main
Filips Kalniņš 2026-02-11 07:53:30 +00:00
parent 63be944e58
commit f4bc953538
1 changed files with 6 additions and 9 deletions

View File

@ -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
# ==========================================================