Le Wednesday 25 June 2008 12:16:24 Jacqui, vous avez écrit :
> Hi, I'm a total newbie too, and I'm kind of replying to see if my
> instinct on the whole GOTO thing is correct. It's hard to learn a
> language without any feedback!
>
> I used GW and Color Basic when I was a kid so I know all about GOTO (and
> it was a mess! I programmed one of those interactive stories in grade 12
> using it, it took all semester and was anything but elegant!)
>
> I would expect with Python, instead of using GOTO you use defined
> functions.
>
> So for instance, you could define chapters as functions
>
> I just tried this bit out
>
> def chapter1():
> print "this is an interactive story test"
> print
>
> def chapter2():
> print "You have chosen to leap across the chasm"
> print "Sadly you forgot you are carrying an anvil"
> print "What part of b-bye don't you understand?"
>
> def chapter3():
> print "You wisely chose to turn back home"
> print "The anvil you are carrying would surely cause you"
> print "to plummet to your death"
>
>
>
> def main():
> chapter1()
> print "You come across a chasm, would you like to jump?"
> jumpQ = raw_input("y/n: ")
> if jumpQ == "y":
> chapter2()
> elif jumpQ == "n":
> chapter3()
> main()
>
> and it worked although I have doubts about how good it would be overall
> once the story got very involved. I'm interested to see what others
> think.
>
You instinct is not too bad :) but the main function will soon become
unreadable and very hard to modify. In your examples the functions act as
simple strings, you could write it like this:
chapter1 = "This is an interactive story test"
chapter2 = "..."
def main () :
print chapter1
...
One problem is that in such a game you often go back on your step and read the
same chapter several times (ie there might be cycles in the story graph).
This would be impossible to do this way. But the idea is not so bad, if the
game logic is rather handled by the chapter function itself:
def chapter1 () :
print "You come across a chasm, would you like to jump?"
jumpQ = raw_input("y/n: ")
if jumpQ == "y":
chapter2()
elif jumpQ == "n":
chapter3()
def chapter2 () :
print "OK, but you can still change your mind"
r = raw_input("go back to the chasm ?")
if r == "y" :
chapter1()
else :
chapter4()
def main () :
chapter1()
Which introduces another problem: as functions/chapters will be called in
chain (chapter1 calls chapter2 which calls chapterX...), you will soon fill
the system stack and end with a 'maximum recursion error'. But in python
functions are objects which can be handled like anything else, so they could
return the destination chapter rather than calling it directly, and a main
loop would do the job of calling them (note there is no () after the "return
chapterX", so we return the function itself without calling it) :
def chapter1 () :
print "..."
r = raw_input("y/n: ")
if r == "y" :
return chapter2
else :
return chapter3
def chapter2 () :
...
def main () :
# Store the 'chapter1' function in 'current_chapter'
# without calling it
current_chapter = chapter1
while True :
# Call the function stored in current_chapter
# and keep the returned function object.
# Could also be written as 'current_chapter = current_chapter()'
next_chapter = current_chapter()
current_chapter = next_chapter
This way the 'main' function won't have to be modified and the content of each
chapter is kept in a simple logical unit. It will make the future
modifications much easier.
--
Cédric Lucantis
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor