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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# 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.


Expand Down
256 changes: 247 additions & 9 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from room import Room
from player import Player
from item import Item
import textwrap

# Declare all the rooms

Expand Down Expand Up @@ -38,14 +41,249 @@
#

# Make a new player object that is currently in the 'outside' room.
new_player = Player("Jayne", room['outside'])


# CREATE VARIABLES FOR ROOMS
outside = room['outside']
foyer = room['foyer']
overlook = room['overlook']
narrow = room['narrow']
treasure = room['treasure']


# ADD ITEMS TO ROOMS
axe = Item("Axe", "by jove, it is sharp")
outside.stealthy_add(axe)

compass = Item("Compass", "weathered, but still works")
foyer.stealthy_add(compass)

binoculars = Item("Binoculars", "handy for spying")
overlook.stealthy_add(binoculars)

torch = Item("Torch", "batteries running low")
narrow.stealthy_add(torch)

gold = Item("Gold", "a small canvas sack filled with gold coins, they are worth a fortune!")
treasure.stealthy_add(gold)

knife = Item("Knife", "pocket knife, could be hidden in your socks")
new_player.stealthy_add(knife)

print("\n------- WELCOME TO JAYNE'S GAME ---------\n")

# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.

game_playing = True

while game_playing == True:

# * Prints the current room name
print(new_player)

# * Prints the current description (the textwrap module might be useful here).
room_description = new_player.current_room.description
wrapper = textwrap.TextWrapper(width=25)
word_list = wrapper.wrap(text=room_description)
for element in word_list:
print(" > " + element)

# USER PROMPT

# * Waits for user input and decides what to do
user_prompt = (input("\n---- WHAT NOW ? ---- \n[n], [s], [e] or [w] to move\n\n[look] to search room\n[inv] to see your stash\n[grab] [item] to grab\n[drop] [item] to drop\n[q] to quit: "))


# If the user enters a cardinal direction, attempt to move to the room there.

# NORTH
if user_prompt.lower() == 'n':
if hasattr(new_player.current_room, 'n_to'):
print("\n---- LET US GO NORTH ----\n")
new_player.current_room = new_player.current_room.n_to
else:
print("\n---- YOU SHALL NOT PASS ----")
print("---- CHOOSE ANOTHER DIRECTION ----\n")

# SOUTH
elif user_prompt.lower() == 's':
if hasattr(new_player.current_room, 's_to'):
print("\n---- LET US GO SOUTH ----\n")
new_player.current_room = new_player.current_room.s_to
else:
print("\n---- YOU SHALL NOT PASS ----")
print("---- CHOOSE ANOTHER DIRECTION ----\n")
# EAST
elif user_prompt.lower() == 'e':
if hasattr(new_player.current_room, 'e_to'):
print("\n---- LET US GO EAST ----\n")
new_player.current_room = new_player.current_room.e_to
else:
print("\n---- YOU SHALL NOT PASS ----")
print("---- CHOOSE ANOTHER DIRECTION ----\n")
# WEST
if user_prompt.lower() == 'w':
if hasattr(new_player.current_room, 'w_to'):
print("\n---- LET US GO WEST ----\n")
new_player.current_room = new_player.current_room.w_to
else:
print("\n---- YOU SHALL NOT PASS ----")
print("---- CHOOSE ANOTHER DIRECTION ----\n")


# LOOK
elif user_prompt.lower() == 'look':
#OUTSIDE
if new_player.current_room == outside:
outside.room_inventory()
# FOYER
elif new_player.current_room == foyer:
foyer.room_inventory()
# OVERLOOK
elif new_player.current_room == overlook:
overlook.room_inventory()
# NARROW
elif new_player.current_room == narrow:
narrow.room_inventory()
# TREASURE
elif new_player.current_room == treasure:
treasure.room_inventory()
# INV
elif user_prompt.lower() == 'inv':
new_player.player_inventory()

# ITEM ONLY

# AXE
elif user_prompt.lower() == 'axe':
typed_item = user_prompt.lower()

player_items = [data for data in new_player.player_items]

if typed_item in player_items:
print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n")
else:
print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n")
# COMPASS
elif user_prompt.lower() == 'compass':
typed_item = user_prompt.lower()

player_items = [f"{data.name.lower()}" for data in new_player.player_items]

if typed_item in player_items:
print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n")
else:
print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n")
# BINOCULARS
elif user_prompt.lower() == 'binoculars':
typed_item = user_prompt.lower()

player_items = [f"{data.name.lower()}" for data in new_player.player_items]

if typed_item in player_items:
print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n")
else:
print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n")
# TORCH
elif user_prompt.lower() == 'torch':
typed_item = user_prompt.lower()

player_items = [f"{data.name.lower()}" for data in new_player.player_items]

if typed_item in player_items:
print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>>You need to tell me!!\n")
else:
print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n")
# GOLD
elif user_prompt.lower() == 'gold':
typed_item = user_prompt.lower()

player_items = [f"{data.name.lower()}" for data in new_player.player_items]

if typed_item in player_items:
print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> You need to tell me!!\n")
else:
print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n")
# KNIFE
elif user_prompt.lower() == 'knife':
typed_item = user_prompt.lower()

