nathan virgil wrote:
I'm very new to Python, and (to a slightly lesser extent) programming in general. I'd like to get some experience by practicing simple-yet-functional programs, with a bit of an emphasis on games. The first game I'd like to attempt would be a simple, non-linear story, similar to those choose-your-adventure books. I don't want to start with anything too complicated, like having mapped-out directions, or interactive objects, although I do eventually want to get into stuff like that.


Python seems to me like it would be a good language for this sort of stuff. I figure I just need to use a lot of print, if/elif/else, raw_input(), and a ton and a half of variables. My problem at the moment is that I don't know how to get from one section of the story to the next. I vaguely remember reading about some language using a "goto" command for something like this, but I'm not sure how that would be handled in Python.

A rough idea of what I'm trying to do (in a presumably hypothetical language) would be something like this:

0100  print "Ahead of you, you see a chasm.
0200 jump = raw_input("Do you wish to try jumping over it? Y/N")
0300 if jump = Y:
0400       goto 1700
0500 if jump = N:
0600      goto 2100

Does this make any sense?

Yes

Is there some way I could do this in Python?

Definitely. Python is a "structured programming language". This means it has no GOTO statement.
You could, for starters, use the following algorithm:

room = 1
while True:

 # retrieve data for current room
 if room == 1:
   desc = "Ahead of you, you see a chasm."
   ques = "Do you wish to try jumping over it? Y/N"
   destY = 2
   destN = 3
 elif room == 2:
   desc = "Ahead of you, you see a warty green ogre."
   ques = "Do you wish to eat it? Y/N"
   destY = 4
   destN = 5
 # etc for the rest of the rooms

 # ask current question and move to next room
 print ques
 ans = raw_input(ques).upper() # allow for lower case input
 if ans == "Y":
   room = destY
 elif ans == "N":
   room = destN
 elif ans == "Q": # give us a way out.
   break  else:
   print "Please answer Y or N"

Start with this. Try to get it running. Then come back with questions.
Notice that I have separated the "data" (description of rooms and flow between rooms) from the program logic.
This makes things a LOT easier.
There are more complicated structures in Python that make game programming a LOT easier and more flexible that the above.

--
Bob Gailer
919-636-4239 Chapel Hill, NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to