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

# Declare all the rooms

Expand Down Expand Up @@ -33,12 +35,102 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

# print(room["treasure"].s_to.name)

# PLANNING:
'''
*The Brave Knight*
display welcome message & describe game
player will start outside, tell them their location
prompt user to make a choice between n, s, e, w
if user quits the game, stop the loop and show a msg
based on players chosen direction, go to new location & display new location
make sure players choice is a valid direction - if not display error msg
'''
current_player = Player("adventurer", room['outside'])

welcome_msg = f"Welcome {current_player.name}, let's begin your brave journey!\nYou are currently in the {current_player.current_room.name}, Description: {current_player.current_room.description}"
# quit_msg = "Thanks for playing 'The Brave Knight'!"

#
# Main
#

# step 1 - display welcome message & describe game
# welcome_msg = "Welcome to 'The Brave Knight', an adventure game! Please pick a direction to go in - you are currently outside"



# step 2 - prompt user to make a choice
def get_user_direction():
direction = input("please choose a direction to go in - n, s, e, w\n")
return direction


# step 3 - if user quits game, show message
# def quit_game():
# quit = input("press q to quit")
# quit_msg = "Thanks for playing 'The Brave Knight'!"
# print(quit_msg)

def show_location_info():
print(f"You are now in the {current_player.current_room.name}, Description: {current_player.current_room.description}")
current_player.current_room.items_in_room()


def no_room_opt():
print("Sorry, there is nothing in that direction")

# step 4 - compare users input and take them to location, if location is invalid, throw error
# check the player's current location and see if there is
# a room in the specified direction
# if there is, move them there to that room
# otherwise, print a message saying "we can't go there" and
# not move the player
def get_location(get_user_direction):
if get_user_direction == "n" and not current_player.current_room.n_to == None:
current_player.current_room = current_player.current_room.n_to
show_location_info()
elif get_user_direction == "s" and not current_player.current_room.s_to == None:
current_player.current_room = current_player.current_room.s_to
show_location_info()
elif get_user_direction == "e" and not current_player.current_room.e_to == None:
current_player.current_room = current_player.current_room.e_to
show_location_info()
elif get_user_direction == "w" and not current_player.current_room.w_to == None:
current_player.current_room = current_player.current_room.w_to
show_location_info()
else:
no_room_opt()



# ITEMS
items_list = [
Item('bag of gold', 'oooh shiny'),
Item('shield', 'to protect you'),
Item('sword', 'slay your enemy'),
Item('torch', 'light the way'),
Item('rock', 'fight like David & Goliath')
]

# "saber": Item("light saber", "May the force be with you.")

room['narrow'].items.append(items_list[3])
room['foyer'].items.append(items_list[1])
room['foyer'].items.append(items_list[2])
room['outside'].items.append(items_list[0])
room['overlook'].items.append(items_list[4])


# print(items_list[0].name)



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

# print(current_player.current_room.items)

# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +141,7 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
print(welcome_msg)
while True:
choice = get_user_direction()
get_location(choice)
15 changes: 15 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def pickup_item(self, player):
player.items.append(self)
player.current_room.items.remove(self)
print(f'You have picked up {self.name}')


def on_drop(self, player):
player.items.remove(self)
player.current_room.items.append(self)
print(f'You have dropped {self.name}')
16 changes: 16 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, name, current_room, items = None):
self.name = name
self.current_room = current_room
if items is None:
self.items = []
else:
self.items = items

def print_items(self):
if not self.items:
print('You have no items in your inventory')
else:
print('Here is your inventory:\n')
for x in self.items:
print(x.name)
26 changes: 25 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

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


def items_in_room(self):
if self.items == []:
print('Sorry, this room has no items')
else:
print('This room contains these items:')
for item in self.items:
print(f'{item.name}, {item.description}')
# print(item.description)