# Is there a better way to code this text adventure game?

This is my first attempt with a basic text adventure game, and I was wondering if there was a way to make it more efficient (or any other things I could add to make the game better). It isn't totally finished yet, but if there is a way to make it better, please post in the comments.

#lets me use the exit function
import sys
#Lets me use the time function
import time
#Want to play loop

while True:
print 'Good, let us start our great adventure!'
break
print 'That is a real shame...'
time.sleep(1)
print 'Exiting program in 5 seconds:'
time.sleep(1)
print '5'
time.sleep(1)
print '4'
time.sleep(1)
print '3'
time.sleep(1)
print '2'
time.sleep(1)
print '1'
time.sleep(1)
sys.exit('Exiting Game...')
break
else:
print 'That is not a valid answer... Try again'
time.sleep(2)

#First level loop
while True:
print 'You awaken on the ground of a great forest. You can see two possible things to         do, enter the cabin to your left, or wander through the woods.'
print ' '

print 'Where do you want to go?'
FirstDecision = raw_input('cabin/woods ')
#Cabin option
if FirstDecision == 'cabin':
print 'You approach the door to the cabin, luckily it is unlocked.'
Cnt = raw_input('Enter < to continue')
print 'You open the door and walk through, only to hear a dry and shaky voice say:'
print '\"Get out.\"'
Cnt = raw_input('Enter < to continue')
print 'What do you do?'
FirstCabin = raw_input('leave/fight ')
if FirstCabin == 'leave':
print 'As you run out the door, the voice shoots and kills you.'
Cnt = raw_input('Enter < to continue')
print ' '
FirstRetry = raw_input('Try again? (y/n)')
if FirstRetry == 'y':
print 'Restarting back at checkpoint...'
time.sleep(2)
elif FirstRetry == 'n':
print 'That\'s a shame...'
time.sleep(1)
print 'Exiting program in 5 seconds:'
time.sleep(1)
print '5'
time.sleep(1)
print '4'
time.sleep(1)
print '3'
time.sleep(1)
print '2'
time.sleep(1)
print '1'
time.sleep(1)
sys.exit('Exiting Game...')
break
elif FirstCabin == 'fight':
print 'You turn to where the voice came from and you bluntly say \"No.\"

• while True: ready = raw_input('y/n ') : if I'm not wrong and if the indentation is not wrong, this would loop forever. – SylvainD Apr 17 '13 at 22:07

A couple things. First of all, you should abstract the function that exits the game:

def exitGame():
print 'That\'s a shame...'
print 'Exiting program in 5 seconds:'
for i in range(5):
time.sleep(1)
print(i+1)
sys.exit('Exiting Game...')


Second, I would probably push Cabins, retries, etc. into arrays.

DECISIONS = []
CABINS = []
CONTINUES = []


Third, I would define the first level as a function so you can do the gracefully exit:

EXIT_MESSAGE = 'Exiting Game...'
#First level loop
while True:
try: firstLevel()
except SystemExit as e:
print(str(e))
break


One last thing. Use print as a function and not a statement. And maybe it's personal preference, but I find string formatting strongly preferable:

print('Nice to meet you, %s. Are you ready for your adventure?' % name)

• nitpick: those are lists not arrays – Winston Ewert Apr 18 '13 at 1:14
• Array is a general term for the type of data structure. I used lists above but you could use many other types of array depending on your needs. – Adrian Larson Apr 18 '13 at 14:22
• You could argue that, and that's why its just a nitpick. Typically in python arrays refer to fixed-size arrays from either the array module or numpy. – Winston Ewert Apr 18 '13 at 14:31

One thing that stuck out to me is line 38 of your code is 158 characters long:

print 'You awaken on the ground of a great forest. You can see two possible things to         do, enter the cabin to your left, or wander through the woods.'


Below are the first two sentences of the "Maximum Line Length" section in PEP 8:

Limit all lines to a maximum of 79 characters.

For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.

To break line 38 into shorter lines in your source, you can store it in a variable inside parenthesis:

s1 = ('You awaken on the ground of a great forest. '
'You can see two possible things to do, enter '
'the cabin to your left, or wander through the woods.')


Note that with the above, the output will still be one continuous line of text without line breaks.