def play_maze(self): os.system('cls' if os.name == 'nt' else 'clear') if self.field == None: print("There is no maze currently loaded.") input("Press Enter to continue...") return self.player = Player() self.field.enter(self.player) self.is_in_maze = True while self.is_in_maze: os.system('cls' if os.name == 'nt' else 'clear') print(self.field.render()) print() start = self.field.start_block blocks = [block for row in self.field.blocks for block in row] ends = [block for block in blocks if isinstance(block, Portal)] print("Location of start (A) = {0}".format( f"(row {start.y + 1}, col {start.x + 1})")) print("Location of End (B) = {0}".format(', '.join([ f"(row {block.y + 1}, col {block.x + 1})" for block in ends ]))) def move(player, direction): if not self.field.move(player, direction): print("Invalid movement entered in game. Please try again") time.sleep(1) menu = Menu([ Option("w", "Move up", lambda: move(self.player, Direction.up)), Option("a", "Move left", lambda: move(self.player, Direction.left)), Option("s", "Move down", lambda: move(self.player, Direction.down)), Option("d", "Move right", lambda: move(self.player, Direction.right)), Option("m", "Return to menu", lambda: self.end_play_maze()), ]) selection = input( "Use the WASD to move or M key to return to menu: ") menu.select(selection) if self.player.is_finished: print() print("You have completed the maze, congratulations!") input("Press Enter to continue...") self.end_play_maze()
def start(self): while not self.ended: os.system('cls' if os.name == 'nt' else 'clear') self.field.enter(Player()) print(self.field.render()) print() print("Configuration Menu") print("==================") menu = Menu([ Option("1", "Create wall", self.create_wall), Option("2", "Create passageway", self.create_passageway), Option("3", "Create start point", self.create_start_point), Option("4", "Create end point", self.create_end_point), Option("0", "Exit to Main Menu", lambda: self.end()), ]) print(menu.render()) selection = input("Enter your option: ") menu.select(selection)
def start(self): menu = Menu([ Option("1", "Read and load maze from file", self.load_maze), Option("2", "View maze", self.view_maze), Option("3", "Play maze game", self.play_maze), Option("4", "Configure current maze", self.edit_maze), Option("0", "Exit maze", self.end), ]) while not self.ended: os.system('cls' if os.name == 'nt' else 'clear') print("Main menu") print("=========") print(menu.render()) if not menu.select(input("Enter your option: ")): print("Invalid menu option, try again!") input("Press Enter to continue...") print()
def test_menu_select(): option = Option("1", "Select!", _option_select) menu = Menu([option]) assert menu.select("1") assert menu.select("0") == False