def run_maze_game(maze): """ Allows a user to move through a maze. They start at the bottom left (0, 0) and win when they reach the top right. """ game = Game(locations_from_maze(maze)) game.display_handler(render_maze(maze)) # The function which determines whether the game ending # criteria have been met def check_if_end_reached(game): maze_end_location = maze[width-1][height-1] if game.character.current_location.name == maze_end_location.name: game.display_handler('You have reached the end. Well done!') game.should_end = True # Ensure that the game ending check happens at the end of # each iteration of the game loop game.end_of_round_handler = check_if_end_reached game.run()
def test_default_quit_handler_asks_for_confirmation(self): game = Game([Location('L1')]) context = {'requested_quit': False} def display_handler(text): context['displayed'] = text def input_handler(): if not context['requested_quit']: context['requested_quit'] = True return 'q' else: return 'y' game.display_handler = display_handler game.input_handler = input_handler game.run() self.assertEqual('Are you sure you want to quit?', context['displayed'])
# Use case: Run a procedurally defined game # Example: from vengeance.directions import DOWN, IN, WEST from vengeance.game import Direction from vengeance.game import Game from vengeance.game import Location church = Location('A Church', 'Tiny place of worship') crypt = Location('The Crypt', 'Dusty tomb filled with empty sarcophagi') coffin = Location('A Coffin', 'A tight squeeze and pitch dark') cave = Location('A Cave') church.add_exit(DOWN, crypt) crypt.add_one_way_exit(IN, coffin) crypt.add_exit(WEST, cave) game = Game([church, crypt, coffin, cave]) game.run()