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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Up to this point, you've gotten your feet wet by working on a bunch of small Pyt
## What We're Building
[What's an Adventure Game? ![vid](https://tk-assets.lambdaschool.com/7928cdb4-b8a3-45a6-b231-5b9d1fc1e002_ScreenShot2019-03-22at5.47.28PM.png)](https://youtu.be/WaZccFqJUT8)

## Examples

* Dunnet, one of the first mass distributed text based games included in every version of the emacs editor
(emacs is no longer included by default with MacOS Catalina+)
[link](https://ifdb.tads.org/viewgame?id=ig3zbeoqfv4v1xl8)
* Links to 5 text based adventure games that can be played in browser

## Goals

Expand Down
3 changes: 3 additions & 0 deletions src/.vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
6 changes: 6 additions & 0 deletions src/.vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
Binary file added src/.vs/slnx.sqlite
Binary file not shown.
Binary file added src/.vs/src/v16/.suo
Binary file not shown.
137 changes: 121 additions & 16 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import textwrap
from room import Room
from player import Player
from item import Item
import sys
import random
import os

# Declare all the rooms
# create all valid user inputs
direction_commands = ['n', 's', 'e', 'w']
exit_commands = ['q', 'quit', 'exit']
help_commands = ['?', 'help']
item_commands = ['get', 'drop', 'inv']

valid_commands = direction_commands + exit_commands + help_commands + item_commands
# Declare all the rooms
room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
"North of you, the cave mount beckons"),

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
Expand All @@ -19,8 +31,23 @@
'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."""),

'graveyard': Room("Graveyard", """A dark mist prevents you from seeing more
than a few feet in front of you. The floor is littered with tiny bones."""),

'lava': Room("Lava", """A room filled with lava that will be harmful to
the player when the developers find time to make it so soon(TM)""")
}

# Declare all items

item = {
'ring': Item("Ring", "This old rusty ring has what looks to be a dragon wrestling a lion."),

'box': Item("Blossom", "An immaculately preserved flower from another age."),

'fang': Item("Fang", "A massive tooth from an unknown predator." ),
}

# Link rooms together

Expand All @@ -32,20 +59,98 @@
room['narrow'].w_to = room['foyer']
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

#
# Main
#
room['graveyard'].e_to = room['foyer']
room['foyer'].w_to = room['graveyard']
room['lava'].e_to = room['graveyard']
room['graveyard'].w_to = room['lava']

# Make a new player object that is currently in the 'outside' room.
player = Player("Mike", room['outside'])
test_item = Item("Talisman", "A worn silver pendant believed to guard travelers on dangerous journeys.")
player.item_spawn(test_item)

done = False

# helper function to skip invalid inputs
def skip_input():
print("I don't understand that!\n")

def print_help_text():
print("""
Valid commands:
-[n]: move north
-[s]: move south
-[e]: move east
-[w]: move west
-[q]: quit
-[help]: help text
""")

# for each room, adds a random item from item list to its inventory

# instead, there's a bug where each subsequent room has 1 more item than the previous room
def seed_items():
print("SEEDING ITEMS IN WORLD \n")
for key in room:
# print(key)
# import ipdb; ipdb.set_trace()
room[key].item_spawn(test_item)
# room[key].show_inventory()
print(room[key].name, room[key].inventory)

seed_items()
import ipdb; ipdb.set_trace()

# Write a loop that runs the game until quit command
while not done:
# * Prints the current room name
print("You are currently at", player.location.name)
# * Prints the current description (the textwrap module might be useful here).
for line in textwrap.wrap(player.location.print_description()):
print(line)
print("\n")
# print(player.location.show_inventory())
# * Waits for user input and decides what to do.
command = input("What do you want to do? ")

# check that the command is properly formatted
if command not in valid_commands:
skip_input()
print_help_text()
continue

elif command in direction_commands:
player.location = player.move_to(command, player.location)
continue
#
elif command in ['inv']:
print("SHOWING INVENTORY")
player.show_inventory()
continue
# these are hacky and the entire thing should be done with arg parse :(
# this just loots all
elif command in ['get']:
for item in player.location.inventory:
player.item_take(item)
continue
# and this just drops all
elif command in ['drop']:
for item in player.inventory:
player.item_give(item)
continue
# 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.
elif command in exit_commands:
done = True
print("Exiting game!")
sys.exit(0)

elif command in help_commands:
print_help_text()
continue

# 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.
else:
skip_input()
continue
10 changes: 10 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# this is currently identical to Room, decide if its worth

class Item:
def __init__(self, name: str, description: str):
self.name = name
self.description = description
# variables are public so don't need getters
# def get_name(self):
# return self.name
# import ipdb; ipdb.set_trace()
49 changes: 49 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
# Write a class to hold player information, e.g. what room they are in
# currently.

from typing import List, Generic
from item import Item
from room import Room
from inventory_holder import Inventory_Holder

class Player:
def __init__(self, name, location, inventory: List[Item]=[]):
self.name = name
self.location = location
self.inventory = inventory

def show_inventory(self):
print("This is what's in our inventory: \n")
for item in self.inventory:
print(item.name)

def item_take(self, item: Item):
self.inventory.append(item)
print(f"{item.name} has been removed from {self.location.name}'s inventory")
self.location.remove_item(item)
print(f"{item.name} has been added to {self.name}'s inventory")

def item_give(self, item: Item):
self.inventory.remove(item)
print(f"{item.name} has been removed from {self.name}'s inventory")
self.location.inventory.append(item)
print(f"{item.name} has been added to {self.location.name}'s inventory")



# spawn item in inventory
def item_spawn(self, item: Item):
self.inventory.append(item)
print("{item.name} has been spawned and added to Player {self.name}'s inventory")

def move_to(self, direction, current_loc):
# try to move in the specified direction
attribute = direction + '_to'

# if we can move in specified direction from our current location
if hasattr(current_loc, attribute):
# get the room in the specified
return getattr(current_loc, attribute)

# if we can't go that way
print("You can't go that way\n")

return current_loc
33 changes: 32 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

from typing import List
from item import Item
# from inventory_holder import Inventory_Holder

class Room:
def __init__(self, name: str, description: str, inventory: List[Item] = []):
self.name = name
self.description = description
self.inventory = []

def show_inventory(self):
print("This is what's in our inventory: \n")
# this is how to check for empty list in Python

for item in self.inventory:
print(item)

def remove_item(self, inventory):
for i in self.inventory:
if inventory.name == i.name:
self.inventory.remove(i)

# spawn item in inventory
def item_spawn(self, item: Item):
self.inventory.append(item)
# print(f"{item.name} has been spawned and added to {self.name}'s inventory")
print(item.name, "has been spawned to", self.name, "and added to their inventory")

def print_description(self):
return f"{self.description}"