Skip to content

Commit

Permalink
Optimised spacing of the code (main.py is now under 350 lines!), fixe…
Browse files Browse the repository at this point in the history
…d an ending that crashed the game and made options more clear

Co-authored-by: Yummy_Bacon5 <[email protected]>
  • Loading branch information
Jessicolonthree and YummyBacon5 committed Dec 10, 2024
1 parent af8c53a commit d9b83b9
Showing 1 changed file with 17 additions and 58 deletions.
75 changes: 17 additions & 58 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
GAME_WORLD = {
"Cave Entrance": {
"description": (
"You are the town's protector, staying in the local tavern. After hearing rumors of a mysterious person-eating monster within the nearby caves, you decide to investigate.\n"
"You stand at the entrance of the Cave of Shadows with your armour, sword and lantern. The air is thick with the scent of damp earth and decay.\n"
"You can't see much past where you stand, and the only sound is your own thumping heartbeat. Do you have the courage to step inside?"
"You step into the darkness. The light from your lantern dances on the walls as you walk deeper into the cave.\n"
"It's eerily quiet except for the sound of your footsteps echoing against the stone walls.\n"
"Suddenly, the path ahead splits into two."
),
"options": [
{"name": "Enter the cave, hopefully uncovering the rumors", "next": "Dark Tunnel"},
{"name": "Turn back, you need another day to prepare", "next": "Village"}
{"name": "Take the freightening narrow path", "next": "Narrow Path"},
{"name": "Take the menacing wide path", "next": "Wide Path"}
],
"items": []
},


"Village": {
"description": (
"You turn away from the dark cave, choosing to play it safe. The walk back to the village is a calm one, as you return to the cozy tavern.\n"
Expand All @@ -36,10 +34,8 @@
"options": [
{"next": "Sleep"},
],
"items": []
},


"Sleep": {
"description": (
"You wake up the next morning, feeling refreshed. The village bustles with activity as the sun rises.\n"
Expand All @@ -51,21 +47,6 @@
],
},


"Cave Entrance": {
"description": (
"You step into the darkness. The light from your lantern dances on the walls as you walk deeper into the cave.\n"
"It's eerily quiet except for the sound of your footsteps echoing against the stone walls.\n"
"Suddenly, the path ahead splits into two."
),
"options": [
{"name": "Take the freightening narrow path", "next": "Narrow Path"},
{"name": "Take the menacing wide path", "next": "Wide Path"}
],
"items": []
},


"Narrow Path": {
"description": (
"You crawl through the tight space, your lantern flickering nervously as the walls close in on you.\n"
Expand All @@ -76,10 +57,8 @@
{"name": "Attempt to climb out and escape", "next": "Tragic Ending 1 - Falling to your doom!"},
{"name": "Shuffle along to the other side of the tight space", "next": "Pit Ledge Escape"}
],
"items": []
},


"Pit Ledge Escape": {
"description": (
"Being careful with your footing, you shuffle along and find a path on the other side.\n"
Expand All @@ -90,10 +69,8 @@
{"name": "Take the wide path this time", "next": "Wide Path"},
{"name": "Leave the cave, those rumors probably aren't true anyway", "next": "Neutral Ending - Safe and sound!"}
],
"items": []
},


"Wide Path": {
"description": (
"You follow the wider path toward a faint glow in the distance. Soon, you find yourself in a large cavernous room.\n"
Expand All @@ -104,10 +81,8 @@
{"name": "Open the chest, maybe it has something powerful!", "next": "Treasure Room - Chest Opened"},
{"name": "Leave the cave, it could be deadly...", "next": "Neutral Ending - Safe and sound!"}
],
"items": []
},


"Treasure Room - Chest Opened": {
"description": (
"You open the chest and find a crystal relic pulsing with energy. As you grasp it, a monstrous creature emerges from the shadows.\n"
Expand All @@ -120,53 +95,45 @@
{"name": "Throw the Relic as a distraction and attack", "next": "Tragic Ending 3 - Killed by the Creature!"},
{"name": "Throw down all items and equipment and flee the caves", "next": "Escaping the Treasure Room"}
],
"items": ["Crystal Relic"]
},


"Escaping the Treasure Room": {
"description": (
"You throw everything you have down and quickly remove all your armour, making you lighter and faster.\n"
"While the Creature gobbles up your belongings, you sprint your way out of the cave and head back to village.\n"
"You could tell the Mayor about this, but would he believe you? Maybe you should just rest it off..."
),
"options": [
{"name": "Inform the village Mayor about the Creature and prepare an army to defeat it", "next": "Good Ending 2 - Victory!"},
{"name": "Inform the village Mayor about the Creature and prepare an army to defeat it", "next": "Good Ending 1 - Victory!"},
{"name": "Head back to your room in the tavern and rest, hoping the Creature goes away", "next": "Neutral Ending - Safe and sound!"}
],
"items": []
},


"Good Ending 1 - Victory!": {
"description": (
"You and your grand army storm towards the cave, weapons drawn and battle cries filling you with determination.\n"
"Swords slash into flesh, the Creature roars and your blood fills with fury!\n"
"Together, you all charge the creature and strike it down, claiming an easy victory and 50 percent off your stay at the local tavern."
),
"options": [],
"items": []
"options": []
},

"Good Ending 2 - Blinded by the light!": {
"description": (
"You raise the relic high, the moonlight reflecting off it and into the Creature's eyes, blinding it.\n"
"Seizing the opportunity, you strike with your sword, killing it.\n"
"Thanks to your heroic venture, the Creature shall be fed no more and the village is safe! (selling the Relic made you pretty rich too!)"
),
"options": [],
"items": ["Crystal Relic", "Ancient Treasure"]
), # Check teams for my final suggestions
"options": []
},


"Tragic Ending 1 - Falling to your doom!": {
"description": (
"You struggle to climb, but the walls are covered in a mysterious ooze.\n"
"As the floor becomes less and less, you have nothing to stand on and fall downwards.\n"
"The cave claims another helpless victim, the rumors forever a mystery..."
),
"options": [],
"items": []
},

"Tragic Ending 2 - Snack for the Creature!": {
Expand All @@ -176,7 +143,6 @@
"With noone alive to tell about the Creature, the town reminds none the wiser about it lurking within the cave..."
),
"options": [],
"items": []
},

