Skip to content

Commit a451080

Browse files
author
Beej Jorgensen
committed
new adv
1 parent 1568029 commit a451080

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎.gitignore‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.vscode
1+
.vscode
2+
*.pyc
3+
__pycache__

‎src/adv/adv.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import textwrap
2+
from room import Room
3+
from player import Player
4+
5+
# Declare all the rooms
6+
7+
room = {
8+
'outside': Room("Outside Cave Entrance", "North of you, the cave mount beckons"),
9+
10+
'foyer': Room("Foyer", "Dim light filters in from the south. Dusty passages run north and east."),
11+
12+
'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling
13+
into the darkness. Ahead to the north, a light flickers in
14+
the distance, but there is no way across the chasm."""),
15+
16+
'narrow': Room("Narrow Passage", """The narrow passage bends here from west to north. The smell
17+
of gold permeates the air."""),
18+
19+
'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
20+
chamber! Sadly, it has already been completely emptied by
21+
earlier adventurers. The only exit is to the south."""),
22+
}
23+
24+
25+
# Link rooms together
26+
27+
room['outside'].n_to = room['foyer']
28+
room['foyer'].s_to = room['outside']
29+
room['foyer'].n_to = room['overlook']
30+
room['foyer'].e_to = room['narrow']
31+
room['overlook'].s_to = room['foyer']
32+
room['narrow'].w_to = room['foyer']
33+
room['narrow'].n_to = room['treasure']
34+
room['treasure'].s_to = room['narrow']
35+
36+
37+
def tryDirection(d, curRoom):
38+
"""
39+
Try to move a direction, or print an error if the player can't go that way.
40+
41+
Returns the room the player has moved to (or the same room if the player didn't move).
42+
"""
43+
attrib = d + '_to'
44+
45+
# See if the room has the destination attribute
46+
if hasattr(curRoom, attrib):
47+
# If so, return its value (the next room)
48+
return getattr(curRoom, attrib)
49+
50+
# Otherwise print an error and stay in the same room
51+
print("You can't go that way")
52+
53+
return curRoom
54+
55+
#
56+
# Main
57+
#
58+
59+
# Make a new player object that is currently in the 'outside' room.
60+
61+
player = Player(room['outside'])
62+
63+
# Write a loop that:
64+
#
65+
# * Prints the current room name
66+
# * Prints the current description (the textwrap module might be useful here).
67+
# * Waits for user input and decides what to do.
68+
#
69+
# If the user enters a cardinal direction, attempt to move to the room there.
70+
# Print an error message if the movement isn't allowed.
71+
#
72+
# If the user enters "q", quit the game.
73+
74+
done = False
75+
76+
while not done:
77+
# Print the room name
78+
print("\n{}\n".format(player.curRoom.name))
79+
80+
# Print the room description
81+
for line in textwrap.wrap(player.curRoom.description):
82+
print(line)
83+
84+
# User prompt
85+
s = input("\nCommand> ").strip().lower()
86+
87+
# Handle input
88+
if s == "q":
89+
done = True
90+
elif s in ["n", "s", "w", "e"]:
91+
player.curRoom = tryDirection(s, player.curRoom)
92+
else:
93+
print("Unknown command {}".format(s))
94+

‎src/adv/player.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write a class to hold player information, e.g. what room they are in currently
2+
3+
class Player:
4+
"""Holds information about a player"""
5+
def __init__(self, startRoom):
6+
self.curRoom = startRoom

‎src/adv/room.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Implement a class to hold room information. This should have name and
2+
# description attributes.
3+
4+
class Room:
5+
def __init__(self, name, description):
6+
self.name = name
7+
self.description = description

0 commit comments

Comments
 (0)