### Start Adventurelib Project Source: https://github.com/lordmauve/adventurelib/blob/master/doc/intro.rst Basic setup for an adventurelib project. Imports necessary functions and starts the game loop. ```python from adventurelib import * start() ``` -------------------------------- ### Run Adventurelib Game Source: https://github.com/lordmauve/adventurelib/blob/master/doc/intro.rst Command to execute an adventurelib game from the terminal. ```bash python3 my_game.py ``` -------------------------------- ### Use say() for Formatted Text Source: https://github.com/lordmauve/adventurelib/blob/master/doc/intro.rst Demonstrates using the adventurelib say() function for multi-line text output with automatic wrapping. ```python @when("brush teeth") def brush_teeth(): say(""" You squirt a bit too much toothpaste onto your brush and dozily jiggle it round your mouth. """) ``` -------------------------------- ### Add Custom Command Source: https://github.com/lordmauve/adventurelib/blob/master/doc/intro.rst Defines a new player command 'brush teeth' using the @when decorator and prints a response. ```python @when("brush teeth") def brush_teeth(): print("You brush your teeth. They feel clean.") ``` -------------------------------- ### Use say() for Multi-Paragraph Text Source: https://github.com/lordmauve/adventurelib/blob/master/doc/intro.rst Shows how to use say() with multiple paragraphs, separated by blank lines, for richer descriptions. ```python @when("brush teeth") def brush_teeth(): say(""" You squirt a bit too much toothpaste onto your brush and dozily jiggle it round your mouth. Your teeth feel clean and shiny now, as you run your tongue over them. """) ``` -------------------------------- ### Install Adventurelib with Pip Source: https://github.com/lordmauve/adventurelib/blob/master/doc/installing.rst This command installs the Adventurelib library using pip, the standard Python package installer. Ensure you have pip configured correctly in your environment. ```bash pip install adventurelib ``` -------------------------------- ### Example Game Context Transition Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Demonstrates a typical game flow where entering a mirror changes the context, making new commands available (like 'cast SPELL') and hiding others until the context is reset. ```none > enter mirror You step into the silvery surface, which feels wet and cool. You realise that clicking your heels will let you return. > help enter mirror cast SPELL click heels > enter mirror There is no mirror here. > cast fireball You cast the spell. > click heels The moment your heels touch the world rearranges around you. > cast fireball I don't understand 'cast fireball'. > click heels I don't understand 'click heels'. ``` -------------------------------- ### Set and Get Game Context Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Provides examples of using adventurelib.set_context() and adventurelib.get_context() to manage the current game context. This allows for dynamic control over which commands are active. ```python import adventurelib @when('enter mirror') def enter_mirror(): if adventurelib.get_context() == 'wonderland': say('There is no mirror here.') else: adventurelib.set_context('wonderland') say('You step into the silvery surface, which feels wet and cool.') say('You realise that clicking your heels will let you return.') @when('click heels', context='wonderland') def click_heels(spell): adventurelib.set_context(None) say('The moment your heels touch the world rearranges around you.') ``` -------------------------------- ### Install adventurelib using pip Source: https://github.com/lordmauve/adventurelib/blob/master/README.md This command installs the adventurelib library using pip, the Python package installer. Ensure you have pip installed and configured in your environment. ```Shell pip install adventurelib ``` -------------------------------- ### Greedy Algorithm Example (Multi-Word Capture) Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Demonstrates the greedy nature of multi-word captures in Adventurelib. An uppercase placeholder will consume as many words as possible, which can lead to unexpected behavior if not managed carefully. ```Python @when("give ITEM RECIPIENT") # If player types 'give poison apple evil godmother' # item will be 'poison apple evil' # recipient will be 'godmother' ``` -------------------------------- ### Disable AdventureLib Help Command Source: https://github.com/lordmauve/adventurelib/blob/master/doc/customising.rst Demonstrates how to disable the default 'help' and '?' commands in AdventureLib by passing `help=False` to the `start()` function. This is useful for games where players must discover commands through experimentation. ```Python start(help=False) ``` -------------------------------- ### Conditional Command Behavior with Context Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst This Python example illustrates how the same command ('north') can trigger different functions or accept different parameters based on the context. It uses multiple `@when` decorators with varying `context` and `dir` arguments to achieve this conditional behavior. ```python @when('north', dir='north') @when('north', dir='south', context='confused') def go(dir): ... ``` -------------------------------- ### Take Item from Inventory Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Provides an example of a handler function that allows the player to 'eat' an item. It uses the `take` method of the `Bag` to remove the item from the inventory and includes feedback messages. ```Python @when('eat ITEM') def eat(item): obj = inventory.take(item) if not obj: print(f'You do not have a {item}.') else: print(f'You eat the {obj}.') ``` -------------------------------- ### Add Aliases for Movement Commands Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst This example shows how to add shorter alias commands (e.g., 'n' for 'north') to the existing movement functions. This improves user convenience by allowing quicker input for common actions. ```python @when('north', direction='north') @when('south', direction='south') @when('east', direction='east') @when('west', direction='west') @when('n', direction='north') @when('s', direction='south') @when('e', direction='east') @when('w', direction='west') def go(direction): ... ``` -------------------------------- ### Define Item with Grammatical Variations Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Example of creating an Item with multiple names, including one for specific grammatical contexts (e.g., 'the apples'). This is useful for constructing sentences that require definite articles. ```Python apples = Item('some apples', 'apples', 'apple') apples.def_name = 'the apples' ``` -------------------------------- ### Define Commands with @when Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Demonstrates how to define commands using the @when decorator, specifying keywords and arguments that adventurelib will use to parse player input. ```python @when('give ITEM to RECIPIENT') @when('use ITEM on TARGET') @when('hit TARGET with WEAPON') ``` -------------------------------- ### Initialize Player Inventory as a Bag Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Demonstrates the common practice of initializing the player's inventory as an instance of the `Bag` class, which will hold all items the player possesses. ```Python inventory = Bag() ``` -------------------------------- ### Create Adventurelib Rooms Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst Demonstrates how to create Room objects with descriptive text. These rooms form the basis of locations within a text adventure game. ```Python from adventurelib import * space = Room(""" You are drifting in space. It feels very cold. A slate-blue spaceship sits completely silently to your left, its airlock open and waiting. """) spaceship = Room(""" The bridge if the spaceship is shiny and white, with thousands of small, red, blinking lights. """) ``` -------------------------------- ### Handle Multiple Commands with Additional Parameters Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Shows how to use a single handler function for multiple similar commands by passing additional keyword arguments to the @when decorator. These arguments are then passed to the handler function. ```python @when('shout', action='bellow') @when('yell', action='holler') @when('scream', action='shriek') def shout(action): print(f'You {action} loudly.') ``` -------------------------------- ### Model Items in a Room (Python) Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Demonstrates how to represent items present in a room using the Bag class and add them to the room's inventory. It also shows how to define a handler for the 'take ITEM' command, which moves an item from the room to the player's inventory. ```Python chapel.items = Bag([ Item('a golden candlestick', 'candlestick'), ]) @when('take ITEM') def take(item): obj = current_room.items.take(item) if not obj: print(f'There is no {item} here.') else: inventory.add(obj) print(f'You take the {obj}.') ``` -------------------------------- ### Capturing Single Word Arguments Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Binds a command with a placeholder for a single word, capturing that word as a function argument. Uppercase words in the command string act as placeholders. ```Python @when("take THING") def take(thing): print(f"You take the {thing}.") ``` -------------------------------- ### Capturing Multiple Word Arguments Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Binds a command with placeholders for multiple words, capturing them as function arguments. Uppercase words in the command string act as placeholders, and can capture multiple words. ```Python @when("give ITEM to RECIPIENT") def give(item, recipient): print(f"You give the {item} to the {recipient}.") ``` -------------------------------- ### Define an Item with Names Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Demonstrates how to create an Item object with a default name and aliases for user interaction. The default name is used in output, while aliases allow users to refer to the item by different terms. ```Python broom = Item('a broken broom', 'broom') ``` -------------------------------- ### Manage Player Location and Room Transitions Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst Shows how to manage the player's current location using a global variable and implement movement between rooms with custom commands. It includes logic to check if a move is possible and update the current room. ```Python # current_room will be a global variable. Let's start out in # space, so assign the 'space' room from above. current_room = space @when('enter airlock') def enter_spaceship(): # To set a global variable from within a function you have # to include the 'global' keyword, to avoid creating a # local variable instead. global current_room # Got to check if this action can be done here if current_room is not space: print('There is no airlock here.') return current_room = spaceship # You should include some narrative for every action to # ensure the transition doesn't feel abrupt. print( "You heave yourself into the airlock and slam your " + "hand on the button to close the outer door." ) # Show the room description to indicate we have arrived. print(current_room) ``` -------------------------------- ### Define Context with Nested Hierarchy Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst This Python snippet demonstrates how to define a command ('land') that is only available within a specific nested context ('wonderland.flying'). It also shows how to change the context using `set_context` and provide user feedback with `say`. ```python @when('land', context='wonderland.flying') def land(): set_context('wonderland') say('You gradually drop until you feel the earth beneath your feet.') ``` -------------------------------- ### Customize AdventureLib 'I don't understand' Message Source: https://github.com/lordmauve/adventurelib/blob/master/doc/customising.rst This snippet illustrates how to replace the default 'I don't understand' message with a custom function. The `no_command_matches` function accepts the unrecognized command as an argument and can print custom responses, such as random phrases. ```Python import adventurelib import random def no_command_matches(command): print(random.choice([ 'Huh?', 'Sorry?', 'I beg your pardon?' ])) adventurelib.no_command_matches = no_command_matches ``` -------------------------------- ### Control Command Availability with Context Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Explains how to use the 'context' argument in the @when decorator to make commands available only when a specific game context is active. This is useful for managing command availability based on game state or menus. ```python @when('cast SPELL', context='wonderland') def cast(spell): say(f"You cast the spell.") ``` -------------------------------- ### Programmatically Call @when Functions Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Illustrates that @when decorated functions can be called directly like regular Python functions, even after they have been registered for command parsing. This is useful for triggering actions from other parts of the game logic. ```python @when('look') def look(): print(current_room) @when('go north'): def go_north(): global current_room current_room = current_room.north print('You go north.') look() ``` -------------------------------- ### Adventurelib Decorator for Event Handling Source: https://github.com/lordmauve/adventurelib/blob/master/doc/overview.rst The `@when()` decorator in Adventurelib is used to match specific commands or phrases, enabling event handling within text-based games. It splits input by spaces and matches word-by-word, which may not be suitable for all languages. ```Python from adventurelib import when @when('take ITEM') def take_item(item): # Logic to handle taking an item pass @when('look') def look_around(): # Logic to describe the current environment pass ``` -------------------------------- ### Define Command Handler in adventurelib Source: https://github.com/lordmauve/adventurelib/blob/master/README.md This snippet demonstrates how to define a function that is called when a specific command is entered by the user. The `@when` decorator links the command pattern to the function. The function can then process the command's arguments and perform actions, such as updating the player's inventory. ```Python from adventurelib import when, inventory @when('take THING') def take(thing): print(f'You take the {thing}.') inventory.append(thing) ``` -------------------------------- ### Display Player Inventory Contents Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Shows a handler function for the 'inventory' command that iterates through the items in the player's `Bag` (inventory) and prints each item. It also handles the case where the inventory is empty. ```Python @when('inventory') def show_inventory(): print('You have:') if not inventory: print('nothing') return for item in inventory: print(f'* {item}') ``` -------------------------------- ### Customize AdventureLib Prompt Source: https://github.com/lordmauve/adventurelib/blob/master/doc/customising.rst This snippet shows how to customize the game's player prompt by defining a function that returns the desired prompt string and assigning it to `adventurelib.prompt`. This allows for dynamic display of game information like player health. ```Python import adventurelib def prompt(): return '{hp}HP > '.format(hp=player_hp) adventurelib.prompt = prompt ``` -------------------------------- ### Basic Command Binding with @when Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Binds a simple command string to a Python function. The function is executed when the player types the specified command. This is case-insensitive. ```Python @when("scream") def scream(): print("You unleash a piercing shriek that reverberates around you.") ``` -------------------------------- ### Multiple Command Bindings Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst Binds multiple command strings to a single Python function. The function will be called if any of the specified commands are entered by the player. Spacing in player input is ignored. ```Python @when("shout loudly") @when("shout") @when("yell") def yell(): print("You bellow at the top of your lungs.") ``` -------------------------------- ### Define Multiple Directions with @when Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst This snippet demonstrates how to use multiple `@when` decorators to define a single `go` function that handles different directions. Each decorator maps a command string to a specific direction value passed as an argument. ```python @when('north', direction='north') @when('south', direction='south') @when('east', direction='east') @when('west', direction='west') def go(direction): global current_room room = current_room.exit(direction) if room: current_room = room print(f'You go {direction}.') look() ``` -------------------------------- ### Deeply Nested Context Priority Source: https://github.com/lordmauve/adventurelib/blob/master/doc/commands.rst This Python code shows how a command ('north') within a deeply nested context ('confused.really') overrides commands in less specific contexts. It defines a unique behavior for 'north' when the context is 'confused.really'. ```python @when('north', context='confused.really') def confused_north(): say('The cauliflowers are in bloom this year.') ``` -------------------------------- ### Adventurelib Say Function Source: https://github.com/lordmauve/adventurelib/blob/master/doc/overview.rst The `say()` function in Adventurelib is used for character dialogue or narration. Its implementation might be affected in right-to-left (RTL) languages due to the left-to-right greedy nature of pattern matching. ```Python from adventurelib import say def introduce_character(): say("Hello there! I am a helpful NPC.") introduce_character() ``` -------------------------------- ### Use Grammatically Specific Item Names in Output Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Shows how to use a custom attribute like `def_name` to output grammatically correct references to items, especially when dealing with articles like 'a', 'an', or 'the'. ```Python @when('take ITEM') def take_item(item): obj = current_room.items.take(item) if not obj: print(f'There is no {item} here.') else: print(f'You take {item.def_name}.') inventory.add(obj) ``` -------------------------------- ### Define Character Pronouns and Attributes (Python) Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Illustrates how to define character-specific attributes like 'def_name', 'subject_pronoun', and 'object_pronoun' for an Item. It also shows how to create a subclass (MaleCharacter) to inherit these attributes, simplifying character definition. ```Python wizard = Item('a wizard') wizard.def_name = 'the wizard' wizard.subject_pronoun = 'he' wizard.object_pronoun = 'him' ``` ```Python class MaleCharacter(Item): subject_pronoun = 'he' object_pronoun = 'him' wizard = MaleCharacter('a wizard') wizard.def_name = 'the wizard' ``` -------------------------------- ### Store Room-Specific Attributes in Adventurelib Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst Illustrates how to store custom attributes on Room objects to manage game state and control action availability. It shows setting default attributes on the Room class and overriding them for specific instances. ```Python Room.can_scream = True # The default for all rooms space.can_scream = False # Set a value for a specific room. @when('scream') def scream(): if current_room.can_scream: print( "You unleash a piercing shriek that " + "reverberates around you." ) else: print( "You try to yell but there's no sound " + "in the vacuum of space." ) ``` -------------------------------- ### Assign Default Item Attributes Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Illustrates how to set default attributes for all Item instances by assigning them as class attributes. These defaults can be overridden for specific item instances. ```Python Item.colour = 'grey' ``` -------------------------------- ### Use Item's Default Name in Output Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Shows how to use the default name of an Item object when constructing output strings. The Item object automatically resolves to its primary name when used in f-strings or other string formatting. ```Python print(f'You sweep away cobwebs with {broom}.') ``` -------------------------------- ### Override Item Attributes for Specific Instances Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Demonstrates how to assign specific attributes to individual Item instances, overriding any class-level defaults. This allows for unique properties for each item. ```Python mug = Item('mug') mug.colour = 'red' ``` -------------------------------- ### Define Bi-directional Room Exits in Adventurelib Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst Explains how to establish connections between rooms using directional attributes like 'north', 'south', 'east', and 'west'. It highlights that these links are automatically bi-directional. ```Python space.north = spaceship # Accessing the room to the north: # current_room.north # Bi-directional relationship confirmation: # >>> space.north is spaceship # True # >>> spaceship.south is space # True ``` -------------------------------- ### Register Custom Directions Source: https://github.com/lordmauve/adventurelib/blob/master/doc/rooms.rst This snippet illustrates how to register new, custom directions and their opposites using `Room.add_direction()`. This allows for more complex navigation beyond the standard cardinal directions. ```python Room.add_direction('up', 'down') Room.add_direction('enter', 'out') ``` -------------------------------- ### Access Item Attributes in a Handler Source: https://github.com/lordmauve/adventurelib/blob/master/doc/items.rst Shows a handler function that accesses the 'colour' attribute of an item found in the inventory to describe its appearance. It includes error handling for when the item is not found. ```Python @when('look at ITEM') def look(item): obj = inventory.find(item) if not item: print(f"You do not have a {item}.") else: print(f"It's a sort of {obj.colour}-ish colour.") ``` -------------------------------- ### Adventurelib Room Exits Source: https://github.com/lordmauve/adventurelib/blob/master/doc/overview.rst The `rooms` system in Adventurelib has built-in support for cardinal directions (north, south, east, west). While these are identifiers and can be used with non-English commands, the output of functions like `room.exits()` would require translation for display. ```Python from adventurelib import Room start_room = Room("Start", "You are in a starting room.") next_room = Room("Next", "You have moved to the next room.") start_room.north = next_room next_room.south = start_room print(start_room.exits()) # Example output: {'north': } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.