Hi -
I'm working on a command-line game. Is there anything wrong with having each 'chapter' of the game be a function that links to other chapters by calling them? I only ask because when a recent traceback returned about 40 lines worth of error message, I realized that the functions are all being run 'within each other' (ah-ha!).
Technically it's OK but conceptually it isn't really right. After all, when you finish a chapter you don't go back to the chapter you came from!
I would use a central dispatcher. Each chapter function could return a token indicating which chapter is next, or it could return the actual next chapter function.
Here is a simple example:
#########
def dispatch(chapter):
while chapter is not None:
chapter = chapter()def chapter1():
print 'This is chapter 1'
raw_input('Press return to continue')
return chapter2def chapter2():
print 'You see a heffalump.'
yn = raw_input('Do you kill the heffalump? ')
if yn == 'y':
return chapter3
else:
return chapter4def chapter3():
print 'The heffalump is dead. Congratulations, you win!'
return Nonedef chapter4():
print 'The heffalump kills you and eats you.'
print 'You have died!'
return Nonedispatch(chapter1)
#############
Kent
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
