forked from bloominstituteoftechnology/Intro-Python-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
50 lines (39 loc) · 1.27 KB
/
Copy pathplayer.py
File metadata and controls
50 lines (39 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from item import Item
# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, name, room, inventory=[]):
self.__name = name
self.__current_room = room
self.inventory = inventory
def __str__(self):
room = self.__current_room
return f'''Player: {self.__name}, currently in room: {self.__current_room}
\t\t\t Map Overview:
\t\t\t\t{room.n}
\t{room.w}\t\t{room}\t\t\t{room.e}
\t\t\t\t{room.s}
Other info:
Items: {', '.join([str(item) for item in room.items])}'''
def get_room(self):
return self.__current_room
def enter_room(self, direction):
next_room = getattr(self.__current_room, direction)
if not next_room:
raise ValueError()
self.__current_room = next_room
def add_item(self, item):
item = Item(item)
self.inventory.append(item)
item.on_take()
def drop_item(self, item):
item_location = 0
for index in range(len(self.inventory)):
if str(self.inventory[index]) == item:
item_location = index
break
self.inventory = self.inventory[0:item_location] + self.inventory[item_location+1:]
def __contains__(self, item):
for i in self.inventory:
if str(i) == item:
return True