Skip to content
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The `/src` directory contains the files `adv.py`, which is where the main logic
* Put the Player class in `player.py`.
* Players should have a `name` and `current_room` attributes

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

Expand Down
142 changes: 136 additions & 6 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from room import Room
from player import Player
from item import Item
from monster import Monster
import random as r
import textwrap

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
Expand All @@ -22,8 +26,28 @@
}


# Link rooms together
# Declare all the items
item = {
'gauntlet': Item('Gauntlet', 'Some glove with a lot of shiny rocks in it', 4),
'lightsaber': Item('Lightsaber', 'Flashlight weapon', 3),
'katana': Item('Katana', 'A freakin Katana!', 4),
'battery': Item('Battery', 'Just a AA battery', 0),
'spatula': Item('Spatula', 'It is a spatula, pretty straight forward', 5),
'sword': Item('Sword', 'Looks sharp, be carful with that', 2),
'bat': Item('Bat', 'SWING!', 3),
}


# Adding some monster to the dugeon
monster = {
'dracula': Monster('Dracula', 8, 1, item['bat']),
'godzilla': Monster('Godzilla', 12, 2, item['katana']),
'pikachu': Monster('Pikachu', 4, 1, item['lightsaber']),
'robot': Monster('Robot', 8, 1, item['battery'])
}


# Link rooms together
room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
Expand All @@ -33,11 +57,117 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

#
# Main
#

# Link items to a room, hardcoding sword to first room to prepare player for combat
room['outside'].item_list = [item['sword']]
room['foyer'].item_list = [item[r.choice([*item.keys()])], item[r.choice([*item.keys()])]]
room['overlook'].item_list = [item[r.choice([*item.keys()])], item[r.choice([*item.keys()])]]
room['narrow'].item_list = [item[r.choice([*item.keys()])]]
room['treasure'].item_list = [item[r.choice([*item.keys()])]]


# Link monsters to a room, no monster in first room
room['foyer'].monster_list = [monster['pikachu']]
room['overlook'].monster_list = [monster['dracula']]
room['narrow'].monster_list = [monster['robot']]
room['treasure'].monster_list = [monster['godzilla']]


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


# Dictionary that holds directional input and the value tied to the input
directions = {'go north':'n_to', 'go east':'e_to', 'go south':'s_to', 'go west':'w_to'}


# Welcome's player to game and allows custom name input also shows player their hp
def intro():
player.name = input('What is your name Traveler?\n')
print(f'\nWelcome to Generic Adventure Game Title, {player.name}')
roll()
print(f'You have {player.hp} health points! Be cautious!\n')


# Random num generator for health and attack points
def roll():
player.hp = r.randint(5,10)


# Check if the user is going entering a new room, if they are print the new room info
def location(player, prev_room = ''):
if player.current_room.name != prev_room:
print(f'\nCurrent Room: {player.current_room.name}')
print(textwrap.fill(f'Description: {player.current_room.description}\n', 50))
print('In this room:')
for i in player.current_room.item_list:
print(f'{i.name}')
print('\nMonster in this room:')
for i in player.current_room.monster_list:
print(f'{i.name}\nHP: {i.hp}')


# A quick view of a players inventory, prints name and description
def inventory(player):
print('\nInventory:')
for i in player.inventory:
print(f'Item Name: {i.name}\nDescription: {i.description}\nAttack: {i.attack}')


# A view of all input options
def inputChoices():
print('All Input`s Allowed:\n')
print('Go North\nGo South\nGo East\nGo West\nTake `item`\nDrop `item`\nEquip `item`\nFight `monster`\nInventory\nHelp\nQuit')


# Introduce the player and let them know where they are currently
intro()
print(f'\nCurrent Room: {player.current_room.name}')
print(textwrap.fill(f'Description: {player.current_room.description}\n', 50))
print('In this room:')
for i in player.current_room.item_list:
print(f'{i.name}')


