Added ap min_moves, gameStop callback and optimizations
This commit is contained in:
+25
-19
@@ -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 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 gameState object to access the current state of the game.
|
||||||
It provides the Map object to access the map.
|
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 start, stop and reset the game.
|
||||||
It provides public methods to change the current map.
|
It provides public methods to change the current map.
|
||||||
"""
|
"""
|
||||||
@@ -20,17 +20,22 @@ class Game:
|
|||||||
self.map = map
|
self.map = map
|
||||||
self.gameState = gameState
|
self.gameState = gameState
|
||||||
self.waitingForInputCallback = None
|
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
|
self.waitingForInputCallback = callback
|
||||||
|
|
||||||
def set_position_update_callback(self, callback):
|
def set_position_update_callback(self, callback: callable):
|
||||||
self.positionUpdateCallback = callback
|
self.positionUpdateCallback = callback
|
||||||
|
|
||||||
def _position_update(self):
|
def _position_update(self):
|
||||||
if self.positionUpdateCallback:
|
if self.positionUpdateCallback:
|
||||||
self.positionUpdateCallback()
|
self.positionUpdateCallback()
|
||||||
|
|
||||||
|
def set_game_stop_callback(self, callback: callable):
|
||||||
|
self.gameStopCallback = callback
|
||||||
|
|
||||||
def start_game(self):
|
def start_game(self):
|
||||||
self.gameState.set_stop_game(False)
|
self.gameState.set_stop_game(False)
|
||||||
|
|
||||||
@@ -56,7 +61,7 @@ class Game:
|
|||||||
if velocity_position:
|
if velocity_position:
|
||||||
self.gameState.set_player_position(velocity_position)
|
self.gameState.set_player_position(velocity_position)
|
||||||
if block_at_velocity:
|
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
|
# Check if the player is on a solid block
|
||||||
player_position = self.gameState.get_player_position()
|
player_position = self.gameState.get_player_position()
|
||||||
block_at_player_position = self.map.get_block_at(player_position[0], player_position[1] + 1)
|
block_at_player_position = self.map.get_block_at(player_position[0], player_position[1] + 1)
|
||||||
@@ -65,6 +70,9 @@ class Game:
|
|||||||
|
|
||||||
self._update_player_velocity()
|
self._update_player_velocity()
|
||||||
|
|
||||||
|
if self.gameStopCallback and self.gameStopCallback(self.gameState):
|
||||||
|
self.gameState.set_stop_game(True)
|
||||||
|
break
|
||||||
|
|
||||||
def _update_player_velocity(self):
|
def _update_player_velocity(self):
|
||||||
current_velocity = self.gameState.playerVelocity
|
current_velocity = self.gameState.playerVelocity
|
||||||
@@ -102,29 +110,27 @@ class Game:
|
|||||||
player_position = self.gameState.get_player_position()
|
player_position = self.gameState.get_player_position()
|
||||||
current_velocity = self.gameState.playerVelocity
|
current_velocity = self.gameState.playerVelocity
|
||||||
|
|
||||||
if current_velocity == -1:
|
match current_velocity:
|
||||||
|
case -1:
|
||||||
return None
|
return None
|
||||||
|
case "down":
|
||||||
if current_velocity == "down":
|
|
||||||
return (player_position[0], player_position[1] + 1)
|
return (player_position[0], player_position[1] + 1)
|
||||||
|
case "up":
|
||||||
if current_velocity == "left_down":
|
return (player_position[0], player_position[1] - 1)
|
||||||
|
case "left_down":
|
||||||
return (player_position[0] - 1, player_position[1] + 1)
|
return (player_position[0] - 1, player_position[1] + 1)
|
||||||
|
case "right_down":
|
||||||
if current_velocity == "right_down":
|
|
||||||
return (player_position[0] + 1, player_position[1] + 1)
|
return (player_position[0] + 1, player_position[1] + 1)
|
||||||
|
case "left":
|
||||||
if current_velocity == "left":
|
|
||||||
return (player_position[0] - 1, player_position[1])
|
return (player_position[0] - 1, player_position[1])
|
||||||
|
case "right":
|
||||||
if current_velocity == "right":
|
|
||||||
return (player_position[0] + 1, player_position[1])
|
return (player_position[0] + 1, player_position[1])
|
||||||
|
case "left_up":
|
||||||
if current_velocity == "left_up":
|
|
||||||
return (player_position[0] - 1, player_position[1] - 1)
|
return (player_position[0] - 1, player_position[1] - 1)
|
||||||
|
case "right_up":
|
||||||
if current_velocity == "right_up":
|
|
||||||
return (player_position[0] + 1, player_position[1] - 1)
|
return (player_position[0] + 1, player_position[1] - 1)
|
||||||
|
case _:
|
||||||
|
return None
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class Map:
|
|||||||
self.width = int(map_data.get('width', 0))
|
self.width = int(map_data.get('width', 0))
|
||||||
self.height = int(map_data.get('height', 0))
|
self.height = int(map_data.get('height', 0))
|
||||||
self.difficulty = float(map_data.get('difficulty', 0.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', [])
|
self.layout = map_data.get('layout', [])
|
||||||
|
|
||||||
def get_block_at(self, x, y) -> Block | None:
|
def get_block_at(self, x, y) -> Block | None:
|
||||||
@@ -38,6 +39,9 @@ class Map:
|
|||||||
return block.type
|
return block.type
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_min_moves(self):
|
||||||
|
return self.min_moves
|
||||||
|
|
||||||
def to_string_dict(self):
|
def to_string_dict(self):
|
||||||
"""
|
"""
|
||||||
Returns a string matrix representation of the map layout.
|
Returns a string matrix representation of the map layout.
|
||||||
|
|||||||
@@ -36,6 +36,9 @@ class MapReader:
|
|||||||
'layout': self._parse_layout(config.get('layout', ''))
|
'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)
|
return Map(map_data)
|
||||||
|
|
||||||
def _get_block_characters_mapping(self):
|
def _get_block_characters_mapping(self):
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
width = 12
|
width = 12
|
||||||
height = 6
|
height = 6
|
||||||
difficulty = 10
|
difficulty = 10
|
||||||
|
min_moves = 6
|
||||||
layout = """\
|
layout = """\
|
||||||
############
|
############
|
||||||
# E#
|
# E#
|
||||||
|
|||||||
Reference in New Issue
Block a user