3
\$\begingroup\$

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

    #asks for name
name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, '+name+'. Are you ready for your adventure?'
while True:
    ready = raw_input('y/n ')
if ready == 'y':
    print 'Good, let us start our great adventure!'
    break
elif ready == 'n':
    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.\"``
\$\endgroup\$
  • \$\begingroup\$ while True: ready = raw_input('y/n ') : if I'm not wrong and if the indentation is not wrong, this would loop forever. \$\endgroup\$ – SylvainD Apr 17 '13 at 22:07
3
\$\begingroup\$

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)
\$\endgroup\$
  • 2
    \$\begingroup\$ nitpick: those are lists not arrays \$\endgroup\$ – Winston Ewert Apr 18 '13 at 1:14
  • \$\begingroup\$ 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. \$\endgroup\$ – Adrian Larson Apr 18 '13 at 14:22
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Winston Ewert Apr 18 '13 at 14:31
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.