Skip to content
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
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 72 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,172 +1,173 @@
2/13/2019

# Intro to Python II

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.
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.

## Goals

* Solidify the Python basics by implementing a text adventure game
* Get more comfortable writing code that conforms to an (imperfect) specification
- Solidify the Python basics by implementing a text adventure game
- Get more comfortable writing code that conforms to an (imperfect) specification

## Tasks

* 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 `get` and `drop` commands to the parser
- 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 `get` and `drop` commands to the parser

## Specification

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.
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.py` based on what you see in `adv.py`.
- Put the Room class in `room.py` based on what you see in `adv.py`.

* Put the Player class in `player.py`.
- Put the Player class in `player.py`.

* Create a file called `item.py` and add an `Item` class in there.
- Create a file called `item.py` and add an `Item` class in there.

* This will be the _base class_ for specialized item types to be declared
- This will be the _base class_ for specialized item types to be declared
later.

* The item should have `name` and `description` attributes.
- The item should have `name` and `description` attributes.

* Hint: the name should be one word for ease in parsing later.
- Hint: the name should be one word for ease in parsing later.

* Add the ability to add items to rooms.
- Add the ability to add items to rooms.

* The `Room` class should be extended with a `list` that holds the `Item`s
- The `Room` class should be extended with a `list` that holds the `Item`s
that are currently in that room.

* Add functionality to the main loop that prints out all the items that are
- 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 `Item`s to the player's inventory. The inventory can
- Add capability to add `Item`s to the player's inventory. The inventory can
also be a `list` of items "in" the player, similar to how `Item`s can be in a
`Room`.

* Add a new type of sentence the parser can understand: two words.
- Add a new type of sentence the parser can understand: two words.

* Until now, the parser could just understand one sentence form:
- Until now, the parser could just understand one sentence form:

`verb`
`verb`

such as "n" or "q".

* But now we want to add the form:
- But now we want to add the form:

`verb` `object`

such as "take coins" or "drop sword".

* Split the entered command and see if it has 1 or 2 words in it to determine
- 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 `get` followed by an `Item` name. This will be
- Implement support for the verb `get` followed by an `Item` name. This will be
used to pick up `Item`s.

* If the user enters `get` or `take` followed by an `Item` name, look at the
- If the user enters `get` or `take` followed by an `Item` name, look at the
contents of the current `Room` to see if the item is there.

* If it is there, remove it from the `Room` contents, and add it to the
`Player` contents.
- If it is there, remove it from the `Room` contents, and add it to the
`Player` contents.

* If it's not there, print an error message telling the user so.
- If it's not there, print an error message telling the user so.

* Add an `on_take` method to `Item`.
- Add an `on_take` method to `Item`.

* Call this method when the `Item` is picked up by the player.
- Call this method when the `Item` is picked up by the player.

* The `Item` can use this to run additional code when it is picked up.
- The `Item` can use this to run additional code when it is picked up.

* Add an `on_drop` method to `Item`. Implement it similar to `on_take`.
- Add an `on_drop` method to `Item`. Implement it similar to `on_take`.

* Implement support for the verb `drop` followed by an `Item` name. This is the
- Implement support for the verb `drop` followed by an `Item` name. This is the
opposite of `get`/`take`.

* Add the `i` and `inventory` commands that both show a list of items currently
- Add the `i` and `inventory` commands that both show a list of items currently
carried by the player.


## Stretch Goals

In arbitrary order:

* Add more rooms
- Add more rooms

* Add scoring
- Add scoring

* Subclass items into treasures
- Subclass items into treasures

* Add a subclass to `Item` called `LightSource`.
- Add a subclass to `Item` called `LightSource`.

* During world creation, add a `lamp` `LightSource` to a convenient `Room`.
- During world creation, add a `lamp` `LightSource` to a convenient `Room`.

* Override `on_drop` in `LightSource` that tells the player "It's not wise to
drop your source of light!" if the player drops it. (But still lets them drop
it.)
- Override `on_drop` in `LightSource` that 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 `Room` called `is_light` that is `True` if the `Room` is
naturally illuminated, or `False` if a `LightSource` is required to see what
is in the room.
- Add an attribute to `Room` called `is_light` that is `True` if the `Room` is
naturally illuminated, or `False` if a `LightSource` is required to see what
is in the room.

* Modify the main loop to test if there is light in the `Room` (i.e. if
- Modify the main loop to test if there is light in the `Room` (i.e. if
`is_light` is `True` **or** there is a `LightSource` item in the `Room`'s
contents **or** if there is a `LightSource` item in the `Player`'s contents).

* If there is light in the room, display name, description, and contents as
- 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.
- If there isn't, print out "It's pitch black!" instead.

* Hint: `isinstance` might help you figure out if there's a `LightSource`
- Hint: `isinstance` might help you figure out if there's a `LightSource`
among all the nearby `Item`s.

* Modify the `get`/`take` code to print "Good luck finding that in the dark!" if
the user tries to pick up an `Item` in the dark.
- Modify the `get`/`take` code to print "Good luck finding that in the dark!" if
the user tries to pick up an `Item` in the dark.

* Add methods to notify items when they are picked up or dropped
- Add methods to notify items when they are picked up or dropped

* Add light and darkness to the game
- Add light and darkness to the game

* Add more items to the game.
- Add more items to the game.

* Add a way to win.
- Add a way to win.

* Add more to the parser.
- Add more to the parser.

* Remember the last `Item` mentioned and substitute that if the user types
- Remember the last `Item` mentioned and substitute that if the user types
"it" later, e.g.

```
take sword
drop it
```

* Add `Item`s with adjectives, like "rusty sword" and "silver sword".
- Add `Item`s with adjectives, like "rusty sword" and "silver sword".

* Modify the parser to handle commands like "take rusty sword" as well as
- 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
- 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_take` to check the return value. If `on_take`
- Modify the code that calls `on_take` to check the return value. If `on_take`
returns `False`, 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_take` to code things like "don't allow
- This enables you to add logic to `on_take` to code things like "don't allow
the user to pick up the dirt unless they're holding the shovel.

* Add monsters.
- Add monsters.

* Add the `attack` verb that allows you to specify a monster to attack.
- Add the `attack` verb that allows you to specify a monster to attack.

* Add an `on_attack` method to the monster class.
- Add an `on_attack` method to the monster class.

* Similar to the `on_take` return value modification, above, have `on_attack`
- Similar to the `on_take` return value modification, above, have `on_attack`
prevent the attack from succeeding unless the user possesses a `sword` item.

* Come up with more stretch goals! The sky's the limit!
- Come up with more stretch goals! The sky's the limit!
44 changes: 39 additions & 5 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from room import Room
from player import Player
from item import Item

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
"North of you, the cave mount beckons", ["sword", "shield"]),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
passages run north and east.""", ["skull", "torch"]),

'overlook': Room("Grand Overlook", """A steep cliff appears before you, falling
into the darkness. Ahead to the north, a light flickers in
the distance, but there is no way across the chasm."""),
the distance, but there is no way across the chasm.""", ["rope", "potion"]),

'narrow': Room("Narrow Passage", """The narrow passage bends here from west
to north. The smell of gold permeates the air."""),
to north. The smell of gold permeates the air.""", ["knife", "apple"]),

'treasure': Room("Treasure Chamber", """You've found the long-lost treasure
chamber! Sadly, it has already been completely emptied by
earlier adventurers. The only exit is to the south."""),
earlier adventurers. The only exit is to the south.""", ["cobwebs"]),
}


Expand All @@ -38,7 +40,12 @@
#

# Make a new player object that is currently in the 'outside' room.
newPlayer = Player("Matt", room['outside'])
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + str(newPlayer) + CEND)

CBLUE2 = '\33[94m'
# Write a loop that:
#
# * Prints the current room name
Expand All @@ -49,3 +56,30 @@
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
player_input = None
while (player_input is not 'q'):
print(f'''You are in the {newPlayer.current_room.room_name}:\n
'{newPlayer.current_room.room_description}. You are holding {newPlayer.items}.'
You notice the following items:{newPlayer.current_room.items}.\n
Please pick a direction to move in (n, e, s, w). Interested in items? Need to clean out your bag? Use 'take' to add to your inventory, or 'drop' to clear your inventory.\n''')
player_input = input("Enter your action: ")
previous_room = newPlayer.current_room
if player_input != 'take' and player_input != 'drop':
newPlayer.move(player_input)
if player_input.startswith('take '):
items = player_input.split(' ')
newPlayer.pickup(items[1])
if player_input.startswith('drop '):
items = player_input.split(' ')
newPlayer.drop(items[1])
# elif player_input == 'take':
#player_input = input('What do you choose to take? ')
# newPlayer.pickup(player_input)
# elif player_input == 'drop':
#player_input = input('What do you no longer need? ')
# newPlayer.drop(player_input)
if newPlayer.current_room == None:
print(CBLUE2 + "********You are blocked by a mysterious force.********\n" + CEND)
newPlayer.current_room = previous_room
# else:
#print('''Are you sure you've played these games before?''')
9 changes: 9 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This will be the base class for specialized item types to be delcared later.
# The item should have name description attributes.
# The name should be one word for ease in parsing later.


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


class Player:
def __init__(self, player_name, current_room, items=[]):
self.player_name = player_name
self.current_room = current_room
self.items = items

def __str__(self):
return f"Greetings {self.player_name}!\n"

def move(self, direction):
if direction == 'n':
self.current_room = self.current_room.n_to
if direction == 's':
self.current_room = self.current_room.s_to
if direction == 'e':
self.current_room = self.current_room.e_to
if direction == 'w':
self.current_room = self.current_room.w_to

def pickup(self, action):
if action in self.current_room.items:
self.items.append(action)
self.current_room.items.remove(action)

def drop(self, action):
if action in self.items:
self.items.remove(action)
self.current_room.items.append(action)
Loading