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
199 changes: 165 additions & 34 deletions src/adv.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
from room import Room
from player import Player
from helpers import kolors
import os
import sys
from item import Item

# Declare all the rooms

room = {
'outside': Room("Outside Cave Entrance",
"North of you, the cave mount beckons"),
game_title = (f"""
{kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} Welcome to... {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} (¯`·.___.··¸.-~*´¨¯¨`*·~-.---|----.-~*´¨¯¨`*·~-.¸··.___.·´¯) {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} \ | | {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} _ \ _` | \ \ / _ \ __ \ __| | | __| _ \ {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} ___ \ ( | \ \ / __/ | | | | | | __/ {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} _/ _\ \__._| \_/ \___| _| _| \__| \__._| _| \___| {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} Text Adventure Game {kolors.ENDC}
""")

'foyer': Room("Foyer", """Dim light filters in from the south. Dusty
passages run north and east."""),
game_title_2 = (f"""
{kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} Buh Bye... {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} (¯`·.___.··¸.-~*´¨¯¨`*·~-.---|----.-~*´¨¯¨`*·~-.¸··.___.·´¯) {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} \ | | {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} _ \ _` | \ \ / _ \ __ \ __| | | __| _ \ {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} ___ \ ( | \ \ / __/ | | | | | | __/ {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} _/ _\ \__._| \_/ \___| _| _| \__| \__._| _| \___| {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} {kolors.ENDC} {kolors.BackgroundDarkGray} {kolors.ENDC}
{kolors.BackgroundDarkGray} Text Adventure Game {kolors.ENDC}
""")

'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."""),
print(game_title)

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

'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."""),
# Declare all the rooms
room = {
'outside': Room(
"Outside Cave Entrance",
"""North of you, the cave mount beckons"""
),
'foyer': Room(
"Foyer",
"""Dim light filters in from the south.\nDusty passages run north and east."""
),
'overlook': Room(
"Grand Overlook",
"""A steep cliff appears before you, falling into the darkness.\n\nAhead to the north, a light flickers in the distance,\nbut there is no way across the chasm."""
),
'narrow': Room(
"Narrow Passage",
"""The narrow passage bends here from west to north.\nThe smell of gold permeates the air."""
),
'treasure': Room(
"Treasure Chamber",
"""You've found the long-lost treasure chamber! Sadly,\nit has already been completely emptied by earlier adventurers.\nThe only exit is to the south."""
),
}


# Link rooms together

room['outside'].n_to = room['foyer']
room['foyer'].s_to = room['outside']
room['foyer'].n_to = room['overlook']
Expand All @@ -33,19 +69,114 @@
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.
items = {
'ram': Item("RAM", "RAM is your system's short-term memory. Whenever your computer performs calculations, it temporarily stores the data in the RAM until it is needed."),
'cpu': Item("CPU", "The central processing unit (CPU), also called a processor, is located inside the computer case on the motherboard. It is sometimes called the brain of the computer, and its job is to carry out commands."),
'fans': Item('Fans', 'Fans are used to cool internal case components.'),

'gpu': Item("GPU", "The video card is responsible for what you see on the monitor."),
'psu': Item("PSU", "The power supply unit in a computer converts the power from the wall outlet to the type of power needed by the computer."),
'motherboard': Item("Motherboard", "The motherboard is the computer's main circuit board. It's a thin plate that holds the CPU, memory, connectors for the hard drive and optical drives, expansion cards to control the video and audio, and connections to your computer's ports"),

'cables': Item("Cables", "Assorted cables to connect components to an from each other."),
'aio': Item("AIO", "An All-In-One liquid CPU cooler that contains a pump, fans and a radiator."),
'm.2': Item("M.2", "The hard drive is where your software, documents, and other files are stored."),

'monitor': Item("Monitor", "The monitor works with a video card, located inside the computer case, to display images and text on the screen."),
'keyboard': Item("Keyboard", "The keyboard is one of the main ways to communicate with a computer."),
'mouse': Item("Mouse", "Another important tool for communicating with computers. Commonly known as a pointing device, it lets you point to objects on the screen, click on them, and move them."),

'os': Item("OS", "The operating system (OS) manages all of the software and hardware on the computer."),
'case': Item("Case", "The metal and plastic box that contains the main components of the computer, including the motherboard, central processing unit (CPU), and power supply."),
'rgb': Item("RGB", "Assorted RGB lighting accessories.")
}

# Add items to rooms

room['outside'].add_item(items['ram'])
room['outside'].add_item(items['cpu'])
room['outside'].add_item(items['fans'])

room['foyer'].add_item(items['gpu'])
room['foyer'].add_item(items['psu'])
room['foyer'].add_item(items['motherboard'])

room['overlook'].add_item(items['cables'])
room['overlook'].add_item(items['aio'])
room['overlook'].add_item(items['m.2'])

room['narrow'].add_item(items['monitor'])
room['narrow'].add_item(items['keyboard'])
room['narrow'].add_item(items['mouse'])

room['treasure'].add_item(items['os'])
room['treasure'].add_item(items['case'])
room['treasure'].add_item(items['rgb'])

def get_user_choice():
return input(choice_message)

def concat_items(items):
result = ""

for item in items:
result += f"{kolors.Magenta}{item.name}:{kolors.ENDC} {item.description}\n"

return result

def get_user_name():
username = input(f"\n{kolors.OKGREEN}Enter your username:{kolors.ENDC}\n")

if sys.platform == 'win32':
clear = lambda: os.system('cls')
clear()
else:
clear = lambda: os.system('clear')
clear()

return Player(username, room['outside'])

playing = True
player = get_user_name()
choice_message = (f"\n{kolors.UNDERLINE}{kolors.OKBLUE}Which direction would you like to go?{kolors.ENDC}\n\n{kolors.WARNING}[n] North\n[s] South\n[e] East\n[w] West{kolors.ENDC}\n\n{kolors.OKGREEN}Navigate:{kolors.ENDC}")

while playing:
item_list = f"\n{kolors.UNDERLINE}{kolors.OKBLUE}Room items:{kolors.ENDC}\n{concat_items(player.current_room.items)}"
player_inventory = f"\n{kolors.UNDERLINE}{kolors.OKBLUE}Current inventory:{kolors.ENDC}\n{concat_items(player.inventory)}"
current_location = f"\n{kolors.UNDERLINE}{kolors.OKBLUE}Location:{kolors.ENDC}\n{player.current_room.name}\n\n{kolors.UNDERLINE}{kolors.OKBLUE}Description:{kolors.ENDC} \n{player.current_room.description}"

if sys.platform == 'win32':
clear = lambda: os.system('cls')
clear()
else:
clear = lambda: os.system('clear')
clear()

print(current_location)
print(item_list)
print(player_inventory)

choice = get_user_choice().split(' ')

if choice[0] == 'i' or choice[0] == 'inventory':
print(player_inventory)

if choice[0] == 'q':
print(game_title_2)
playing = False

if playing:
if choice[0] == 'n' or choice[0] == 's' or choice[0] == 'e' or choice[0] == 'w':
player.move(choice[0])

if len(choice) == 2 and choice[0] == 'drop':
item = items[choice[1].lower()]
player.drop_item(item)
player.current_room.add_item(item)

if len(choice) == 2 and choice[0] == 'grab':
item = items[choice[1].lower()]
player.grab_item(item)
player.current_room.remove_item(item)


57 changes: 57 additions & 0 deletions src/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class kolors:
RESETALL = '\033[0m'
STRONG = '\033[1m'
Dim = '\033[2m'
Underlined = '\033[4m'
Blink = '\033[5m'
Reverse = '\033[7m'
Hidden = '\033[8m'
ResetBold = '\033[21m'
ResetDim = '\033[22m'
ResetUnderlined = '\033[24m'
ResetBlink = '\033[25m'
ResetReverse = '\033[27m'
ResetHidden = '\033[28m'
Default = '\033[39m'
Black = '\033[30m'
Red = '\033[31m'
Green = '\033[32m'
Yellow = '\033[33m'
Blue = '\033[34m'
Magenta = '\033[35m'
Cyan = '\033[36m'
LightGray = '\033[37m'
DarkGray = '\033[90m'
LightRed = '\033[91m'
LightGreen = '\033[92m'
LightYellow = '\033[93m'
LightBlue = '\033[94m'
LightMagenta = '\033[95m'
LightCyan = '\033[96m'
White = '\033[97m'
BackgroundDefault = '\033[49m'
BackgroundBlack = '\033[40m'
BackgroundRed = '\033[41m'
BackgroundGreen = '\033[42m'
BackgroundYellow = '\033[43m'
BackgroundBlue = '\033[44m'
BackgroundMagenta = '\033[45m'
BackgroundCyan = '\033[46m'
BackgroundLightGray = '\033[47m'
BackgroundDarkGray = '\033[100m'
BackgroundLightRed = '\033[101m'
BackgroundLightGreen = '\033[102m'
BackgroundLightYellow = '\033[103m'
BackgroundLightBlue = '\033[104m'
BackgroundLightMagenta = '\033[105m'
BackgroundLightCyan = '\033[106m'
BackgroundWhite = '\033[107m'

HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
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"name: {self.name}, \ndescription: {self.description}"
40 changes: 40 additions & 0 deletions src/player.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
from helpers import kolors
import os

# Write a class to hold player information, e.g. what room they are in
# currently.
class Player:

def __init__(self, name, current_room):
self.name = name
self.current_room = current_room
self.inventory = []

def grab_item(self, item):
self.inventory.append(item)

def drop_item(self, item):
self.inventory.remove(item)

def move(self, direction):
new_room = self.current_room

if direction == 'n':
if self.current_room.n_to:
new_room = self.current_room.n_to
if direction == 's':
if self.current_room.s_to:
new_room = self.current_room.s_to
if direction == 'e':
if self.current_room.e_to:
new_room = self.current_room.e_to
if direction == 'w':
if self.current_room.w_to:
new_room = self.current_room.w_to

if self.current_room.name != new_room.name:
clear = lambda: os.system('cls')
clear()
self.current_room = new_room
else:
# clear = lambda: os.system('cls')
# clear()
print(f"{kolors.FAIL}\n############## WARNING ##############\n[{kolors.ENDC} {kolors.WARNING}You stepped on a Lego! Try again.{kolors.ENDC} {kolors.FAIL}]\n############## WARNING ##############{kolors.ENDC}\n\n")
20 changes: 19 additions & 1 deletion 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.
# description attributes.
class Room:
def __init__(self, name, description):
self.name = name
self.description = description
self.n_to = None
self.s_to = None
self.e_to = None
self.w_to = None
self.items = []

def __str__(self):
return f"name: {self.name}, \ndescription: {self.description}"

def add_item(self, item):
self.items.append(item)

def remove_item(self, item):
self.items.remove(item)