Up to this point, you've gotten your feet wet by working on a bunch of small Python programs. In this module, we're going to continue to solidify your Python chops by implementing a full-featured project according to a provided specification.
- Solidify the Python basics by implementing a text adventure game
- Get more comfortable writing code that conforms to an (imperfect) specification
- Add classes for rooms and the player
- Add a simple parser that reads user input and performs actions
- Add items to the game that the user can carry around
- Make rooms able to hold multiple items
- Make the player able to carry multiple items
- Add two-word commands to the parser
- Add the
getanddropcommands to the parser
The /src directory contains the files adv.py, which is where the main logic for the entire game should live, room.py, which will contain the definition of the Room class, and player.py, which will contain the definition of the Player class.
-
Put the Room class in
room.pybased on what you see inadv.py. -
Put the Player class in
player.py. -
Create a file called
item.pyand add anItemclass in there.-
This will be the base class for specialized item types to be declared later.
-
The item should have
nameanddescriptionattributes.- Hint: the name should be one word for ease in parsing later.
-
-
Add the ability to add items to rooms.
-
The
Roomclass should be extended with alistthat holds theItems that are currently in that room. -
Add functionality to the main loop that prints out all the items that are visible to the player when they are in that room.
-
-
Add capability to add
Items to the player's inventory. The inventory can also be alistof items "in" the player, similar to howItems can be in aRoom. -
Add a new type of sentence the parser can understand: two words.
-
Until now, the parser could just understand one sentence form:
verbsuch as "n" or "q".
-
But now we want to add the form:
verbobjectsuch as "take coins" or "drop sword".
-
Split the entered command and see if it has 1 or 2 words in it to determine if it's the first or second form.
-
-
Implement support for the verb
getfollowed by anItemname. This will be used to pick upItems.-
If the user enters
getortakefollowed by anItemname, look at the contents of the currentRoomto see if the item is there.-
If it is there, remove it from the
Roomcontents, and add it to thePlayercontents. -
If it's not there, print an error message telling the user so.
-
Add an
on_takemethod toItem.-
Call this method when the
Itemis picked up by the player. -
The
Itemcan use this to run additional code when it is picked up.
-
-
Add an
on_dropmethod toItem. Implement it similar toon_take.
-
-
-
Implement support for the verb
dropfollowed by anItemname. This is the opposite ofget/take. -
Add the
iandinventorycommands that both show a list of items currently carried by the player.
In arbitrary order:
-
Add more rooms
-
Add scoring
-
Subclass items into treasures
-
Add a subclass to
ItemcalledLightSource.-
During world creation, add a
lampLightSourceto a convenientRoom. -
Override
on_dropinLightSourcethat tells the player "It's not wise to drop your source of light!" if the player drops it. (But still lets them drop it.) -
Add an attribute to
Roomcalledis_lightthat isTrueif theRoomis naturally illuminated, orFalseif aLightSourceis required to see what is in the room. -
Modify the main loop to test if there is light in the
Room(i.e. ifis_lightisTrueor there is aLightSourceitem in theRoom's contents or if there is aLightSourceitem in thePlayer's contents). -
If there is light in the room, display name, description, and contents as normal.
-
If there isn't, print out "It's pitch black!" instead.
-
Hint:
isinstancemight help you figure out if there's aLightSourceamong all the nearbyItems. -
Modify the
get/takecode to print "Good luck finding that in the dark!" if the user tries to pick up anItemin the dark.
-
-
Add methods to notify items when they are picked up or dropped
-
Add light and darkness to the game
-
Add more items to the game.
-
Add a way to win.
-
Add more to the parser.
-
Remember the last
Itemmentioned and substitute that if the user types "it" later, e.g.take sword drop it -
Add
Items with adjectives, like "rusty sword" and "silver sword".-
Modify the parser to handle commands like "take rusty sword" as well as "take sword".
- If the user is in a room that contains both the rusty sword and silver sword, and they type "take sword", the parser should say, "I don't know which you mean: rusty sword or silver sword."
-
-
-
Modify the code that calls
on_taketo check the return value. Ifon_takereturnsFalse, then don't continue picking up the object. (I.e. prevent the user from picking it up.)- This enables you to add logic to
on_taketo code things like "don't allow the user to pick up the dirt unless they're holding the shovel.
- This enables you to add logic to
-
Add monsters.
-
Add the
attackverb that allows you to specify a monster to attack. -
Add an
on_attackmethod to the monster class. -
Similar to the
on_takereturn value modification, above, haveon_attackprevent the attack from succeeding unless the user possesses asworditem. -
Come up with more stretch goals! The sky's the limit!