Skip to content

Commit b9e6494

Browse files
committed
Added Characters and Item properties
1 parent 95b07b7 commit b9e6494

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

Using-Adventurelib/main.py

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Place the adventurelib.py file in the root folder and import
1515
from adventurelib import *
1616

17-
# The remaining logic of the game goes before the start() call
17+
# The remaining logic of the game goes before calling the start() function
1818

1919
# A Bag() is an class used to hold a collection of items
2020
# Create an instance of Bag() for the player's inventory
@@ -26,16 +26,34 @@
2626
# Create the items to be found in the game
2727
# Use the Item() class to create objects for the player to find
2828
key = Item('small key','key')
29+
key.carry = True
2930
sword = Item('broad sword','sword')
30-
flower = Item('pot of flowers','flowers')
31+
sword.carry = True
32+
flower = Item('pot of flowers','flower','flowers')
33+
flower.def_name = 'the pot of flowers'
34+
flower.carry = False
3135
potion = Item('magic potion','potion')
32-
36+
potion.carry = True
37+
cheese = Item('block of cheese', 'cheese')
38+
cheese.carry = True
39+
# Also create characters in the game using the Item() class
40+
# Add properties to the character such as health level and weakness in battle
41+
greenZombie = Item('green Zombie','zombie')
42+
greenZombie.def_name = 'the green Zombie'
43+
greenZombie.carry = False
44+
greenZombie.health = 3
45+
greenZombie.weakness = 'sword'
46+
47+
redZombie = Item('red Zombie','zombie')
48+
redZombie.def_name = 'the red Zombie'
49+
redZombie.carry = False
50+
redZombie.health = 3
51+
redZombie.weakness = 'cheese'
3352

3453
# Start the game with the player in the Hall using the class Room()
3554
current_room = Hall = Room(
3655
"""
3756
You are in the main downstairs hallway.
38-
You see exits in every direction.
3957
"""
4058
)
4159
# Place items as a property of a room
@@ -46,75 +64,66 @@
4664
"""
4765
A large, very clean kitchen.
4866
There should be flowers here for decoration.
49-
There are exits to the north, west and east.
5067
"""
5168
)
52-
53-
Kitchen.items = Bag({flower,})
69+
Kitchen.items = Bag({flower,redZombie})
5470

5571
Dining_Room = Hall.east = Room(
5672
"""
5773
You are in the dining room. There is a long, majestic
5874
mahagony table in the center of the room.
59-
There are exits to the west, south and north.
6075
"""
6176
)
62-
63-
Dining_Room.items = Bag({sword,})
77+
Dining_Room.items = Bag({sword,cheese})
6478

6579
Library = Hall.west = Room(
6680
"""
67-
You are in the library.
68-
An impressive collection of very old books.
69-
There are exits to the east, south and north.
81+
You are in the library with
82+
an impressive collection of very old books.
7083
"""
7184
)
72-
7385
Library.items = Bag({potion,})
7486

7587
Study = Library.south = Room(
7688
"""
7789
This is the Study.
7890
A quiet room with large, comfortable chairs.
79-
There are exits to the north and east.
8091
"""
8192
)
93+
Study.items = Bag({greenZombie,})
8294

8395
Parlor = Library.north = Room(
8496
"""
8597
A parlor nicely decorated with paintings and
8698
number of chairs arranged in a circle.
87-
Therer is an exit to the south and east.
8899
"""
89100
)
90101

91102
Atrium = Hall.north = Room(
92103
"""
93104
An atrium with a high, vaulted ceiling.
94-
There are exits in every direction.
95105
"""
96106
)
97107

98108
Garden = Atrium.north = Room(
99109
"""
100-
A magnificent, sprawling garden with every
101-
imaginable type of flower, shrubbery and trees.
102-
The entrance to the house is to the south.
110+
You are outside of the house in
111+
a magnificent, sprawling garden with every
112+
imaginable type of flower, shrubbery and
113+
a number of huge trees.
103114
"""
104115
)
105116

106117
Guest_Bedroom = Dining_Room.north = Room(
107118
"""
108119
A small, cozy guest bedroom with a window looking out on the Garden.
109-
There are exits to the south and west.
110120
"""
111121
)
112122

113123
Pantry = Dining_Room.south = Room(
114124
"""
115125
A pantry with rows of shelves containing kitchen utensils
116126
and an ample supply of canned goods.
117-
There exits to the north and west.
118127
"""
119128
)
120129

@@ -140,11 +149,16 @@ def go(direction):
140149
look()
141150

142151
@when('take ITEM')
152+
@when('get ITEM')
143153
def take(item):
144-
obj = current_room.items.take(item)
154+
obj = current_room.items.find(item)
145155
if obj:
146-
say(f'You pick up the {obj}.')
147-
inventory.add(obj)
156+
if obj.carry:
157+
obj = current_room.items.take(item)
158+
say(f'You pick up the {obj}.')
159+
inventory.add(obj)
160+
else:
161+
say(f'You can not hold {obj.def_name}')
148162
else:
149163
say(f'There is no {item} here.')
150164

@@ -164,6 +178,11 @@ def look():
164178
for i in current_room.items:
165179
say(f'A {i} is here.')
166180

181+
@when('doors')
182+
def list_exits():
183+
for exits in current_room.exits():
184+
say(f'There is a door to the {exits}.')
185+
167186
@when('inventory')
168187
def show_inventory():
169188
say('You have:')
@@ -172,4 +191,4 @@ def show_inventory():
172191

173192
# Start the game
174193
look()
175-
start()
194+
start()

0 commit comments

Comments
 (0)