HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

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

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thistextwayadventurebettergamecodethere

Problem

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

Solution

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)

Code Snippets

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...')
DECISIONS = []
CABINS = []
CONTINUES = []
EXIT_MESSAGE = 'Exiting Game...'
#First level loop
while True:
    try: firstLevel()
    except SystemExit as e:
        print(str(e))
        break
print('Nice to meet you, %s. Are you ready for your adventure?' % name)

Context

StackExchange Code Review Q#25194, answer score: 3

Revisions (0)

No revisions yet.