diff options
| author | Julian Irwin <julian.irwin@gmail.com> | 2013-01-26 13:03:36 -0500 |
|---|---|---|
| committer | Julian Irwin <julian.irwin@gmail.com> | 2013-01-26 13:03:36 -0500 |
| commit | 96ec51791405fd8982fdda3ee430a0520f0a822c (patch) | |
| tree | 337afbc8831baddd574700b0a0150102d5120e69 | |
| parent | 7f6cb008d54bb650662605ecd702089f87eea70d (diff) | |
| parent | f19f86792b33beaf2e1e12989a85557ae7fbc29c (diff) | |
Merge branch 'master' of git://github.com/takluyver/weatbag into southern-tiles-1
Getting everything up to date before I continue. Also see if anyone has added cool new features.
| -rw-r--r-- | doc/api_misc.rst | 20 | ||||
| -rw-r--r-- | weatbag/action.py | 35 | ||||
| -rw-r--r-- | weatbag/items/__init__.py | 26 | ||||
| -rw-r--r-- | weatbag/items/unlit_torch.py | 6 | ||||
| -rw-r--r-- | weatbag/tiles/n2.py | 19 | ||||
| -rw-r--r-- | weatbag/words.py | 7 |
6 files changed, 103 insertions, 10 deletions
diff --git a/doc/api_misc.rst b/doc/api_misc.rst index 674ab08..3a68d81 100644 --- a/doc/api_misc.rst +++ b/doc/api_misc.rst @@ -24,6 +24,26 @@ Understanding commands .. automodule:: weatbag.words +Combining items +--------------- + +To allow items to be combined to form other items, add a module in ``weatbag.items`` +with the same name as one of the items, replacing spaces with underscores. There, +define a function called ``combine``, which will take the name of a second +object. You only need to define this for one object; the game will try to find +it for both objects being combined. + +If the objects can be combined, it should describe what has happened, and return +a list of the new objects created. Otherwise, it should return ``None``. For +example, this function is in :mod:`weatbag.items.unlit_torch`:: + + def combine(other): + if other == 'match': + print("The torch sputters into life.") + return ['flaming torch'] + + return None + Utility functions ----------------- diff --git a/weatbag/action.py b/weatbag/action.py index 9ef0f47..73e5a47 100644 --- a/weatbag/action.py +++ b/weatbag/action.py @@ -1,24 +1,23 @@ import sys from . import words +from weatbag.items import combine def get_action(): """Prompts for an action, splits it into words, and removes any prepositions. - + movement actions will be represented by the move token object in this module, followed by a one-letter direction. """ action = [] while len(action) == 0: action = input('> ').lower().split() - for prep in words.prepositions.intersection(action): - action.remove(prep) - + action = [w for w in action if w not in words.prepositions] return action move_directions = {'n','e','s','w','north','east','south','west'} def is_move(do): - return len(do) == 2 and (do[0] in words.move) and (do[1] in move_directions) + return len(do) == 2 and (do[0] in words.move) and (do[1] in move_directions) def handle_action(tile, player, do): if len(do) == 1 and do[0] == 'quit': @@ -35,6 +34,32 @@ def handle_action(tile, player, do): break print(item, '(%d)' % n) + elif len(do) >= 3 and (do[0] in words.combine): + for split in range(2, len(do)): + item1 = ' '.join(do[1:split]) + item2 = ' '.join(do[split:]) + if player.has(item1): + if player.has(item2): + results = combine(item1, item2) + if results is not None: + player.take(item1) + player.take(item2) + player.inventory.update(results) + else: + print("You can't combine {} with {}".format(item1, item2)) + + else: + print("You don't have a {}".format(item2)) + + return + + elif player.has(item2): + print("You don't have a {}".format(item1)) + return + + else: + print("You don't have those items.") + else: tile.action(player, do) diff --git a/weatbag/items/__init__.py b/weatbag/items/__init__.py new file mode 100644 index 0000000..3d0922f --- /dev/null +++ b/weatbag/items/__init__.py @@ -0,0 +1,26 @@ +from importlib import import_module +from types import ModuleType + +def get_item_module(name): + """Returns the module for that item, or a blank module if no module exists. + """ + name = name.replace(' ','_') + try: + return import_module('weatbag.items.' + name) + except ImportError: + return ModuleType(name, "Dummy Module") + + +def _combine(item1, item2): + "Try combining items one way" + try: + return get_item_module(item1).combine(item2) + except AttributeError: + pass + +def combine(item1, item2): + "Try combining items either way round." + results = _combine(item1, item2) + if results is None: + results = _combine(item2, item1) + return results diff --git a/weatbag/items/unlit_torch.py b/weatbag/items/unlit_torch.py new file mode 100644 index 0000000..1f313ca --- /dev/null +++ b/weatbag/items/unlit_torch.py @@ -0,0 +1,6 @@ +def combine(other): + if other == 'match': + print("The torch sputters into life.") + return ['flaming torch'] + + return None diff --git a/weatbag/tiles/n2.py b/weatbag/tiles/n2.py index 32e3ce4..70f6051 100644 --- a/weatbag/tiles/n2.py +++ b/weatbag/tiles/n2.py @@ -1,10 +1,16 @@ from weatbag import words +from weatbag.utils import transfer class Tile: + def __init__(self): + self.contents = {'match': 42} + def describe(self): print("You are standing in the mouth of a cave. The air feels cool and " "is filled with the stench of rotting flesh. " "A dark path leads to the north. An exit to the south.") + if self.contents['match'] > 0: + print("Someone has dropped a box of matches on the ground.") def leave(self, player, direction): if direction=='s': @@ -15,14 +21,21 @@ class Tile: "that stalwart friend of the adventurer, the flaming torch.") def action(self, player, do): - if (do[0] in words.use or do[0]=='light') and do[1]=='torch': - if player.has('unlit torch'): + if (do[0] in words.take) and ('matches' in do): + if transfer('match', self.contents, player.inventory, n=42): + print("You pick up the matches.") + else: + print("You've already taken the matches.") + + elif (do[0] in words.use or do[0]=='light') and do[1]=='torch': + if player.has('unlit torch') and player.has('match'): player.take('unlit torch') + player.take('match') player.give('flaming torch') print("The torch catches, casting a flickering light on the " "cave walls.") else: - print("Good plan, but you don't have a torch.") + print("You'll need a torch and a match.") else: print("Sorry, I don't understand") diff --git a/weatbag/words.py b/weatbag/words.py index 25f762e..94d2134 100644 --- a/weatbag/words.py +++ b/weatbag/words.py @@ -6,7 +6,7 @@ e.g. This will recognise take, pick, get or collect:: if do[0] in weatbag.words.take: #... """ - +# Verbs move = {'move','walk','go'} give = {'give', 'feed', 'present'} use = {'eat', 'use', 'wear'} @@ -15,7 +15,10 @@ drop = {'drop'} take = {'pick', 'take', 'get', 'collect'} look = {'look', 'inspect', 'examine'} attack = {'attack', 'swing', 'hit', 'punch', 'fight'} +combine = {'combine', 'join', 'mix'} + +# Nouns inventory = {'inventory', 'possessions', 'belongings', 'bag'} surroundings = {'surroundings', 'around', 'scenery'} -prepositions = {'up', 'down', 'on', 'under', 'in', 'at', 'to'} +prepositions = {'up', 'down', 'on', 'under', 'in', 'at', 'to', 'with', 'and'} |
