Exemplo n.º 1
0
    def test_process_input_matching_multiple_commands_ignored(self):
        location_one = Location('L1')
        location_two = Location('L2')
        location_one.add_one_way_exit(Direction('quick'), location_two)
        game = Game([location_one, location_two])

        game.process_input('q')
Exemplo n.º 2
0
    def test_find_location(self):
        name = 'Arbitrary name'
        description = 'Arbitrary description'
        game = Game([Location(name, description)])

        location = game.find_location(name)

        self.assertEqual(description, location.description)
Exemplo n.º 3
0
    def test_movement(self):
        location_one = Location('L1')
        location_two = Location('L2')
        location_one.add_one_way_exit(Direction('west'), location_two)
        game = Game([location_one, location_two])

        game.process_input('w')

        self.assertEqual('L2', game.character.current_location.name)
Exemplo n.º 4
0
    def _arbitrary_game(self, quit_called={}):
        game = Game([Location('L1')])

        def quit_handler(display_handler, input_handler):
            quit_called['yes'] = True
            return True

        game.quit_handler = quit_handler
        return game
Exemplo n.º 5
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'])
Exemplo n.º 6
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()
# 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()