diff options
| author | Thomas Kluyver <takowl@gmail.com> | 2013-01-07 20:50:07 +0000 |
|---|---|---|
| committer | Thomas Kluyver <takowl@gmail.com> | 2013-01-07 20:50:07 +0000 |
| commit | 3400844980917bb3d59da322a3e33f55f81afe12 (patch) | |
| tree | 898ff62681401e2c0a4b4a66c11f626d7b784a25 | |
| parent | 87e6fd0a9d0ec7869dc9e6e404638cc1e090e3a7 (diff) | |
Add a simple way to test tiles
| -rw-r--r-- | doc/index.rst | 1 | ||||
| -rw-r--r-- | doc/testing.rst | 13 | ||||
| -rwxr-xr-x | test_tile.py | 25 | ||||
| -rw-r--r-- | weatbag/__init__.py | 52 | ||||
| -rw-r--r-- | weatbag/tiles/n2.py | 2 |
5 files changed, 80 insertions, 13 deletions
diff --git a/doc/index.rst b/doc/index.rst index 61d7ed2..c4184d5 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -21,6 +21,7 @@ The idea is that anything goes, but we do need a few house rules: interface.rst api_misc.rst + testing.rst If you prefer to learn by example, have a look at the `tiles already in the game <https://github.com/takluyver/weatbag/tree/master/weatbag/tiles>`_. diff --git a/doc/testing.rst b/doc/testing.rst new file mode 100644 index 0000000..db70f09 --- /dev/null +++ b/doc/testing.rst @@ -0,0 +1,13 @@ +Testing tiles +============= + +To save you repeatedly playing through the game to test your latest changes, +there's a script to skip to any given tile:: + + ./test_tile.py n2 + +If the player needs specific items to test the tile, add a list called test_items +to the module. The test script will add these to the player's inventory. For +example, tile n2 defines:: + + test_items = ['unlit torch'] diff --git a/test_tile.py b/test_tile.py new file mode 100755 index 0000000..e6ab9e6 --- /dev/null +++ b/test_tile.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import sys +from importlib import import_module +from weatbag import Player, interact, name_to_coords + +if len(sys.argv) != 2: + print("Usage: ./test_tile.py <tile module>") + print("E.g. : ./test_tile.py n2") + sys.exit() + +player = Player('Test player') +player.position = name_to_coords(sys.argv[1]) + +mod = import_module('weatbag.tiles.' + sys.argv[1]) +if hasattr(mod, 'test_items'): + player.inventory.update(mod.test_items) + print("You have test items:") + for item, n in player.inventory.most_common(): + if n < 1: + break + print(item, '(%d)' % n) + print() + +interact(player) + diff --git a/weatbag/__init__.py b/weatbag/__init__.py index 7c39d21..72912c6 100644 --- a/weatbag/__init__.py +++ b/weatbag/__init__.py @@ -1,5 +1,6 @@ from collections import Counter from importlib import import_module +import re from . import action @@ -34,18 +35,7 @@ class World: try: return self.tiles[key] except KeyError: - e,n = key - modname = 'weatbag.tiles.' - if n > 0: - modname += 'n'+str(n) - elif n < 0: - modname += 's'+str(abs(n)) - - if e > 0: - modname += 'e'+str(e) - elif e < 0: - modname += 'w'+str(abs(e)) - + modname = 'weatbag.tiles.' + coords_to_name(*key) try: mod = import_module(modname) except ImportError: @@ -55,11 +45,47 @@ class World: self.tiles[key] = tile return tile +def coords_to_name(e, n): + modname = '' + if n > 0: + modname += 'n'+str(n) + elif n < 0: + modname += 's'+str(abs(n)) + + if e > 0: + modname += 'e'+str(e) + elif e < 0: + modname += 'w'+str(abs(e)) + + return modname + +_tile_name_re = re.compile(r'([ns]\d+)?([ew]\d+)?') +def name_to_coords(tilename): + ns, es = _tile_name_re.match(tilename).groups() + if ns is None: + n = 0 + elif ns[0] == 'n': + n = int(ns[1:]) + else: + n = -int(ns[1:]) + + if es is None: + e = 0 + elif es[0] == 'e': + e = int(es[1:]) + else: + e = -int(es[1:]) + + return e, n + def main(): name = input("What is your name? ") player = Player(name) + interact(player) + +def interact(player): world = World() - tile = world[0,0] + tile = world[player.position] tile.describe() while True: do = action.get_action() diff --git a/weatbag/tiles/n2.py b/weatbag/tiles/n2.py index b903ed3..0f8da93 100644 --- a/weatbag/tiles/n2.py +++ b/weatbag/tiles/n2.py @@ -24,3 +24,5 @@ class Tile: else: print("Sorry, I don't understand") + +test_items = ['unlit torch'] |
