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
75 changes: 59 additions & 16 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from room import Room
from player import Player
from item import Item

items = {
"Coins": Item("Coins", "Lost Treasure"),
"Potato": Item("Potato", "Just a normal potato"),
"Sword": Item("Sword", "Arawn's Mighty Blade")
}

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
'outside': Room("Outside Cave Entrance","North of you, the cave mount beckons", [items["Sword"]]),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
passages run north and east.""", [items["Potato"]]),

'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling
into the darkness. Ahead to the north, a light flickers in
Expand All @@ -17,8 +24,7 @@
to north. The smell of gold permeates the air."""),

'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
chamber! Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south."""),
chamber! You see something shiny in the corner. The only exit is to the south.""", [items["Coins"]]),
}


Expand All @@ -36,16 +42,53 @@
#
# Main
#

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

# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

new_player = Player("John Price", [], room['outside'])



ui_display = ""
ui_display += "\nActions: [get item] [drop item] [i]nventory [q]uit\n"
ui_display += "\nMovement: [n]orth [s]outh [e]ast [w]est \n"


directions = ("n", "s", "e", "w")

def err_msg():
print(f"\nThat is not a valid input\n")
print(new_player.current_room)

print("\n Enter n, s, e or w to navigate between rooms \n Enter 'get item' to pick up item, 'drop item' to get rid of it and 'i' to check your inventory\n Enter q to quit the game ")

while True:

user_input = input("-").split()

if len(user_input) == 1:
if user_input[0] == "i":
print(new_player)

elif user_input[0] in directions:
new_player.move(user_input[0])

elif user_input[0] == "q":
print("\nYou have quit the game\n")
break

else:
err_msg()
continue


elif len(user_input) == 2:
if user_input[1] in items:
new_player.action(user_input[0], items[user_input[1]])
else:
err_msg()
continue

else:
err_msg()
continue
16 changes: 16 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

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

def look(self):
return f"{self.description}"

def on_take(self):
return f"You have picked up the {self.name}."

def on_drop(self):
return f"You have dropped the {self.name}"
33 changes: 33 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

class Player:
def __init__(self, name, inventory, current_room):
self.name = name
self.inventory = inventory
self.current_room = current_room

def __str__(self):
display_string = ""
display_string += f"\nName:{self.name}\n"
display_string += f"\nInventory:{[i.name for i in self.inventory]}"
return display_string

def move(self, direction):
next_room = self.current_room.get_room_by_direction(direction)
#check if move is valid
if next_room:
self.current_room = next_room
print(self.current_room)
else:
return self.current_room.wrong_room()

def action(self,action,item):
if action == "get" and self.current_room.get_item(item):
self.inventory.append(item)
print(item.on_take())
elif action == "drop" and item in self.inventory:
self.inventory.remove(item)
self.current_room.items.append(item)
print(item.on_drop())

else:
print(f"Im sorry {self.name}, you can't do that.")
33 changes: 32 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, name, description, items=[]):
self.name = name
self.description = description
self.items = items
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None

def get_room_by_direction(self,direction):
print(direction)
if hasattr(self, f"{direction}_to"):
return getattr(self, f"{direction}_to")
else:
return None

def get_item(self, item):
if item in self.items:
self.items.remove(item)
return True
else:
print("That item is not here.")
return False


def __str__(self):
return f"\n{self.name}\n{self.description}\n\nRoom Items {[i.name for i in self.items]}"
def wrong_room(self):
print(f"\nYou cannot enter that room\n\n{self.description}\n")