From 47b2809daa7d7136ad784ee29f91b725e1c15c0f Mon Sep 17 00:00:00 2001 From: Ren Date: Sun, 15 Jun 2025 13:46:06 +0200 Subject: [PATCH] upd target, spawn, score --- bullet.py | 1 + game.py | 44 ++++++++++++++++++++++---------------------- main.py | 8 ++++---- npc.py | 2 +- player.py | 2 +- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/bullet.py b/bullet.py index 12b7bea..de24a15 100644 --- a/bullet.py +++ b/bullet.py @@ -8,6 +8,7 @@ class Bullet: def __init__(self, x, y, target_x, target_y,color=(0, 0, 0),shooter=None): self.rect = pygame.Rect(x, y, BULLET_SIZE, BULLET_SIZE) + self.rect.center = (x, y) angle = math.atan2(target_y - y, target_x - x) self.dx = math.cos(angle) * BULLET_SPEED self.dy = math.sin(angle) * BULLET_SPEED diff --git a/game.py b/game.py index 91f8f86..ebb471d 100644 --- a/game.py +++ b/game.py @@ -46,15 +46,19 @@ def __init__(self, **kwargs): self.game_result = None - def spawn_random_npc(self): - npc_type = random.choices( - ["easy", "medium", "hard"], - weights=[70, 25, 5], - k=1 - )[0] - x = random.randint(50, self.screen.get_width() - 50) - y = random.randint(50, self.screen.get_height() - 50) + npc_type = random.choices(["easy", "medium", "hard"], weights=[70, 25, 5])[0] + + sw, sh = self.screen.get_width(), self.screen.get_height() + margin = 10 + + corners = [ + (margin, margin), # levý horní roh + (sw - margin, margin), # pravý horní roh + (margin, sh - margin), # levý dolní roh + (sw - margin, sh - margin), # pravý dolní roh + ] + x, y = random.choice(corners) self.npcs.append(NPC(x, y, npc_type=npc_type)) @@ -249,12 +253,12 @@ class SingleGame(AbstractGame): def __init__(self, **kwargs): super().__init__(**kwargs) + center_x = self.screen.get_width() // 2 + center_y = self.screen.get_height() // 2 - pl1 = Player(self.PLAYER1) # todo some better init - pl1.set_coords(20,20) + pl1 = Player(self.PLAYER1) + pl1.set_coords(center_x - 30, center_y) self.players[self.PLAYER1] = pl1 - - self.input_manager.add_keymap(self.PLAYER1, PLAYER_KEYMAPS["wasd"]) @@ -286,24 +290,20 @@ class CoopGame(AbstractGame): def __init__(self, **kwargs): super().__init__(**kwargs) + center_x = self.screen.get_width() // 2 + center_y = self.screen.get_height() // 2 pl1 = Player(self.PLAYER1) - pl1.set_coords(20,20) + pl1.set_coords(center_x - 30, center_y) self.players[self.PLAYER1] = pl1 - - self.input_manager.add_keymap(self.PLAYER1, PLAYER_KEYMAPS["wasd"]) - - pl2 = Player(self.PLAYER2) # experimental player 2 - pl2.set_coords(100, 20) - pl2.color = (0,0,255) + pl2 = Player(self.PLAYER2) + pl2.set_coords(center_x + 30, center_y) + pl2.color = (0, 0, 255) self.players[self.PLAYER2] = pl2 - - self.input_manager.add_keymap(self.PLAYER2, PLAYER_KEYMAPS["arrows"]) - self.npcs.append(NPC(400, 400)) diff --git a/main.py b/main.py index c0fd9b6..162cb05 100644 --- a/main.py +++ b/main.py @@ -26,17 +26,17 @@ def main(): game = SingleGame(screen=screen) result = game.run() if result == "win": - show_end_message(screen, "You win!") + show_end_message(screen, f"You win! Score: {game.score}") elif result == "lost": - show_end_message(screen, "You lost.") + show_end_message(screen, f"You lost.Score: {game.score}") if choice == GameType.COOP: game = CoopGame(screen=screen) result = game.run() if result == "win": - show_end_message(screen, "You win!") + show_end_message(screen, f"You win!Score: {game.score}") elif result == "lost": - show_end_message(screen, "You lost.") + show_end_message(screen, f"You lost.Score: {game.score}") else: show_end_message(screen, "Game ended.") diff --git a/npc.py b/npc.py index b53674c..0c71d33 100644 --- a/npc.py +++ b/npc.py @@ -77,7 +77,7 @@ def draw(self, screen): def distance(self, rect1, rect2): """Helper function for distance computing""" - return math.hypot(rect1.x - rect2.x, rect1.y - rect2.y) + return math.hypot(rect1.centerx - rect2.centerx, rect1.centery - rect2.centery) def find_closest_player(self, npc, players): diff --git a/player.py b/player.py index 302ffb1..13db21e 100644 --- a/player.py +++ b/player.py @@ -80,7 +80,7 @@ def update(self, **kwargs): def distance(self, rect1, rect2): """Helper function for distance computing""" - return math.hypot(rect1.x - rect2.x, rect1.y - rect2.y) + return math.hypot(rect1.centerx - rect2.centerx, rect1.centery - rect2.centery) def find_closest_npc(self, npcs):