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
4 changes: 3 additions & 1 deletion examples/guessing_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ def guessing_game():
print("Too big!")

if __name__ == '__main__':
guessing_game()
guessing_game()

# init
114 changes: 97 additions & 17 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from room import Room
from player import Player
from item import Item

# Declare items available

sword = Item('Sword', 'A short and rusty blade, must of been left here ages ago!')
shield = Item('Shield', 'a small wooden shield')

# Declare all the rooms

Expand All @@ -21,7 +28,6 @@
earlier adventurers. The only exit is to the south."""),
}


# Link rooms together

room['outside'].n_to = room['foyer']
Expand All @@ -33,19 +39,93 @@
room['narrow'].n_to = room['treasure']
room['treasure'].s_to = room['narrow']

#
# Main
#

# Make a new player object that is currently in the 'outside' room.

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

# Name input
name = input("What is your adventurer's name? ")

# Player object starts 'outside'
player = Player(room['outside'], name)

# Greeting for when you enter the game
print(f"\nGreetings {player.playerName}, you can control your adventure using the keys: N, E, S, W to move around the dungeon and Q to end your time here")

# Add items to foyer room
def add_items_to_foyer():
if player.currentRoom.name != room['foyer'].name:
room['foyer'].items.pop()
room['foyer'].items.pop()
if player.currentRoom.name == room['foyer'].name:
room['foyer'].items.append(sword)
room['foyer'].items.append(shield)


# game loop
game_over = False

while game_over == False:

player_move = input("\n[N] North, [E] East, [S] South, [W] West, [Q] Quit, [I] Items >> ").upper()

# Inventory inputs

if player_move == 'I':

print(*player.playerItem, sep = "\n")

item_input = input("[Drop] or [Take] [Item] >> ").upper()

if item_input == 'DROP SWORD':
player.playerItem.remove(sword)
print(f'\n{sword.itemName} has been dropped')

if item_input == 'TAKE SWORD':
player.playerItem.append(sword)
print(f"{sword.itemName} has been picked up")

if item_input == 'DROP SHIELD':
player.playerItem.remove(shield)
print(f'\n{shield.itemName} has been dropped')

if item_input == 'TAKE SHIELD':
player.playerItem.append(shield)
print(f"{shield.itemName} has been picked up")


# Player movement

if player_move == 'N' or player_move == 'E' or player_move == 'S' or player_move == 'W' or player_move == 'Q':

if player_move == 'N' and player.currentRoom.n_to != None:
player.currentRoom = player.currentRoom.n_to
add_items_to_foyer()
print(player.currentRoom)

elif player_move == 'E' and player.currentRoom.e_to != None:
player.currentRoom = player.currentRoom.e_to
add_items_to_foyer()
print(player.currentRoom)

elif player_move == 'S' and player.currentRoom.s_to != None:
player.currentRoom = player.currentRoom.s_to
add_items_to_foyer()
print(player.currentRoom)

elif player_move == 'W' and player.currentRoom.w_to != None:
player.currentRoom = player.currentRoom.w_to
add_items_to_foyer()
print(player.currentRoom)

elif player_move == 'N' and player.currentRoom.n_to == None or player_move == 'E' and player.currentRoom.e_to == None or player_move == 'S' and player.currentRoom.s_to == None or player_move == 'W' and player.currentRoom.w_to == None:
print('This seems to be the wrong way, please choose a different direction')

elif player_move == 'Q':
print("Good bye!")
game_over = True



# else:
# print('Invalid command, please choose from the given options')
11 changes: 11 additions & 0 deletions src/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This will be the base class for specialized item types to be declared later.
# The item should have name and description attributes.
# Hint: the name should be one word for ease in parsing later.

class Item:
def __init__(self, itemName, itemDes):
self.itemName = itemName
self.itemDes = itemDes

def __str__(self):
return f"{self.itemName}: {self.itemDes}"
10 changes: 10 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
# Write a class to hold player information, e.g. what room they are in
# currently.


class Player:
def __init__(self, currentRoom, playerName, playerItem = []):
self.currentRoom = currentRoom
self.playerName = playerName
self.playerItem = playerItem

def __str__(self):
return f"{self.playerName} is currently in room: {self.currentRoom}"
19 changes: 18 additions & 1 deletion src/room.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# Implement a class to hold room information. This should have name and
# description attributes.
# description attributes.

class Room:
def __init__(self, name, description, n_to = None, s_to = None, e_to = None, w_to = None, items = []):
self.name = name
self.description = description
self.n_to = n_to
self.s_to = s_to
self.e_to = e_to
self.w_to = w_to
self.items = items

def __str__(self):
if len(self.items) > 0:
return f"\nYou are currently in {self.name}.\n{self.description}\n\nYou spot some items in the distance\n\n{self.items[0]}\n{self.items[1]}"
else:
return f"\nYou are currently in {self.name}.\n{self.description}"