Skip to content

Instantly share code, notes, and snippets.

@Phuseos
Last active May 3, 2017 11:10
Show Gist options
  • Save Phuseos/797640ffeb630c6f84abeb6e58870d7b to your computer and use it in GitHub Desktop.
Save Phuseos/797640ffeb630c6f84abeb6e58870d7b to your computer and use it in GitHub Desktop.
Simple text adventure game
# Made by Phuseos
# https://gist.github.com/Phuseos/
# Thanks for playing!
# Runs best using Python 2.7 and up
# ToFix : inventory doesn't work yet, repeat commmands crash
import sys # General usage
import math # Used for calculations (duh)
#import pdb
#pdb.set_trace()
# Define global stats, will be randomized at the start of the game later.
glob_Name = None # Global name
glob_Level = 1 # Starting level
glob_HP = 5 # HP the player has, starting is 5
glob_Str = 1 # Strength, aids in attack rolls
glob_Dex = 1 # Dexterity, aids in traps and evasion
glob_Cha = 1 # Charisma, aids in pursuasion
# Story flags to use
glob_Story_BdLc = 0 # Story, lit bedroom (player has not lit the candle yet if this is 0)
glob_Story_BdOd = 0 # Story flag, checks if the player has opened the bedroom door yet.
# Global answers that the player can use
yes = set(['yes','y','ye','ok','']) # Empty space so people can hit enter instead of typing something
no = set(['no','n'])
# Global actions to check for
look = set(['look','lookat','view'])
use = set(['use'])
talk = set(['talk','speak'])
go = set(['go','move'])
exit = set(['exit', 'quit', 'q'])
character = set(['status', 'stats'])
help = set(['help'])
# ToDo : Listen action
take = set(['take'])
inventory = set(['inventory', 'inv'])
# Inventory
inventory = set([])
# Complete action set for checking
completeSet = set(['look','lookat','view', 'use', 'talk','speak', 'go','move', 'exit', 'quit', 'q', 'status', 'stats', 'help', 'take', 'inventory', 'inv'])
def exitSystem(showPrint):
#Bye bye
if showPrint == True:
print 'Thanks for playing!'
sys.exit
return
def getName():
# Define useable results for usage in intro loop
global yes
global no
# ToDo : check for null
nameEntered = False
while nameEntered == False:
raw_GetName = raw_input("Greetings and welcome! Before we begin our adventure, what is your name?\n")
if len(raw_GetName) > 1:
nameEntered = True
elif len(raw_GetName) <= 1:
print "I think I misheard you, let's try again."
raw_CorrectName = raw_input("Your name is " + raw_GetName + ", is this correct?\n")
if raw_CorrectName.lower() in yes: # Loop over the set to see if the player input matches a known keyword
if raw_CorrectName.lower() is not None:
global glob_Name
glob_Name = raw_GetName # Set the global variable to remember the name
return True
elif raw_CorrectName.lower() is None:
return False
elif raw_CorrectName.lower() in no:
return False
else:
sys.stdout.write("Please respond with 'yes' or 'no'\n")
return False
def showCommands():
# Print commands that can be used
print ("The commands you can use are look, use, talk, go, take, exit, inventory and status.\n")
return
def showInventory():
# Prints all the items the user is holding, if it's nothing, don't bother.
global inventory
if inventory is not None:
for i in inventory:
if i > 1:
print("".join(str(e) for e in i) + ", ")
else:
print("".join(str(e) for e in i))
else:
print("You don't have any items in your inventory.")
def showStatus():
# Prints the user stats
print("Your current stats: \n")
global glob_Str
global glob_Dex
global glob_Cha
global glob_HP
print("Hit points: " + str(glob_HP) + "\n")
print("Strength: " + str(glob_Str) + "\n")
print("Dexterity: " + str(glob_Dex) + "\n")
print("Charisma: " + str(glob_Cha) + "\n")
return
def checkSet(input): # Checks if the action matches one known in the set
global completeSet
if input.lower() in completeSet:
return True
elif input.lower() not in completeSet:
return False
def addToInventory(item): # Add an item to the inventory
global inventory
if item is not None:
inventory.add(item)
return True
else:
return False
def removeFromInventory(item): # Remove an item from the inventory, needs to be tested
global inventory
inventory.remove(item)
# #
# GAME START #
# #
# Begin by getting the name of the player
while getName() == False:
print("Let's try that again, then!\n")
getName() # Loop until the user has the correct name
# Now that we have the name, let the adventure begin!
print ("Great, welcome, " + glob_Name + "! If you want to stop playing, simply type 'Exit'.\n To show your stats, type 'status' and to see all commands, type 'help'.")
# Define the correct answer, player has to use the matches to advance
lightCandle = "use matches"
# Loop control, if the matches have been used, continue with the story.
candleLit = False
# Make sure the complete question (you awaken in a dark room etc) is only printed once
repeat = False
# Loop for the intro scene
while (candleLit) == False:
if repeat == False:
playerAction = raw_input("You open your eyes to find yourself lying in a dark room. A small strip of light shines under a door, you can hear faint noises coming from outside. What do you do?\n")
repeat = True # Set once
elif repeat == True:
playerAction = raw_input("What do you do?\n")
if playerAction.lower() == lightCandle:
candleLit = True
break
elif playerAction.lower() == "help":
showCommands()
repeat = True
else:
if checkSet(playerAction) == True:
if playerAction.lower() in exit:
exitSystem(True)
break
elif playerAction.lower() in character:
showStatus()
elif playerAction.lower() in look:
print("You're in a dark room, you can faintly make out the bed you're lying in, and next to you is a candle with some matches.\n")
elif playerAction.lower() in use:
print("Use what?\n")
elif playerAction.lower() in go:
print("Go where? It's too dark too see!\n")
elif playerAction.lower() in talk:
print("There doesn't seem to be anybode to talk to.\n")
elif checkSet(playerAction) == False:
print "I'm sorry, I didn't understand that.\n"
# Player used the matches, let's move on
if candleLit:
print("You light the candle using the matches. The room illuminates, revealing a bookcase and a door.")
print("There's no windows on either wall, making it uncertain what time of day it is.")
print("The room is unfamiliar to you, and you seem to have no recollection of how you ended up here.")
print("You decide to get out of bed and investigate.\n")
glob_Story_BdLc = 1
while glob_Story_BdOd == 0 and glob_Story_BdLc == 1:
# Next part, the player has lit up the room, now they've got to exit the room by using the door.
playerAction = raw_input("What do you do?\n") # Always fire so the user knows they can perform the next action
strPA = playerAction.lower()
lookables = (["room", "books", "bookcase"])
splitString = None # String that will hold additional command if user has given one.
input_CheckList = [None, None] # List that checks if the user has given more than one command
if strPA not in completeSet:
# If it's not an unknown command, check if it's multiple
input_CheckList = strPA.split()
#print input_CheckList
if 0 <= index < len(input_CheckList):
splitString = input_CheckList[1]
#print strPA # For debugging
if strPA in exit or input_CheckList[0] in exit:
exitSystem(True)
break
elif strPA in character or input_CheckList[0] in character: # Show stats
showStatus()
elif strPA in look or input_CheckList[0] in look: # Look action
if splitString is not None:
if splitString.lower() == "door":
print("It's a sturdy looking wooden door, a small strip of light shines from under it.\n")
elif splitString.lower() == "room":
print("The room has a nice hardwoord floor. The ceiling and walls seem to be made of either cobblestone or bricks, it's hard to tell. It's pretty cold here, too.\n")
elif splitString.lower() == "candle":
print("It's an ordinairy candle, made from wax.\n")
elif splitString.lower() == "matches":
print("There's 4 simple wooden matches lying on the nightstand besides the bed, one of them is used.\n")
elif splitString.lower() == "bookcase" or splitString.lower() == "books":
print("The bookcase stands at the end of the room, against the wall. Upon closer inspection, it seems to be used as general storage, as there are no books but a few articles of clothing, bedsheets and a few assorted pieces of pottery.\n")
else:
print ("Look at what? \n")
elif strPA in use or input_CheckList[0] in use: # Use action
print("Use what?\n")
elif strPA in go or input_CheckList[0] in go: # Go action
print("Go where? \n")
elif strPA in help or input_CheckList[0] in help: # Show commands
showCommands()
elif strPA in talk or input_CheckList[0] in talk: # Talk action
print("There doesn't seem to be anybode to talk to.\n")
elif strPA in inventory or input_CheckList[0] in inventory: # Prints a list of held items
showInventory()
elif strPA in take or input_CheckList[0] in take: # Take command
if splitString is not None:
if splitString.lower() == "matches": # Allows the user to add the matches to their inventory
print("You take the unused matches and put them in your pocket.\n")
addToInventory("matches")
showInventory()
else:
print("Take what?\n")
else:
print "I'm sorry, I didn't understand that.\n"
# Game finished
#exitGame = raw_input("Thanks for playing! More story and gameplay will be added soon.\n Press any key to exit this program.\n").lower()
#exitSystem(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment