Added ap min_moves, gameStop callback and optimizations
This commit is contained in:
+34
-28
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user