Beispiel #1
0
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()
Beispiel #2
0
    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'])