I am new to Python and I have not yet done any OOP. I wrote this text adventure as a first project and I would like to know if there is a better and more efficient way to write my code within its current paradigm. Any suggestions or critique are more than welcome.
The code is quite long. I don't expect anyone to read all of it.
#!/usr/bin/env python
import cmd
import random
import time
import sys
import os
#### Effects ####
def print_slow(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
print("\n")
def load_effect(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.5)
#### Title Screen ####
def menu():
print("\n")
print("SHADOWS OVER LAURENDALE")
print("Version 1.0")
action = input("> ")
if action.lower() == "p":
intro()
elif action.lower() == "q":
sys.exit()
elif action.lower() == "i":
info_menu()
elif action.lower() == "h":
help_menu()
while action.lower() not in ['p', 'q', 'i', 'h']:
print("Invalid option! Enter 'p' to launch the game,")
print("'q' to quit, 'i' for info, or 'h' for help.")
action = input("> ")
if action.lower() == "p":
intro()
elif action.lower() == "q":
sys.exit()
elif action.lower() == "i":
info_menu()
elif action.lower() == "h":
help_menu()
def info_menu():
print("www.shadows-over-laurendale.github.io")
action = input("> ")
if action.lower() == "p":
intro()
elif action.lower() == "q":
sys.exit()
elif action.lower() == "i":
info_menu()
elif action.lower() == "h":
help_menu()
while action.lower() not in ['p', 'q', 'i', 'h']:
print("Invalid option! Enter 'p' to launch the game,")
print("'q' to quit, 'i' for info, or 'h' for help.")
action = input("> ")
if action.lower() == "p":
intro()
elif action.lower() == "q":
sys.exit()
elif action.lower() == "i":
info_menu()
elif action.lower() == "h":
help_menu()
def help_menu():
print("Enter commands to interact with the world.")
print("For more info and a list of commands, consult the manual.")
action = input("> ")
if action.lower() == "p":
intro()
elif action.lower() == "q":
sys.exit()
elif action.lower() == "i":
info_menu()
elif action.lower() == "h":
help_menu()
while action.lower() not in ['p', 'q', 'i', 'h']:
print("Invalid option! Enter 'p' to launch the game,")
print("'q' to quit, 'i' for info, or 'h' for help.")
action = input("> ")
if action.lower() == "p":
intro()
elif action.lower() == "q":
sys.exit()
elif action.lower() == "i":
info_menu()
elif action.lower() == "h":
help_menu()
#### Player ####
player_health = 100
player_inventory = ["Map"]
player_gold = 50
#### Main Scenes ####
def intro():
print("\n")
load_effect("...loading...")
print("\n")
print_slow("'Wake up, Arris...'")
print_slow("'You've been sleeping all day!'")
print_slow("'Why don't you go outside and do something?'")
print_slow("'It's so lovely today. Be back for dinner!'")
print("\n")
print_slow("'Okay...'")
print("\n")
village()
def village():
global player_gold
global player_health
print("\n")
print("You are in the village.")
while True:
action = input("> ")
if action == "1":
river()
if action == "2":
farmlands()
elif action == "3":
forest()
elif action == "4":
shop()
elif action == "5":
home()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the river(1), the farmlands(2), the forest(3), the shop(4), home(5).")
elif action == "e":
print("Sounds of laughter and loud conversation can be heard in this lively village.")
print("What a lovely place! I was born here and my heart will always be here...")
elif action == "t":
print("A merchant on the street waves you over to where he is.")
print("'Care for some fresh bread, young man? It's only 5 gold.' He asks.")
print("You have",player_gold,"gold.")
print("Yes(y) or no(n)?")
action = input("> ")
if action == "y":
if "Bread" in player_inventory:
print("Looks like you already have some bread, kid!")
print("Eat it up and come back... Hehe.")
elif player_gold >= 5:
player_inventory.append("Bread")
player_gold -= 5
print("'Here you go!'")
print("You take the bread from the man.")
elif player_gold < 5:
print("Sorry you don't have enough! Ask your mother for a little gold! Hehe.")
elif action == "n":
print("Okay. Maybe next time!")
else:
print("Invalid command! Try again.")
action = input("> ")
if action == "y":
if player_gold >= 5:
player_inventory.append("Bread")
player_gold -= 5
print("'Here you go!'")
print("You take the bread from the man.")
elif player_gold < 5:
print("Sorry you don't have enough! Ask your mother for a little gold! Hehe.")
elif action == "n":
print("Okay. Maybe next time!")
else:
print("Invalid command! Try again.")
def river():
global player_gold
global player_health
print("\n")
print("You are by the river.")
while True:
action = input("> ")
if action == "1":
village()
elif action == "2":
farmlands()
elif action == "3":
forest()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the farmlands(2), the forest(3).")
elif action == "e":
print("I can hear the water flowing gently and the birds are singing...")
print("A small boy is out here with his fishing net.")
elif action == "t":
print("The little boy with his net look over at you.")
print("'Have you ever tried crossing this river?'")
print("'It looks so beautiful on the other side.'")
print("'I better get back to catching the fishes.'")
print("'My papa will be proud!'")
if "Raft" in player_inventory:
print("/n")
print("Will you cross the river? Yes(y) or no(n)?")
action = input("> ")
if action == "n":
print("That river looks dangerous anyways...")
print("It's infested with snakes.")
elif action == "y":
outskirts()
else:
print("Invalid command! Try again.")
action = input("> ")
if action == "n":
print("That river looks dangerous anyways...")
print("It's infested with snakes.")
elif action == "y":
outskirts()
else:
print("Invalid command! Try again.")
def farmlands():
global player_gold
global player_health
global ruby_saved
print("\n")
print("You are in the farmlands.")
while True:
action = input("> ")
if action == "1":
village()
elif action == "2":
river()
elif action == "3":
forest()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the forest(3).")
elif action == "e":
print("Rosy red tomatoes and evergreen lettuce are growing from the ground...")
print("A farmer waves to you and smiles.")
elif action == "t":
print("'Have you seen my lovely Ruby?' The farmer asks.")
print("'She's run away again.'")
print("He looks very concerned.")
else:
print("Invalid command! Try again.")
def forest():
global player_gold
global player_health
print("\n")
print("You are in the forest.")
while True:
action = input("> ")
if action == "1":
village()
elif action == "2":
river()
elif action == "3":
farmlands()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3).")
elif action == "e":
print("This is a far way from home...")
print("The sound of a girl screaming can be faintly heard.")
print("I'm afraid to go on.")
if "Sword" in player_inventory:
print("\n")
print("But I do have a weapon to protect myself.")
print("Shall I go on? Yes(y) or no(n)?")
action = input("> ")
if action == "n":
print("I won't go any further...")
elif action == "y":
forest_two()
else:
print("Invalid command! Try again.")
action = input("> ")
if action == "n":
print("I won't go any further...")
elif action == "y":
forest_two()
elif action == "t":
print("There's no one to talk to here...")
else:
print("Invalid command! Try again.")
#### Sub Scenes ####
def home():
global player_gold
global player_health
print("\n")
print("You are at home.")
while True:
action = input("> ")
if action == "1":
village()
elif action == "2":
river()
elif action == "3":
farmlands()
elif action == "4":
forest()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3), the forest(4).")
elif action == "e":
print("A warm fire is crackling in the hearth...")
print("Something smells delicious!")
elif action == "t":
print("You call for your mother.")
print("She comes inside from the back door and smiles.")
print("'There you are! Now what have you been up to?'")
if player_health < 100:
print("\n")
print("'You're hurt! Let me tend to your wounds.'")
player_health = 100
print("You feel better. You now have",player_health,"health.")
else:
print("Invalid command! Try again.")
def outskirts():
global player_gold
global player_health
print("\n")
print("You are in the outskirts.")
while True:
action = input("> ")
if action == "1":
village()
elif action == "2":
river()
elif action == "3":
farmlands()
elif action == "4":
forest()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3), the forest(4).")
elif action == "e":
print("You can see the boy fishing from across the river.")
if "Sword" not in player_inventory:
print("You see a shiny item lying in the grass.")
print("Will you take it? Yes(y) or no(n)?")
action = input("> ")
if action == "n":
print("Some things are better left where you found them.")
elif action == "y":
print("You reach for it. It's a beautiful and sharp sword!")
print("But as soon as you grab it, a snake bites your hand!")
snake_attack = random.randint(1,100)
player_health -= snake_attack
player_inventory.append("Sword")
else:
print("Invalid command! Try again.")
action = input("> ")
if action == "n":
print("Some things are better left where you found them.")
elif action == "y":
print("You reach for it. It's a beautiful and sharp sword!")
print("But as soon as you grab it, a snake bites your hand!")
snake_attack = random.randint(1,100)
player_health -= snake_attack
player_inventory.append("Sword")
elif action == "t":
print("You yell to the boy across the river.")
print("'Hey! How did you get over there?'")
else:
print("Invalid command! Try again.")
def shop():
global player_gold
global player_health
print("\n")
print("You are in the shop.")
while True:
action = input("> ")
if action == "1":
village()
elif action == "2":
river()
elif action == "3":
farmlands()
elif action == "4":
forest()
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3), the forest(4).")
elif action == "e":
print("A friendly woman is standing at the counter.")
print("'Hello! Can I help you, young man?'")
elif action == "t":
print("'Please, browse our wares.'")
print("'We have what you need.'")
print("'Do you want to see what we have?'")
print("Yes(y) or no(n)?")
action = input("> ")
if action == "y":
print("You can buy:")
print("a fishing pole(f) for 10 gold, a leather tunic(t) for 20 gold,")
print("a guitar(g) for 30 gold, and a raft(r) for 15 gold.")
action = input("> ")
if action == "f":
if "Fishing poll" in player_inventory:
print("I aleady have one!")
elif player_gold >= 10:
player_inventory.append("Fishing poll")
player_gold -= 10
print("'Here you go!'")
print("You bought a lovely handmade fishing poll.")
elif player_gold < 5:
print("Sorry you don't have enough for this, kid.")
elif action == "t":
if "Tunic" in player_inventory:
print("I aleady have one!")
elif player_gold >= 20:
player_inventory.append("Leather tunic")
player_gold -= 20
print("'Here you go!'")
print("You bought a bulky leather tunic.")
elif player_gold < 20:
print("Sorry you don't have enough for this, kid.")
elif action == "g":
if "Guitar" in player_inventory:
print("I aleady have one!")
elif player_gold >= 30:
player_inventory.append("Guitar")
player_gold -= 30
print("'Here you go!'")
print("You bought a gorgeous guitar.")
elif player_gold < 30:
print("Sorry you don't have enough for this, kid.")
elif action == "r":
if "Raft" in player_inventory:
print("I aleady have one!")
elif player_gold >= 15:
player_inventory.append("Raft")
player_gold -= 15
print("'Here you go!'")
print("You bought a small wooden raft.")
elif player_gold < 15:
print("Sorry you don't have enough for this, kid.")
else:
print("Invalid command! Try again.")
elif action == "n":
print("'Okay, no worries! I just love your company.'")
print("'Tell your friends about this place, okay?'")
else:
print("Invalid command! Try again.")
print("'Please, browse our wares.'")
print("'We have what you need.'")
print("'Do you want to see what we have?'")
print("Yes(y) or no(n)?")
action = input("> ")
else:
print("Invalid command! Try again.")
def forest_two():
global player_gold
global player_health
print("\n")
print("You are deep into the forest.")
while True:
action = input("> ")
if action == "1":
print("I don't know how to get there. I'm lost.")
elif action == "2":
print("I don't know how to get there. I'm lost.")
elif action == "3":
print("I don't know how to get there. I'm lost.")
elif action == "4":
print("I don't know how to get there. I'm lost.")
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3), the forest entrance(4)")
elif action == "e":
print("You are in a part of the forest you've never seen before.")
print("You are lost...")
elif action == "t":
print("You call for help.")
print("You suddenly hear the girl scream again.")
print("This time louder...")
print("Will you investigate?")
print("Yes(y) or no(n)?")
action = input("> ")
if action == "n":
print("You decide not to go.")
print("A wolf howls in the distance.")
print("The moon glistening above you.")
elif action == "y":
final()
else:
print("Invalid command! Try again.")
else:
print("Invalid command! Try again.")
def final():
global player_gold
global player_health
print("\n")
print("You are even further into the woods.")
print("You see an old, abandoned cabin just ahead.")
while True:
action = input("> ")
if action == "1":
print("I can't go there. I'm lost!")
elif action == "2":
print("I can't go there. I'm lost!")
elif action == "3":
print("I can't go there. I'm lost!")
elif action == "4":
print("I can't go there. I'm lost!")
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3), the forest entrance(4).")
elif action == "e":
print("The screaming is now even closer...")
print("You are frozen with terror.")
elif action == "t":
print("You call for the girl.")
print("She can hear you now!")
print("'Help me! Please, help me!'")
print("Her voice sounds like it's coming from the cabin.")
print("Will you go?")
print("Yes(y) or no(n)?")
action = input("> ")
if action == "n":
print("You hide behind some trees.")
print("You look behind you into the forest.")
print("There's no turning back.")
elif action == "y":
cabin()
else:
print("Invalid command! Try again.")
else:
print("Invalid command! Try again.")
def cabin():
global player_gold
global player_health
print("\n")
print("You are right outside of the cabin.")
print("You draw your sword...")
while True:
action = input("> ")
if action == "1":
print("No turning back.")
elif action == "2":
print("No turning back.")
elif action == "3":
print("No turning back.")
elif action == "4":
print("No turning back.")
elif action == "h":
print("Health:",player_health)
elif action == "i":
print("Inventory:")
for items in player_inventory:
print(items)
elif action == "g":
print("Gold:",player_gold)
elif action == "m":
print("Map: the village(1), the river(2), the farmlands(3), the forest entrance(4).")
elif action == "e":
print("You walk up to the door and raise your sword.")
print("Will you enter? Yes(y) or no(n)?")
action = input("> ")
if action == "n":
print("You wait to regain your composure.")
print("You muster up a little courage.")
print("Something terrible is waiting behind the door.")
elif action == "y":
print("\n")
print("You open the door. It creaks loudly as peak inside.")
print("You see a small girl with red hair lying in the corner.")
print("'Help me! Someone brought me here and he's going to kill me!'")
print("She sobs hopelessly.")
print("You ask her where he is.")
print("'I don't know.' She says. 'He left a few minutes ago to get an axe!'")
print("'Please help me! My name is Ruby. My dad is looking for me!'")
time.sleep(10)
print("\n")
print("A man in a black robe bursts into the cabin with a bloody axe!")
time.sleep(5)
print("\n")
print("Ruby screams.")
print("'No! Please. I didn't do anything...'")
print("'Help me, boy!'")
time.sleep(5)
print("\n")
print("The man raises his axe to slice you apart.")
print("But you swiftly parry his attack with your sword.")
time.sleep(5)
print("\n")
print("You raise your sword to strike back.")
print("Ruby puts her hands over her face.")
time.sleep(5)
print("\n")
print("You slice into the man's shoulder.")
time.sleep(5)
print("\n")
print("He strikes again...")
enemy_attack = random.randint(1,100)
player_health -= enemy_attack
print("He swings his axe furiously toward you...")
time.sleep(5)
print("\n")
print("He cuts into your arm as you moan in agony!")
print("Ruby screams.")
time.sleep(5)
print("\n")
print("You fall on the floor and drop your sword...")
if player_health > 0:
time.sleep(5)
print("\n")
print("In a last burst of strength,")
print("You lift up the sword with both hands")
print("and charge toward the shadowy figure.")
time.sleep(5)
print("\n")
print("You cut into the man's chest and pierce his heart.")
time.sleep(5)
print("\n")
print("He falls to the floor.")
print("He lies in a puddle of his own blood.")
time.sleep(5)
print("\n")
print("Ruby runs toward you, crying.")
print("'You saved my life!' She exclaims enthusiastically.")
print("You drop your sword, exausted")
time.sleep(5)
print("\n")
print("She embraces you, tightly.")
print("'Please take me home to my daddy's farm...' She begs.")
time.sleep(5)
print("\n")
print("You both return to the farm.")
print("The farmer sees you and his daughter walking toward the farm.")
print("'Ruby! You're back!'")
time.sleep(5)
print("\n")
print("'You were the boy I saw yesterday!'")
print("You shrug in agreement.")
time.sleep(5)
print("\n")
print("'You found my daughter, boy!' He says excitedly.")
print("'I can't thank you enough.'")
print("Ruby runs home to her father.")
print("She looks back at you and smiles.")
time.sleep(5)
print("\n")
print("You return home.")
print("Your mother must be worried sick!")
elif player_health <= 0:
print("You fall unconscious as the hooded figure drags you away...")
sys.exit
else:
print("Invalid command! Try again.")
elif action == "t":
print("You anxiously hyperventilate. You cannot make a sound.")
else:
print("Invalid command! Try again.")
menu()