Update player.py

main
Rihards Ševčuks 2026-02-25 06:26:24 +00:00
parent 30b14294cf
commit e2c7506398
1 changed files with 48 additions and 1 deletions

View File

@ -3,7 +3,7 @@ from settings import *
class Player: class Player:
def __init__(self, x, y): def __init__(self, x, y, weapon_manager=None):
# ========================= # =========================
# POSITION # POSITION
# ========================= # =========================
@ -49,6 +49,8 @@ class Player:
self.knockback_timer = 0 self.knockback_timer = 0
self.invulnerable_timer = 0 self.invulnerable_timer = 0
self.weapon_manager = weapon_manager
# ========================= # =========================
# STATE # STATE
# ========================= # =========================
@ -69,11 +71,28 @@ class Player:
self.rect.x = round(self.pos.x) self.rect.x = round(self.pos.x)
self.handle_horizontal_collisions(world) 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 ---- # ---- Vertical ----
self.pos.y += self.velocity.y * dt self.pos.y += self.velocity.y * dt
self.rect.y = round(self.pos.y) self.rect.y = round(self.pos.y)
self.handle_vertical_collisions(world) 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() self.update_state()
# ========================================================== # ==========================================================
@ -222,3 +241,31 @@ class Player:
if SHOW_COLLIDERS: if SHOW_COLLIDERS:
pygame.draw.rect(screen, (0, 255, 0), draw_rect, 2) 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)