# Loop that checks player actions and decides what to return
while True:
cmd = input('\nWhat would you like to do\nType help for help\n').lower()
actions = cmd.split(' ')
if cmd in directions:
prev_room = player.current_room.name
player.current_room = player.current_room.enterRoom(directions[cmd])
location(player, prev_room)
elif actions[0] == 'take':
player.pickUp(player.inventory, item[actions[1]])
print('Inventory: ')
for i in player.inventory:
print(f'{i.name}')
elif actions[0] == 'drop':
player.drop(player.inventory, item[actions[1]])
print('Inventory: ')
for i in player.inventory:
print(f'{i.name}')
elif actions[0] == 'fight':
player.combat(player.inventory[-1], monster[actions[1]])
for i in player.current_room.monster_list:
i.fightBack(player)
if player.hp <= 0:
print('You have died')
break
elif actions[0] == 'equip':
player.equip(item[actions[1]])
elif cmd == 'remind':
location(player, prev_room)
elif cmd == 'inventory':
inventory(player)
elif cmd == 'help':
inputChoices()
elif cmd == 'quit':
print('Thanks for playing! \n')
break
else:
print('That is not a doable action \n')


# Write a loop that:
#
Expand All @@ -48,4 +178,4 @@
# 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.
# If the user enters "q", quit the game.
5 changes: 5 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Item:
def __init__(self, name, description, attack):
self.name = name
self.description = description
self.attack = attack
12 changes: 12 additions & 0 deletions src/monster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Monster:
def __init__(self, name, hp, attack, item=[]):
self.name = name
self.hp = hp
self.attack = attack
self.item = item
# Cannot let the user have all the fun
def fightBack(self, player):
if player.hp > 0:
player.hp -= self.attack
print(f'You were hit for {self.attack} damage!')
print(f'You have {player.hp} hp left.')
44 changes: 42 additions & 2 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:
def __init__(self, name, current_room, hp, inventory=[]):
self.name = name
self.current_room = current_room
self.hp = hp
self.inventory = inventory
# Pick up item adds to player.inventory while removing from room.item_list
def pickUp(self, inventory, item):
if item in self.current_room.item_list:
print(f'\nYou picked up {item.name}')
inventory.append(item)
self.current_room.item_list.remove(item)
else:
print('That item is not in here')
# Drops item removes from player.inventory while adding to room.item_list
def drop(self, inventory, item):
if item in inventory:
print(f'\nYou dropped {item.name}')
inventory.remove(item)
self.current_room.item_list.append(item)
else:
print('You cannot drop an item you do not have')
# Combat allows a player to fight a monster if monster has no health the monster should drop an item
def combat(self, item, monster):
if monster in self.current_room.monster_list:
monster.hp -= item.attack
print(f'You hit {monster.name} with {item.name} for {item.attack} damage!')
print(f'{monster.name} has {monster.hp} hp left!')
if monster.hp <= 0:
self.current_room.item_list.append(monster.item)
self.current_room.monster_list.remove(monster)
print(f'You defeated {monster.name} and they dropped {monster.item.name}!')
else:
print('No reason to fight in here')
# Equip will put the chosen item at the end of the list, making it default for fights
def equip(self, item):
if item in self.inventory:
self.inventory.remove(item)
self.inventory.append(item)
print(f'You have equipped {item.name}')
else:
print('You do not have that item')
22 changes: 20 additions & 2 deletions src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# Implement a class to hold room information. This should have name and
# description attributes.
class Room:
def __init__(self, name, description, item_list=[], monster_list=[]):
self.name = name
self.description = description
self.item_list = item_list
self.monster_list = monster_list
self.n_to = self
self.e_to = self
self.s_to = self
self.w_to = self
# Check if the current_room has the directions that the player inputs, if not player stays in current_room and print error
def enterRoom(self, directions):
new_room = self.__getattribute__(directions)
if new_room == self:
print('\nYou explore in that direction and find no where to go\n')
return new_room