Added ap min_moves, gameStop callback and optimizations

This commit is contained in:
2026-10-10 15:29:20 +02:00
parent 10a3d93821
commit 14a39b7154
4 changed files with 42 additions and 28 deletions
+34 -28
View File
@@ -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
+4
View File
@@ -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.
+3
View File
@@ -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):