diff --git a/game/game.py b/game/game.py index 78f067c..b2d4c97 100644 --- a/game/game.py +++ b/game/game.py @@ -4,7 +4,7 @@ This class is responsible for managing the game's logic. It offers to set a callback function that will be called when waiting for a player input. It provides the gameState object to access the current state of the game. It provides the Map object to access the map. -It provides some stopGame conditions to stop the game like the max number of moves or the player score. +TODO: It provides some stopGame conditions to stop the game like the max number of moves. It provides public methods to start, stop and reset the game. It provides public methods to change the current map. """ @@ -20,16 +20,21 @@ class Game: self.map = map self.gameState = gameState self.waitingForInputCallback = None + self.positionUpdateCallback = None + self.gameStopCallback = None - def set_waiting_for_input_callback(self, callback): + def set_waiting_for_input_callback(self, callback: callable): self.waitingForInputCallback = callback - def set_position_update_callback(self, callback): + def set_position_update_callback(self, callback: callable): self.positionUpdateCallback = callback def _position_update(self): if self.positionUpdateCallback: self.positionUpdateCallback() + + def set_game_stop_callback(self, callback: callable): + self.gameStopCallback = callback def start_game(self): self.gameState.set_stop_game(False) @@ -56,7 +61,7 @@ class Game: if velocity_position: self.gameState.set_player_position(velocity_position) if block_at_velocity: - block_at_velocity.on_collision(self.gameState) # Do something when the player is on the block, like add score or stop the game + block_at_velocity.on_collision(self.gameState) # Do something when the player is on the block, like add coins or stop the game # Check if the player is on a solid block player_position = self.gameState.get_player_position() block_at_player_position = self.map.get_block_at(player_position[0], player_position[1] + 1) @@ -64,7 +69,10 @@ class Game: self.gameState.set_player_velocity(-1) # Stop the player from falling self._update_player_velocity() - + + if self.gameStopCallback and self.gameStopCallback(self.gameState): + self.gameState.set_stop_game(True) + break def _update_player_velocity(self): current_velocity = self.gameState.playerVelocity @@ -102,29 +110,27 @@ class Game: player_position = self.gameState.get_player_position() current_velocity = self.gameState.playerVelocity - if current_velocity == -1: - return None - - if current_velocity == "down": - return (player_position[0], player_position[1] + 1) - - if current_velocity == "left_down": - return (player_position[0] - 1, player_position[1] + 1) - - if current_velocity == "right_down": - return (player_position[0] + 1, player_position[1] + 1) - - if current_velocity == "left": - return (player_position[0] - 1, player_position[1]) - - if current_velocity == "right": - return (player_position[0] + 1, player_position[1]) - - if current_velocity == "left_up": - return (player_position[0] - 1, player_position[1] - 1) - - if current_velocity == "right_up": - return (player_position[0] + 1, player_position[1] - 1) + match current_velocity: + case -1: + return None + case "down": + return (player_position[0], player_position[1] + 1) + case "up": + return (player_position[0], player_position[1] - 1) + case "left_down": + return (player_position[0] - 1, player_position[1] + 1) + case "right_down": + return (player_position[0] + 1, player_position[1] + 1) + case "left": + return (player_position[0] - 1, player_position[1]) + case "right": + return (player_position[0] + 1, player_position[1]) + case "left_up": + return (player_position[0] - 1, player_position[1] - 1) + case "right_up": + return (player_position[0] + 1, player_position[1] - 1) + case _: + return None return None diff --git a/game/map.py b/game/map.py index 15273d9..8c9b5e5 100644 --- a/game/map.py +++ b/game/map.py @@ -16,6 +16,7 @@ class Map: self.width = int(map_data.get('width', 0)) self.height = int(map_data.get('height', 0)) self.difficulty = float(map_data.get('difficulty', 0.0)) + self.min_moves = map_data.get('min_moves', None) self.layout = map_data.get('layout', []) def get_block_at(self, x, y) -> Block | None: @@ -38,6 +39,9 @@ class Map: return block.type return None + def get_min_moves(self): + return self.min_moves + def to_string_dict(self): """ Returns a string matrix representation of the map layout. diff --git a/game/mapReader.py b/game/mapReader.py index 4572bed..b936a6f 100644 --- a/game/mapReader.py +++ b/game/mapReader.py @@ -36,6 +36,9 @@ class MapReader: 'layout': self._parse_layout(config.get('layout', '')) } + if config.get('min_moves') is not None: + map_data['min_moves'] = config.get('min_moves') + return Map(map_data) def _get_block_characters_mapping(self): diff --git a/maps/default.toml b/maps/default.toml index 565f047..2b0146e 100644 --- a/maps/default.toml +++ b/maps/default.toml @@ -1,6 +1,7 @@ width = 12 height = 6 difficulty = 10 +min_moves = 6 layout = """\ ############ # E#