summaryrefslogtreecommitdiffstats
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
parentdfa96fc3a20785d1d405003748a368e5b2ac3aec (diff)
Create initial game framework.
-rw-r--r--.gitignore1
-rw-r--r--README.md4
-rw-r--r--README.rst6
-rw-r--r--start.py4
-rw-r--r--weatbag/__init__.py78
-rw-r--r--weatbag/action.py57
-rw-r--r--weatbag/tiles/__init__.py0
-rw-r--r--weatbag/tiles/centre.py25
-rw-r--r--weatbag/words.py11
9 files changed, 182 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index d2d6f36..ea5bb31 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
*.py[cod]
+*~
# C extensions
*.so
diff --git a/README.md b/README.md
deleted file mode 100644
index 1e83f43..0000000
--- a/README.md
+++ /dev/null
@@ -1,4 +0,0 @@
-weatbag
-=======
-
-Written by Everyone Altogether, The Big Adventure Game \ No newline at end of file
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..5617c47
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,6 @@
+WEATBAG
+=======
+
+Written by Everyone Altogether, The Big Adventure Game
+
+To play, run ``python3 start.py``. Move around with commands like 'go s'.
diff --git a/start.py b/start.py
new file mode 100644
index 0000000..dc36b82
--- /dev/null
+++ b/start.py
@@ -0,0 +1,4 @@
+#!/usr/bin/python3
+from weatbag import main
+
+main()
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)
+
diff --git a/weatbag/action.py b/weatbag/action.py
new file mode 100644
index 0000000..159e9a0
--- /dev/null
+++ b/weatbag/action.py
@@ -0,0 +1,57 @@
+import sys
+from . import words
+
+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)
+
+
+ # Look around
+ return look_around
+
+
+ return (move, action[1][0])
+ 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)
+
+def handle_action(tile, player, do):
+ if len(do) == 1 and do[0] == 'quit':
+ sys.exit()
+
+ elif len(do) == 2 and (do[0] in words.look) and (do[1] in words.surroundings):
+ # Look around
+ tile.describe()
+
+ elif len(do) == 2 and (do[0] in words.look) and (do[1] in words.inventory):
+ # Look at bag
+ for item, n in inventory.most_common():
+ if n < 1:
+ break
+ print(item, '(%d)' % n)
+
+ else:
+ tile.action(player, do)
+
+def process_move(direction):
+ direction = direction[0].lower()
+ if direction == 'n':
+ return (0, 1)
+ if direction == 's':
+ return (0, -1)
+ if direction == 'w':
+ return (-1, 0)
+ if direction == 'e':
+ return (1, 0)
+ return (0, 0)
diff --git a/weatbag/tiles/__init__.py b/weatbag/tiles/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/weatbag/tiles/__init__.py
diff --git a/weatbag/tiles/centre.py b/weatbag/tiles/centre.py
new file mode 100644
index 0000000..7ab4d54
--- /dev/null
+++ b/weatbag/tiles/centre.py
@@ -0,0 +1,25 @@
+from weatbag import words
+
+class Tile:
+ def __init__(self):
+ self.contents = {'rope': 1}
+
+ def describe(self):
+ print("You are in a sizeable clearing in the forest. At your feet, you see "
+ "a mysterious trap door, with three golden keyholes. Paths lead off "
+ "in four directions, which your unerring sense of direction tells you "
+ "correspond to the four compass points.")
+ if self.contents.get('rope'):
+ print("A short length of rope is lying on the floor.")
+
+ def action(self, player, do):
+ if (do[0] in words.take) and ('rope' in do):
+ if self.contents['rope'] > 0:
+ self.contents['rope'] -= 1
+ player.give('rope')
+ print("You put the rope in your bag. That could come in handy.")
+ else:
+ print("You already collected the rope.")
+
+ else:
+ print("Sorry, I don't understand.")
diff --git a/weatbag/words.py b/weatbag/words.py
new file mode 100644
index 0000000..8212657
--- /dev/null
+++ b/weatbag/words.py
@@ -0,0 +1,11 @@
+move = {'move','walk','go'}
+give = {'give', 'feed', 'present'}
+use = {'eat', 'use', 'wear'}
+fight = {'fight', 'kill', 'hit', 'attack'}
+drop = {'drop'}
+take = {'pick', 'take', 'get', 'collect'}
+look = {'look', 'inspect', 'examine'}
+inventory = {'inventory', 'possessions', 'belongings', 'bag'}
+surroundings = {'surroundings', 'around', 'scenery'}
+
+prepositions = {'up', 'down', 'on', 'under', 'in', 'at', 'to'}