Skip to content

CSPT13-Karthik-Intro2Python-2 #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
49 changes: 48 additions & 1 deletion src/adv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from room import Room

from player import Player
from item import Item
# Declare all the rooms

room = {
Expand Down Expand Up @@ -33,11 +34,31 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

#list of items

items = {'greatsword': Item("greatsword","Ancient sword of Elves"),
'warhammer': Item("warhammer","Dwarven WarHammer"),
'Armor': Item("Armor","Dragonscale armor"),
'Potion': Item("Potion","Potion of resist Poison"),
'Banner': Item("Banner","House Banner"),
'Ring': Item("Ring","Pearl Ring")}

#Put items in Rooms

room['foyer'].items = [items['greatsword'],items['Potion']]
room['treasure'].items = [items['Ring']]
room['overlook'].items = [items['Banner']]
room['narrow'].items = [items['Armor'],]

print (room['foyer'].items)

#
# Main
#

# Make a new player object that is currently in the 'outside' room.
name = input ('State thy name adventurer! : ')
player = Player(name,room['outside'])

# Write a loop that:
#
Expand All @@ -49,3 +70,29 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

# Create a dict of possible directions
directions = {'N':'n_to', 'S':'s_to', 'E':'e_to', 'W':'w_to'}
#Print the current position of player.
print(f"###### {player.name}, you are at {player.current_room.name}######")

while True:

choice = input('Where do you want to go? (Press N for North, E for east , W for West , S for South. Q for quit game')
# if user choice is valid move player to new location and print it,
# if Choice is to quit then break out of loop
# if choice is invalid then ask for valid input.
if choice in directions.keys():
direction = directions[choice]
previous_room = player.current_room
try:
player.current_room = getattr(player.current_room,direction)
print(f"###### {player.name}, you are at {player.current_room.name}######")
except AttributeError:
player.current_room = previous_room
print('Cannot go that way! Choose again. ')
elif choice is 'Q':
print ('Good bye adventurer! Thanks for Playing :)')
break
else:
print('Invalid choice! Choose again.')
6 changes: 6 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#item Base Class

class Item():
def __init__(self,item,description):
self.item=item
self.description=description
9 changes: 9 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

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

def __str__(self):
return '{0} is at {1}'.format(self.name,self.current_room)

19 changes: 18 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room():
def __init__(self, name, description, n_to=None, s_to=None, e_to=None, w_to=None,items=[]):
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
self.items=items

def put_item(self,item_name):
self.items = self.items.append(Item[item_name])

def take_item(self,item_name):
self.items = self.items.remove(item_name)