"Tragic Ending 3 - Killed by the Creature!": {
Expand All @@ -186,25 +152,21 @@
"So the rumor was true! Although I doubt you can tell anyone about it now..."
),
"options": [],
"items": []
},


"Neutral Ending - Safe and sound!": {
"description": (
"You decide that the dangers of the cave are too great. Retracing your steps, you carefully make your way back to the entrance.\n"
"The cool night air greets you as you step outside. Though you leave the cave empty-handed, you feel relieved to be alive.\n"
"The rumors shall stay as just rumors - for now."
),
"options": [],
"items": []
"options": []
}
}


class PlayerDataType(TypedDict):
current_location: str
inventory: list[str]
game_name: str
last_updated: int

Expand All @@ -226,7 +188,7 @@ def get_game_name_syntax(
exclude_last_update: bool | None = False
) -> list[str]:
arr: list[str] = []
for i, game in enumerate(game_data):
for game in game_data:
name = shlex.quote(game["game_name"])
last_updated = datetime.fromtimestamp(game["last_updated"]).strftime("%d-%m-%Y %H:%M:%S")
if exclude_last_update:
Expand All @@ -237,13 +199,11 @@ def get_game_name_syntax(

class PlayerData():
current_location: str
inventory: list[str]
game_name: str
last_updated: int

def __init__(self):
self.current_location = list(GAME_WORLD.keys())[0]
self.inventory = []

def _print_section(self, title: str, content: str, color=Fore.GREEN):
print(f"{color}{Style.BRIGHT}\n{'-' * 40}\n{title.upper()}\n{'-' * 40}\n{Style.RESET_ALL}{content}\n")
Expand All @@ -253,7 +213,7 @@ def play_game(self):

if not location["options"]:
self._print_section(f"Ending: {self.current_location}", location["description"], Fore.CYAN)
print(f"{Fore.BLUE}This is the end of the path. Thank you for playing!{Style.RESET_ALL}")
print(f"{Fore.BLUE}This is the end of the game. Thank you for playing!{Style.RESET_ALL}")
return False

self._print_section(f"Location: {self.current_location}", location["description"], Fore.YELLOW)
Expand All @@ -278,7 +238,7 @@ def delete_game(self):
if all_player_datas:
game_names = get_game_name_syntax(all_player_datas)
game_names.append(f"{Fore.RED}Go back{Style.RESET_ALL}")
choicen_save_int = get_valid_arr_input(f"{Fore.CYAN}Choose a game to delete: {Style.RESET_ALL}", game_names)
choicen_save_int = get_valid_arr_input(f"{Fore.CYAN}Choose a save to delete: {Style.RESET_ALL}", game_names)
if choicen_save_int == len(game_names) - 1:
return
all_player_datas.pop(choicen_save_int)
Expand All @@ -293,7 +253,7 @@ def new_game(self):
while True:
input_name = get_valid_input(f"{Fore.CYAN}What will you name this save? {Style.RESET_ALL}")
if input_name in all_game_names:
print(f"{Fore.RED}This name is already taken. Please enter another{Style.RESET_ALL}")
print(f"{Fore.RED}This name is already taken, please enter another.{Style.RESET_ALL}")
continue
self.game_name = input_name
break
Expand All @@ -311,7 +271,6 @@ def load_game(self):
loaded_save = all_player_datas[choicen_save_int]

self.current_location = loaded_save["current_location"]
self.inventory = loaded_save["inventory"]
self.game_name = loaded_save["game_name"]
self.last_updated = loaded_save["last_updated"]
print(f"{Fore.GREEN}Loaded game: {shlex.quote(self.game_name)}{Style.RESET_ALL}")
Expand Down Expand Up @@ -355,8 +314,8 @@ def slow_print(text: str, delay: float = .1):

while True:
choice = get_valid_arr_input(
f"{Fore.CYAN}Choose an option:{Style.RESET_ALL}\n",
[f"{Fore.GREEN}New Game{Style.RESET_ALL}", f"{Fore.YELLOW}Load Game{Style.RESET_ALL}", f"{Fore.RED}Delete Games{Style.RESET_ALL}", f"{Fore.MAGENTA}Exit{Style.RESET_ALL}"]
f"{Fore.CYAN}Please choose an option:{Style.RESET_ALL}\n",
[f"{Fore.GREEN}Start a new game{Style.RESET_ALL}", f"{Fore.YELLOW}Load a saved game{Style.RESET_ALL}", f"{Fore.RED}Delete a saved game{Style.RESET_ALL}", f"{Fore.MAGENTA}Exit the game{Style.RESET_ALL}"]
)
player = PlayerData()

Expand All @@ -368,7 +327,7 @@ def slow_print(text: str, delay: float = .1):
player.delete_game()
continue
elif choice == 3:
print(f"{Fore.GREEN}Thanks for playing! Goodbye.{Style.RESET_ALL}")
print(f"{Fore.GREEN}Thank your for playing Cave of Shadows! Goodbye.{Style.RESET_ALL}")
WAITING_TIME = 1
for i in range(WAITING_TIME):
print(f"{Fore.RED}Exiting in {WAITING_TIME - i}s...{Style.RESET_ALL}", end="\r")
Expand Down

0 comments on commit d9b83b9

Please sign in to comment.