summaryrefslogtreecommitdiffstats
path: root/weatbag/__init__.py
diff options
context:
space:
mode:
authorThomas <thomas@study-old.(none)>2012-12-29 19:36:19 +0000
committerThomas <thomas@study-old.(none)>2012-12-29 19:36:19 +0000
commitba2ade314b07f7e16b7860afc621df02f3df46b8 (patch)
tree3299a91636c0dea59d5ae1cea48d091d609aae63 /weatbag/__init__.py
parentdfa96fc3a20785d1d405003748a368e5b2ac3aec (diff)
Create initial game framework.
Diffstat (limited to 'weatbag/__init__.py')
-rw-r--r--weatbag/__init__.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/weatbag/__init__.py b/weatbag/__init__.py
new file mode 100644
index 0000000..6c8ed1b
--- /dev/null
+++ b/weatbag/__init__.py
@@ -0,0 +1,78 @@
+from collections import Counter
+from importlib import import_module
+
+from . import action
+
+class Player:
+ def __init__(self, name):
+ self.name = name
+ self.inventory = Counter()
+ self.position = (0,0)
+
+ def has(self, item):
+ return self.inventory[item] > 0
+
+ def give(self, item, n=1):
+ "Put the item in the player's inventory."
+ self.inventory.update([item]*n)
+
+ def take(self, item, n=1):
+ "Remove an item from the inventory. Raises KeyError if item isn't there."
+ if self.inventory[item] >= n:
+ self.inventory[item] -= n
+ else:
+ raise KeyError(item)
+
+class World:
+ def __init__(self):
+ self.tiles = {}
+ from .tiles import centre
+ self.tiles[(0,0)] = centre.Tile()
+
+ def __getitem__(self, key):
+ try:
+ return self.tiles[key]
+ except KeyError:
+ n,e = 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(n)
+ elif n < 0:
+ modname += 'w'+str(abs(n))
+
+ try:
+ mod = import_module(modname)
+ except ImportError:
+ raise KeyError(key)
+
+ tile = mod.Tile()
+ self.tiles[key] = tile
+ return tile
+
+def main():
+ name = input("What is your name? ")
+ player = Player(name)
+ world = World()
+ tile = world[0,0]
+ while True:
+ tile.describe()
+ while True:
+ do = action.get_action()
+ if action.is_move(do):
+ # move
+ n,e = player.position
+ dn, de = action.process_move(do[1])
+ new_posn = n+dn, e+de
+ try:
+ tile = world[new_posn]
+ except KeyError:
+ print("The undergrowth in that direction is impassable. "
+ "You turn back.")
+ else:
+ action.handle_action(tile, player, do)
+