Update player.py
parent
63be944e58
commit
f4bc953538
15
player.py
15
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
|
||||
|
||||
# ==========================================================
|
||||
|
|
|
|||
Loading…
Reference in New Issue