From a17928671866f4caed79c542fca19fe9c8fdc122 Mon Sep 17 00:00:00 2001 From: Ren Date: Sun, 15 Jun 2025 11:39:15 +0200 Subject: [PATCH] upd win/lost --- game.py | 24 +++++++++--------------- main.py | 16 +++++++++++++--- ui.py | 9 +++++++++ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/game.py b/game.py index 32d95a5..405da66 100644 --- a/game.py +++ b/game.py @@ -10,6 +10,7 @@ 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 @@ -44,6 +45,8 @@ def __init__(self, **kwargs): self.last_spawn_time = pygame.time.get_ticks() self.spawn_interval_range = (3000, 5000) + self.game_result = None + def spawn_random_npc(self): npc_type = random.choices( @@ -76,7 +79,7 @@ def try_npc_shoot(self, npc): npc.rect.centerx, npc.rect.centery, target.rect.centerx, target.rect.centery, color=(255, 0, 0), - shooter=npc # tady přidáš referenci na střílejícího NPC + shooter=npc ) self.npc_bullets.append(bullet) self.npc_last_shot_times[npc_id] = now @@ -193,21 +196,11 @@ 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: - self.running = False - if self.score >= 200: - self.display_end_message("You win!") + if self.score >= 100: + self.game_result = "win" else: - self.display_end_message("You lost.") - - def display_end_message(self, message): - font = pygame.font.SysFont(None, 60) - text = font.render(message, True, (255, 255, 255)) - rect = text.get_rect(center=(self.screen.get_width() // 2, self.screen.get_height() // 2)) - self.screen.fill((0, 0, 0)) - self.screen.blit(text, rect) - pygame.display.flip() - pygame.time.wait(3000) - + self.game_result = "lost" + self.running = False def run(self): """Running loop""" @@ -233,6 +226,7 @@ def run(self): print("Closing game ....") + return self.game_result diff --git a/main.py b/main.py index 0d193d1..c0fd9b6 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ from enums import GameType from game import SingleGame, CoopGame -from ui import show_menu, show_splash +from ui import show_menu, show_splash, show_end_message from config import SCREEN_WIDTH, SCREEN_HEIGHT pygame.init() @@ -24,11 +24,21 @@ def main(): if choice == GameType.SINGLE: game = SingleGame(screen=screen) - game.run() + result = game.run() + if result == "win": + show_end_message(screen, "You win!") + elif result == "lost": + show_end_message(screen, "You lost.") if choice == GameType.COOP: game = CoopGame(screen=screen) - game.run() + result = game.run() + if result == "win": + show_end_message(screen, "You win!") + elif result == "lost": + show_end_message(screen, "You lost.") + else: + show_end_message(screen, "Game ended.") elif choice == "quit": break diff --git a/ui.py b/ui.py index e0149c0..ebbfdcd 100644 --- a/ui.py +++ b/ui.py @@ -59,3 +59,12 @@ def show_splash(screen): if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: return + +def show_end_message(screen, message): + font = pygame.font.SysFont(None, 60) + text = font.render(message, True, (255, 255, 255)) + rect = text.get_rect(center=(screen.get_width() // 2, screen.get_height() // 2)) + screen.fill((0, 0, 0)) + screen.blit(text, rect) + pygame.display.flip() + pygame.time.wait(3000)