Skip to content

Commit d7e1c82

Browse files
committed
Initial import.
1 parent 172ea9d commit d7e1c82

File tree

7 files changed

+118
-4
lines changed

7 files changed

+118
-4
lines changed

README.md

-4
This file was deleted.

README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Python Sheffield Adventure Game
2+
===============================
3+
4+
To play, run start.py.
5+
6+
Note that this is written for Python 3.

agps/__init__.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from collections import Counter
2+
from .tiles import grid
3+
from .tiles.start import GameWon
4+
5+
def process_move(move):
6+
move = move.upper()
7+
if move == 'N':
8+
return (0, 1)
9+
if move == 'S':
10+
return (0, -1)
11+
if move == 'W':
12+
return (-1, 0)
13+
if move == 'E':
14+
return (1, 0)
15+
print("Move %s not recognised" % move)
16+
return (0, 0)
17+
18+
def main():
19+
name = input("What is your name? ")
20+
inventory = Counter()
21+
x, y = 0, 0
22+
23+
try:
24+
while True:
25+
tile = grid[x,y]
26+
move = tile.enter(name, inventory)
27+
dx, dy = process_move(move)
28+
new_locn = x+dx, y+dy
29+
if new_locn in grid:
30+
x, y = new_locn
31+
else:
32+
print("The undergrowth in that direction is impenetrable. "
33+
"You turn back.")
34+
print()
35+
36+
except GameWon:
37+
print("Congratulations!")

agps/tiles/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from . import start
2+
3+
grid = {
4+
(0,0): start,
5+
}

agps/tiles/start.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""(0, 0) - the square where the game starts and ends."""
2+
3+
from agps.utils import action_prompt, move, take_words
4+
5+
class GameWon(Exception):
6+
"""Abuse exception handling to escape the game loop when we finish."""
7+
pass
8+
9+
scene_contents = dict(keys_used = 0,
10+
rope = 1,
11+
)
12+
13+
def use_key(inventory):
14+
if inventory['golden key'] > 0:
15+
inventory['golden key'] -= 1
16+
scene_contents['keys_used'] += 1
17+
if scene_contents['keys_used'] == 5:
18+
raise GameWon
19+
print("You put the key in and turn it carefully. "
20+
"%s more to go." % 5 - scene_contents['keys_used'])
21+
else:
22+
print("You don't have a golden key to use.")
23+
use = {'use', 'insert'}
24+
25+
def pick_up_rope(inventory):
26+
if scene_contents['rope'] > 0:
27+
inventory['rope'] += 1
28+
scene_contents['rope'] -= 1
29+
print("You put the rope in your bag. That could come in useful.")
30+
else:
31+
print("You already collected the rope.")
32+
33+
def enter(name, inventory):
34+
print("You are in a sizeable clearing in the forest. At your feet, you see "
35+
"a mysterious trap door, with five golden keyholes. Paths lead off "
36+
"in four directions, which your unerring sense of direction tells you "
37+
"correspond to the four compass points.")
38+
if scene_contents.get('rope'):
39+
print("A short length of rope is lying on the floor.")
40+
print()
41+
while True:
42+
action = action_prompt()
43+
if action[0] is move:
44+
return action[1]
45+
if (action[0] in use) and ('key' in action):
46+
use_key(inventory)
47+
elif (action[0] in take_words) and ('rope' in action):
48+
pick_up_rope(inventory)

agps/utils.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
move = object()
2+
move_directions = {'n','e','s','w','north','east','south','west'}
3+
move_words = {'move','walk','go'}
4+
prepositions = {'up', 'down', 'on', 'under', 'in', 'at', 'to'}
5+
6+
def action_prompt():
7+
"""Prompts for an action, splits it into words, and removes any prepositions.
8+
9+
movement actions will be represented by the move token object in this module,
10+
followed by a one-letter direction.
11+
"""
12+
action = input('> ').lower().split()
13+
for prep in prepositions.intersection(action):
14+
action.remove(prep)
15+
if len(action) == 2 and (action[0] in move_words) and (action[1] in move_directions):
16+
return (move, action[1][0])
17+
return action
18+
19+
take_words = {'pick', 'take', 'get', 'collect'}

start.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/python3
2+
import agps
3+
agps.main()

0 commit comments

Comments
 (0)