player_items = [f"{data.name.lower()}" for data in new_player.player_items]

if typed_item in player_items:
print(f"\n\t>> What do you want me to do with {typed_item}? \n\t>> YOU NEED TO TELL ME!!\n")
else:
print(f"\n\t>> If you want to do something with {typed_item}, YOU NEED TO GO FIND IT FIRST!!!!\n")

# VERB NOUN

# GRAB ONLY
elif user_prompt.lower() == 'grab':
print("\n\tGRAB WHAT..?\nYou need to tell me!!\n")

# GRAB
elif user_prompt.lower()[:4] == 'grab':
# GRAB
grab_separate_words = user_prompt.split(' ')
command = grab_separate_words[0]
item_to_grab = grab_separate_words[1].lower()

current_room_items = [f"{data.name.lower()}" for data in new_player.current_room.room_items]

if item_to_grab in current_room_items:
print(f"\n\t>> YOU GRAB: {item_to_grab}")
if item_to_grab == 'axe':
new_player.add_player_item(axe)
elif item_to_grab == 'compass':
new_player.add_player_item(compass)
elif item_to_grab == 'binoculars':
new_player.add_player_item(binoculars)
elif item_to_grab == 'torch':
new_player.add_player_item(torch)
elif item_to_grab == 'gold':
new_player.add_player_item(gold)
elif item_to_grab == 'knife':
new_player.add_player_item(knife)
else:
print("\nThat item is not found here!!\nSearch for it elsewhere\n")

# DROP ONLY
elif user_prompt.lower() == 'drop':
print("\n\tDROP WHAT..?\nYou need to specify - I'm not psychic!\n")

# DROP
elif user_prompt.lower()[:4] == 'drop':
print("\nDrop!")
# new_player.drop_player_item(knife)
grab_separate_words = user_prompt.split(' ')
command = grab_separate_words[0]
item_to_grab = grab_separate_words[1].lower()

current_room_items = [f"{data.name.lower()}" for data in new_player.player_items]

if item_to_grab in current_room_items:
print(f"\t>> YOU DROP: {item_to_grab}")
if item_to_grab == 'axe':
new_player.drop_player_item(axe)
elif item_to_grab == 'compass':
new_player.drop_player_item(compass)
elif item_to_grab == 'binoculars':
new_player.drop_player_item(binoculars)
elif item_to_grab == 'torch':
new_player.drop_player_item(torch)
elif item_to_grab == 'gold':
new_player.drop_player_item(gold)
elif item_to_grab == 'knife':
new_player.drop_player_item(knife)
else:
print("\nThat item is not found here!!\nSearch for it elsewhere\n")

# If the user enters "q", quit the game
elif user_prompt.lower() == 'q':
print("Thank you for playing \nGoodbye!")
game_playing = False
# Print an error message if the movement isn't allowed.
else:
print("\n---- INVALID COMMAND ----\n")
print("PLEASE SELECT FROM: n, s, e, w or q\n")
7 changes: 7 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Item:
def __init__(self, name, description):
self.name = name
self.description = description

def __str__(self):
return f">> {self.name}: {self.description}"
46 changes: 46 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
from item import Item

class Player:
# constructor function
def __init__(self, name, room):
self.name = name
self.current_room = room
self.player_items = []

# string value
def __str__(self):
return f"{self.name} is currently located: {self.current_room.name}"

def stealthy_add(self, item):
self.item = item
self.player_items.append(item)

def add_player_item(self, item):
self.item = item
self.player_items.append(item)
for i in self.player_items:
print(f"\t>> {i.name} is in {self.name}'s stash")
print("\n")
self.current_room.room_items.remove(item)

def drop_player_item(self, item):
self.item = item
self.player_items.remove(item)
print(f"{item.name} removed from your stash")
self.current_room.room_items.append(item)
print(f"{item.name} left in {self.current_room.name}")
print("\n")

def player_inventory(self):
if (len(self.player_items) >= 1):
print(f"\n>> Here is what you have on your person:")
for i in self.player_items:
print(f"\t{i}")
else:
print(f"\n>> You dont have anything on your person.\nLook in rooms!\n")
print("\n")

pass



42 changes: 41 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.
from item import Item

class Room:
# construction function
def __init__(self, name, description):
self.name = name
self.description = description
self.room_items = []

def __str__(self):
return f"room name is {self.name}"

def stealthy_add(self, item):
self.item = item
self.room_items.append(item)

def add_room_item(self, item):
self.item = item
self.room_items.append(item)
for i in self.room_items:
print(f"{i.name} added to {self.name}")

def room_inventory(self):
if (len(self.room_items) == 0):
print(f"\n\t>> There's nothing in this room. That sucks. Search other rooms!\n")
elif (len(self.room_items) > 0):
print(f"\n>> Look what you found at {self.name}:")
for i in self.room_items:
print(f"\t{i}")
print(f"\n")
else:
print(f"\n\t>> There's nothing in this room. That sucks. Search other rooms!\n")





# def add_item(self, item):
# self.item = item
pass