Skip to content

Commit 6bf8ae7

Browse files
added item to player, room and item.py, added import for item class in adv.py
1 parent 4938f7c commit 6bf8ae7

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎src/adv.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from room import Room
22
from player import Player
3+
from item import Item
34

45
# Declare all the rooms
56

67
room = {
78
'outside': Room("Outside Cave Entrance",
8-
"North of you, the cave mount beckons"),
9+
"North of you, the cave mount beckons",
10+
[Item("sword", "this sword has dull edges")]),
911

1012
'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
1113
passages run north and east."""),
@@ -95,4 +97,5 @@ def direction_error(direction):
9597
textwrap.fill(player.current_room.name +
9698
". " + player.current_room.description, width=50))
9799
direction = grab_direction()
98-
player.current_room = try_direction(direction, player.current_room)
100+
player.current_room = try_direction(direction, player.current_room)
101+
print(player.current_room.items)

‎src/item.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@ def __init__(self, name, description):
33
self.name = name
44
self.description = description
55

6+
def on_take(self, item):
7+
print(f"You have picked up {item}")
8+
9+
def on_drop(self, item):
10+
print(f"You have picked up {item}")
11+
612
def __repr__(self):
713
return self.name

‎src/player.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# currently.
33

44
class Player():
5-
def __init__(self, name, room):
5+
def __init__(self, name, room, inventory=[]):
66
self.name = name
77
self.current_room = room
8+
self.inventory = inventory
89

910
def __repr__(self):
1011
return f"{self.name} is currently in the {self.current_room}"

‎src/room.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
# description attributes.
33

44
class Room():
5-
def __init__(self, name, description):
5+
def __init__(self, name, description, items=[]):
66
self.name = name
77
self.description = description
8+
self.items = items
9+
10+
def add_item(self, item):
11+
self.items.append(item)
12+
13+
def display_items(self):
14+
if len(self.items) == 0:
15+
return None
16+
else:
17+
for i in self.items:
18+
print(f"{i.name}")
819

920
def __repr__(self):
1021
return f"{self.name}"

0 commit comments

Comments
 (0)