From e2c75063981252056d19197df24ba043ae2b701d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rihards=20=C5=A0ev=C4=8Duks?= Date: Wed, 25 Feb 2026 06:26:24 +0000 Subject: [PATCH] Update player.py --- player.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/player.py b/player.py index 7bf47da..bf738d1 100644 --- a/player.py +++ b/player.py @@ -3,7 +3,7 @@ from settings import * class Player: - def __init__(self, x, y): + def __init__(self, x, y, weapon_manager=None): # ========================= # POSITION # ========================= @@ -49,6 +49,8 @@ class Player: self.knockback_timer = 0 self.invulnerable_timer = 0 + self.weapon_manager = weapon_manager + # ========================= # STATE # ========================= @@ -69,11 +71,28 @@ class Player: self.rect.x = round(self.pos.x) self.handle_horizontal_collisions(world) + # World border check + if self.rect.left < 0: + self.rect.left = 0 + self.pos.x = 0 + self.velocity.x = 0 + if self.rect.right > world.width * TILE_SIZE: + self.rect.right = world.width * TILE_SIZE + self.pos.x = self.rect.x + self.velocity.x = 0 + # ---- Vertical ---- self.pos.y += self.velocity.y * dt self.rect.y = round(self.pos.y) self.handle_vertical_collisions(world) + # World border - bottom + if self.rect.bottom > world.height * TILE_SIZE: + self.rect.bottom = world.height * TILE_SIZE + self.pos.y = self.rect.y + self.on_ground = True + self.velocity.y = 0 + self.update_state() # ========================================================== @@ -220,5 +239,33 @@ class Player: pygame.draw.rect(screen, color, draw_rect) + if SHOW_COLLIDERS: + pygame.draw.rect(screen, (0, 255, 0), draw_rect, 2) + + # Draw health bar + health_bar_width = 28 + health_bar_height = 4 + health_percent = self.health / self.max_health + + health_bar_rect = pygame.Rect( + draw_rect.x, + draw_rect.y - 8, + health_bar_width * health_percent, + health_bar_height + ) + pygame.draw.rect(screen, (0, 255, 0), health_bar_rect) + pygame.draw.rect(screen, (255, 0, 0), pygame.Rect( + draw_rect.x, + draw_rect.y - 8, + health_bar_width, + health_bar_height + ), 1) + if self.invulnerable_timer > 0: + color = (255, 255, 255) + else: + color = (255, 50, 50) + + pygame.draw.rect(screen, color, draw_rect) + if SHOW_COLLIDERS: pygame.draw.rect(screen, (0, 255, 0), draw_rect, 2) \ No newline at end of file