Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from room import Room

from player import Player
# Declare all the rooms

room = {
Expand Down Expand Up @@ -38,7 +38,7 @@
#

# Make a new player object that is currently in the 'outside' room.

player = Player(input("What is your name, brave soul: "), room["outside"])
# Write a loop that:
#
# * Prints the current room name
Expand All @@ -47,5 +47,14 @@
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
while True:
cmd = input("-> ").lower()
if cmd in ["n", "e", "s", "w"]:
player.travel(cmd)
elif cmd == "q":
print("leaving already?")
exit()
else:
print("What was that?")

# If the user enters "q", quit the game.
11 changes: 11 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, name, starting_room):
self.name = name
self.current_room = starting_room
def travel(self, direction):
next_room = getattr(self.current_room, f"{direction}_to")
if next_room is not None:
self.current_room = next_room
print(self.current_room)
else:
print("There is nothing that way")
13 changes: 12 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
class Room:
def __init__(self, name, description):
self.name = name
self.description = description
self.n_to = None
self.e_to = None
self.s_to = None
self.w_to = None

def __str__(self):
return(f"{self.name}\n{self.description}")