text_game_maker.utils package¶
Submodules¶
-
class
text_game_maker.utils.runner.MapRunner¶ Bases:
objectExtend this class and implement the two methods below to run maps with the text-game-runner.py script
-
build_map(builder)¶ Implement this method to build a map using functions from text_game_maker.builder.map_builder
Parameters: builder (text_game_maker.builder.map_builder.MapBuilder) – map builder instance
-
build_parser(parser)¶ Implement this method to add custom commands to the game parser
Parameters: parser (text_game_maker.parser.parser.CommandParser) – command parser
-
-
exception
text_game_maker.utils.runner.MapRunnerError¶ Bases:
exceptions.Exception
-
text_game_maker.utils.runner.get_runner_from_filename(filename)¶ Import the given file, look for any classes that are subclasses of text_game_maker.utils.runner.MapRunner, and return the class object of the first one found (if any)
Parameters: filename (str) – file to read Returns: MapRunner subclass object found (None if no MapRunner subclasses are found
-
text_game_maker.utils.runner.run_map_from_class(classobj)¶ Create an instance of the given map runner class, and run it
Parameters: classobj – mapp runner class object
-
text_game_maker.utils.runner.run_map_from_filename(filename)¶ Import a file, look for any classes that are subclasses of text_game_maker.utils.runner.MapRunner, and run the first one found (if any)
Parameters: filename (str) – file to read
-
class
text_game_maker.utils.utils.SubclassTrackerMetaClass(name, bases, clsdict)¶ Bases:
type-
__init__(name, bases, clsdict)¶ x.__init__(…) initializes x; see help(type(x)) for signature
-
-
text_game_maker.utils.utils.add_format_token(token, func)¶ Add a format token
Parameters: - token (str) – token string to look for
- func – function to call to obtain replacement text for format token
-
text_game_maker.utils.utils.add_serializable_callback(callback)¶ Add a callback object to registry of callbacks that can be securely referenced in a save file. An ID will be assigned to represent this callback in save files. When loading a save file, whatever object you pass here will be used when the same ID is seen.
Parameters: callback – callback object to register
-
text_game_maker.utils.utils.ask_multiple_choice(choices, msg=None, cancel_word=u'cancel', default=None)¶ Ask the user a multiple-choice question, and return their selection
Parameters: choices ([str]) – choices to present to the player Returns: list index of player’s selection (-1 if user cancelled) Return type: int
-
text_game_maker.utils.utils.ask_yes_no(msg, cancel_word=u'cancel')¶ Ask player a yes/no question, and repeat the prompt until player gives a valid answer
Parameters: - msg (str) – message to print inside prompt to player
- cancel_word (str) – player response that will cause this function to return -1
Returns: 1 if player responded ‘yes’, 0 if they responded ‘no’, and -1 if they cancelled
Return type: int
-
text_game_maker.utils.utils.capitalize(text)¶ Capitalize first non-whitespace character folling each period in string
Parameters: text (str) – text to capitalize Returns: capitalized text Return type: str
-
text_game_maker.utils.utils.centre_text(string, line_width=None)¶ Centre a line of text within a specific number of columns
Parameters: - string (str) – text to be centred
- line_width (int) – line width for centreing text. If None, the current line width being used for game output will be used
Returns: centred text
Return type: str
-
text_game_maker.utils.utils.container_listing(container, item_fmt=u' {0:33}{1:1}({2})', width=50, bottom_border=False, name=None)¶
-
text_game_maker.utils.utils.del_from_lists(item, *lists)¶
-
text_game_maker.utils.utils.deserialize_callback(callback_name)¶
-
text_game_maker.utils.utils.disable_command(command)¶ Disable a parser command. Useful for situations where you want to disable certain capabilities, e.g. loading/saving game states
Parameters: command (str) – word that makes to parser command to disable
-
text_game_maker.utils.utils.disable_commands(*commands)¶ Disable multiple commands at once. Equivalent to multiple ‘disable_command’ calls
Parameters: commands – one or more words mapping to parser commands to disable
-
text_game_maker.utils.utils.draw_map_of_nearby_tiles(player)¶ Draw a ASCII representation of tile surrounding the player’s current position
Parameters: player (text_game_maker.player.player.Player) – player object Returns: created map Return type: str
-
text_game_maker.utils.utils.english_to_list(text)¶ Convert a string of the form ‘a, b, c and d’ to a list of the form [‘a’,’b’,’c’,’d’]
Parameters: text (str) – input string Returns: list of items in string, split on either ‘,’ or ‘and’ Return type: list
-
text_game_maker.utils.utils.find_any_item(player, name)¶ Find an item by name in either the player’s inventory or in the current tile
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – name of item to search for
Returns: found item. If no matching item is found, None is returned.
Return type: text_game_maker.game_objects.items.Item
-
text_game_maker.utils.utils.find_inventory_item(player, name)¶ Find an item by name in player’s inventory
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – name of item to search for
Returns: found item. If no matching item is found, None is returned.
Return type: text_game_maker.game_objects.items.Item
-
text_game_maker.utils.utils.find_inventory_item_class(player, classobj)¶ Find first item in player’s inventory which is an instance of a specific class
Parameters: - player (text_game_maker.player.player.Player) – player object
- classobj – class to check for instances of
Returns: found item. If no matching item is found, None is returned.
Return type: text_game_maker.game_objects.items.Item
-
text_game_maker.utils.utils.find_inventory_wildcard(player, name)¶ Find the first item in player’s inventory whose name matches a wildcard pattern (‘*’).
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – wildcard pattern
Returns: found item. If no matching item is found, None is returned.
Return type: text_game_maker.game_objects.items.Item
-
text_game_maker.utils.utils.find_item(player, name, locations=None, ignore_dark=False)¶ Find an item by name in the provided locations
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – name of item to find
- locations ([[text_game_maker.game_objects.items.Item]]) – location lists to search. If None, the item list of the current tile is used
Returns: found item (None if no matching item is found)
Return type: text_game_maker.items.Item
-
text_game_maker.utils.utils.find_item_class(player, classobj, locations=None, ignore_dark=False)¶ Find the first item that is an instance of a specific class in the provided locations
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – name of item to find
- locations ([[text_game_maker.game_objects.items.Item]]) – location lists to search
Returns: found item (None if no matching item is found)
Return type: text_game_maker.items.Item
-
text_game_maker.utils.utils.find_item_wildcard(player, name, locations=None)¶ Find the first item whose name matches a wildcard pattern (‘*’) in specific locations.
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – wildcard pattern
- locations ([[text_game_maker.game_objects.items.Item]]) – location lists to search. If None, the item list of the current tile is used
Returns: found item. If no matching item is found, None is returned.
Return type: text_game_maker.game_objects.items.Item
-
text_game_maker.utils.utils.find_person(player, name)¶ Find a person by name in the current tile
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – name of person to search for
Returns: found person. If no matching person is found, None is returned.
Return type:
-
text_game_maker.utils.utils.find_tile(player, name)¶ Find an adjacent tile that is connected to the current tile by name
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – name of adjacent tile to search for
Returns: adjacent matching tile. If no matching tiles are found, None is returned
Return type:
-
text_game_maker.utils.utils.flush_waiting_prints()¶
-
text_game_maker.utils.utils.game_print(msg, wait=False)¶ Print one character at a time if player has set ‘print slow’, otherwise print normally
Parameters: msg (str) – message to print
-
text_game_maker.utils.utils.get_all_contained_items(item, stoptest=None)¶ Recursively retrieve all items contained in another item
Parameters: - item (text_game_maker.game_objects.items.Item) – item to retrieve items from
- stoptest – callback to call on each sub-item to test whether recursion should continue. If stoptest() == True, recursion will continue
Returns: list of retrieved items
Return type: [text_game_maker.game_objects.items.Item]
-
text_game_maker.utils.utils.get_all_items(player, locations=None, except_item=None)¶ Retrieves all items from specified locations
Parameters: - player (text_game_maker.player.player.Player) – player object
- locations ([[text_game_maker.game_objects.items.Item]]) – location lists to search. If None, the item list of the current room/tile is used
- except_item (object) – do not retrive item from location if it is the same memory object as except_item. If None, no items are ignored.
Returns: list of retreived items
Return type: [text_game_maker.game_objects.items.Item]
-
text_game_maker.utils.utils.get_basic_controls()¶ Returns a basic overview of game command words
-
text_game_maker.utils.utils.get_builder_instance()¶
-
text_game_maker.utils.utils.get_chardelay()¶
-
text_game_maker.utils.utils.get_full_controls(parser)¶ Returns a comprehensive listing of of all game command words
-
text_game_maker.utils.utils.get_full_import_name(classobj)¶
-
text_game_maker.utils.utils.get_last_command()¶
-
text_game_maker.utils.utils.get_local_tile_map(tileobj, crawltiles=2, mapsize=5)¶ Builds a 2D list of tiles, representing an x/y grid of tiles sorrounding the players current position.
Parameters: - tileobj (text_game_maker.tile.tile.Tile) – starting tile
- crawltiles (int) – maximum tiles to travel from starting tile
- mapsize (int) – map height and width in tiles
Returns: 2D list representing x/y grind of tiles around player
-
text_game_maker.utils.utils.get_print_controls()¶
-
text_game_maker.utils.utils.get_random_name()¶ Get a random first and second name from old US census data, as a string e.g. “John Smith”
Returns: random name Return type: str
-
text_game_maker.utils.utils.get_sequence_count(count)¶
-
text_game_maker.utils.utils.get_serializable_class(name)¶
-
text_game_maker.utils.utils.get_slow_printing()¶
-
text_game_maker.utils.utils.import_module_attribute(fullname)¶
-
text_game_maker.utils.utils.inputfunc(prompt)¶ Block until user input is available
Parameters: prompt (str) – string to prompt user for input Returns: user input Return type: str
-
text_game_maker.utils.utils.is_disabled_command(*commands)¶ Checks if any of the provided words map to a disabled command
Parameters: commands – one or more strings containing words mapping to parser commands
-
text_game_maker.utils.utils.is_location(player, name)¶ Checks if text matches the name of an adjacent tile that is connected to the current tile
Parameters: - player (text_game_maker.player.player.Player) – player object
- name (str) – text to check
Returns: True if text matches adjacent tile name
Return type: bool
-
text_game_maker.utils.utils.last_saved_sound()¶ Retrieve last sound ID saved with text_game_maker.save_sound
Returns: last saved sound ID
Centre a line of text within a specific number of columns, and surround text with a repeated character on either side.
Example:
———- centred text ———-
Parameters: - text (str) – text to be centred
- width (int) – line width for centreing text
- bannerchar (str) – character to use for banner around centred text
- spaces (int) – number of spaces seperating centred text from banner
Returns: centred text with banner
Return type: str
-
text_game_maker.utils.utils.list_to_english(strlist, conj=u'and')¶ Convert a list of strings to description of the list in english. For example, [‘4 coins’, ‘an apple’, ‘a sausage’] would be converted to ‘4 coins, an apple and a sausage’
Parameters: strlist (str) – list of strings to convert to english Returns: english description of the passed list Return type: str
-
text_game_maker.utils.utils.multisplit(s, *seps)¶ Split a string into substrings by multiple tokens
Parameters: - s (str) – string to split
- seps ([str]) – list of strings to split on
Returns: list of substrings
Return type: [str]
-
text_game_maker.utils.utils.pop_command()¶ Pop oldest command from game command sequence list
Returns: oldest command in game command sequence list Return type: str
-
text_game_maker.utils.utils.pop_waiting_print()¶
-
text_game_maker.utils.utils.printfunc(text)¶ Display game output
Parameters: text (str) – text to display Returns: value returned by print function
-
text_game_maker.utils.utils.queue_command_sequence(seq)¶ Add to game command sequence list
Parameters: seq ([str]) – list of command strings to add
-
text_game_maker.utils.utils.read_line(msg, cancel_word=None, default=None)¶
-
text_game_maker.utils.utils.read_line_raw(msg=u'', cancel_word=None, default=None)¶ Read a line of input from stdin
Parameters: msg (str) – message to print before reading input Returns: a line ending with either a newline or carriage return character Return type: str
-
text_game_maker.utils.utils.read_path_autocomplete(msg)¶
-
text_game_maker.utils.utils.register_serializable_class(classobj, name)¶
-
text_game_maker.utils.utils.replace_format_tokens(text)¶ Replace format tokens in string (if any)
Parameters: text (str) – text that may contain format tokens Returns: formatted text Return type: str
-
text_game_maker.utils.utils.run_parser(parser, action)¶
-
text_game_maker.utils.utils.save_sound(sound)¶ Save a sound to be played when parsing of the current command is completed. Overwrites any previously saved sound.
Parameters: sound – sound ID key needed by text_game_maker.audio.audio.play_sound
-
text_game_maker.utils.utils.serializable_callback(callback)¶ Decorator version of add_serializable_callback. Example:
from text_game_maker.player.player import serializable_callback @serializable_callback def my_callback_function(): pass
-
text_game_maker.utils.utils.serialize_callback(callback)¶
-
text_game_maker.utils.utils.set_builder_instance(ins)¶
-
text_game_maker.utils.utils.set_chardelay(delay)¶
-
text_game_maker.utils.utils.set_inputfunc(func)¶ Set function to be used for blocking on input from the user. The default if unset is to use a prompt session from prompt-toolkit reading from stdin of the process where text_game_maker is running. The provided function is responsible for blocking until user input is available, and must return the user input as a string
Parameters: func – function to use for reading user input
-
text_game_maker.utils.utils.set_last_command(cmd)¶
-
text_game_maker.utils.utils.set_printfunc(func)¶ Set function to be used for displaying game output. The default if unset is to use standard python “print”.
Parameters: func – function to pass game output text to be displayed
-
text_game_maker.utils.utils.set_sequence_count(count)¶
-
text_game_maker.utils.utils.set_slow_printing(val)¶
-
text_game_maker.utils.utils.set_wrap_width(width)¶ Set the maximum line width (in characters) used by text_game_maker when wrapping long lines of text (default is 60)
Parameters: width (int) – maximum line width in characters