from retro.game import Game from treasure import Treasure from trap import Trap class Player: character = "⍾" color = "red" direction_vectors = { "KEY_RIGHT": [1, 0], "KEY_UP": [0, -1], "KEY_LEFT": [-1, 0], "KEY_DOWN": [0, 1], } def __init__(self, position): self.position = position def handle_keystroke(self, keystroke, game): if keystroke.name in self.direction_vectors: vector = self.direction_vectors[keystroke.name] x, y = self.position dx, dy = vector new_position = (x + dx, y + dy) if game.on_board(new_position) and self.is_passable(new_position, game): self.reset_color() self.position = new_position self.handle_collisions(game) def reset_color (self): self.character = "⍾" self.color = "red" def handle_collisions (self, game): agents=game.get_agents_by_position() agents_at_position=agents[self.position] for agent in agents_at_position: if agent!= self: agent.collided_with_player(self, game) def is_passable(self, position, game): agents=game.get_agents_by_position() agents_at_position=agents[position] for agent in agents_at_position: if not agent.passable: return False return True def check_collision(self, agents, game_state): new_agents = [] for agent in agents: if self.position == agent.position: if isinstance(agent, Treasure): game_state['score'] += 10 # Treasure is collected, score 10 points, don't add it to new_agents self.character = '▓' # Change character to block self.color = 'yellow' # Change color to yellow elif isinstance(agent, Trap): game_state['lives'] -= 1 # Trap is triggered, 1 live is used, don't add it to new_agents self.character = '▓' # Change character to block self.color = 'red' # Change color to red else: new_agents.append(agent) # No collision, keep the agent # Update the original list with remaining agents agents[:] = new_agents # Check for game-ending conditions if game_state['score'] >= 60 or game_state['lives'] <= 0: return True # Return True to indicate the game should end # Reset player character and color if no collision self.character = "⍾" self.color = "red" return False # Return False to continue the game """When player hits a Treasure, character changes to ‘▓’ and color changes to yellow. When player hits a Trap, character changes to ‘▓’ and color changes to red. If there's no collision, the player character and color are reset to the default values"""