From 7fbed2f212478ccddccc9780df1bb143b315e4f4 Mon Sep 17 00:00:00 2001 From: Ren Date: Sun, 15 Jun 2025 12:41:42 +0200 Subject: [PATCH] fix players a npc --- game.py | 64 ++++++++++++++++++++++++++++++++------------------------- npc.py | 16 ++++++++------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/game.py b/game.py index 405da66..91f8f86 100644 --- a/game.py +++ b/game.py @@ -10,7 +10,6 @@ from inputs import InputManager, PLAYER_KEYMAPS from player import Player from npc import NPC -from ui import show_end_message from config import GAME_FPS @@ -67,22 +66,28 @@ def try_spawn_npc(self): # Nastav nový random interval pro další spawn self.next_spawn_interval = random.randint(*self.spawn_interval_range) - def try_npc_shoot(self, npc): now = pygame.time.get_ticks() npc_id = id(npc) - last_shot = self.npc_last_shot_times.get(npc_id, 0) - if now - last_shot >= self.npc_shoot_cooldown: - target = npc.get_shot_target(self.players.values()) - if target: - bullet = Bullet( - npc.rect.centerx, npc.rect.centery, - target.rect.centerx, target.rect.centery, - color=(255, 0, 0), - shooter=npc - ) - self.npc_bullets.append(bullet) - self.npc_last_shot_times[npc_id] = now + + if now - self.npc_last_shot_times.get(npc_id, 0) < self.npc_shoot_cooldown: + return + + target = npc.get_shot_target( + [p for p in self.players.values() if p.health > 0] + ) + + if not target: + return + + bullet = Bullet( + npc.rect.centerx, npc.rect.centery, + target.rect.centerx, target.rect.centery, + color=(255, 0, 0), + shooter=npc + ) + self.npc_bullets.append(bullet) + self.npc_last_shot_times[npc_id] = now def check_bullet_collisions(self): @@ -127,13 +132,11 @@ def handle_key_events(self): def update_players(self, **kwargs): """Handles players updates""" - for player in self.players.values(): - + if player.health <= 0: + continue inputs = self.input_manager.get_inputs(player.uid) - - player.update(inputs = inputs, **kwargs) @@ -145,17 +148,22 @@ def update_players(self, **kwargs): self.input_manager.clear_inputs(player.uid) - - - - def update_npcs(self, **kwargs): + def update_npcs(self, **kwargs): """Handles npcs updates""" - - for npc in self.npcs: - npc.update(players = self.players, **kwargs) + for npc in self.npcs[:]: + npc.update(players=self.players, **kwargs) self.try_npc_shoot(npc) + for player in self.players.values(): + if player.health <= 0: + continue + + if npc.rect.colliderect(player.rect): + player.health -= npc.damage * 2 + self.npcs.remove(npc) + break + @@ -163,9 +171,9 @@ def render_all(self): """Draws itself""" self.screen.fill((0, 0, 0)) - for player in self.players.values(): - player.draw(self.screen) + if player.health > 0: + player.draw(self.screen) for npc in self.npcs: @@ -196,7 +204,7 @@ def update_bullets(self): def check_game_end(self): alive_players = [p for p in self.players.values() if p.health > 0] if not alive_players: - if self.score >= 100: + if self.score >= 200: self.game_result = "win" else: self.game_result = "lost" diff --git a/npc.py b/npc.py index ffe508b..b53674c 100644 --- a/npc.py +++ b/npc.py @@ -29,15 +29,15 @@ def __init__(self, x, y,npc_type="medium"): self.damage = npc_config["damage"] self.color = npc_config["color"] - - - def update(self, **kwargs): """Updates self position according to players""" - nearest_player = self.find_closest_player(self, kwargs["players"].values()) - + all_players = kwargs["players"].values() + living_players = [p for p in all_players if p.health > 0] + if not living_players: + return + nearest_player = self.find_closest_player(self, living_players) if self.rect.x < nearest_player.rect.x: self.rect.x += NPC_SPEED @@ -48,9 +48,11 @@ def update(self, **kwargs): elif self.rect.y > nearest_player.rect.y: self.rect.y -= NPC_SPEED - def get_shot_target(self, players): - return self.find_closest_player(self, players) + living_players = [p for p in players if p.health > 0] + if not living_players: + return None + return self.find_closest_player(self, living_players) def draw_lifebar(self, screen):