[Tutor] Hands-on beginner's project?
Cédric Lucantis
omer at no-log.org
Tue Jun 24 19:16:55 CEST 2008
Le Tuesday 24 June 2008 17:37:00 nathan virgil, vous avez écrit :
> 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
hmm, back to the 80's :)
Hard-coding all your story would be a very bad method and a nightmare to
maintain. Ideally, such a game would be written in two parts: an 'engine'
which contains all the logic, and a set of datas, containing the story itself
in some special form understood by the engine. One of the multiple advantages
of this approach is that you will be able to easily write new stories by
reusing the engine with different datas (this is what we call a 'mod' in
modern games)
> Does this make any sense? Is there some way I could do this in Python? Any
> and all help is definitely appreciated!
Python is a good choice to learn programing. Below is a simple example. It
might look complicated to a beginner, but if you take the time to read the
python tutorial you should be able to understand it. Of course feel free to
ask for more help about it.
# ------------------------------------------------------------------
# --- GAME ENGINE ---
# A global map of the nodes
NODES = {}
# Print one node's text and ask the user for a choice.
# Return the next node or None if the game is over.
#
def process_node (node) :
tag = node[0]
text = node[1]
options = node[2]
# print the title
print
print tag
print '-' * len(tag)
print
# print the node's text
print text
print
# if there are no options, the game is over
if not options :
print 'GAME OVER'
return None
# print the various options
index = 1
for opt in options :
opt_text = opt[0]
opt_tag = opt[1]
print 'If you want to', opt_text, 'then press', index
index = index + 1
# read the user's choice
print
choice = int(raw_input("Your choice: "))
opt = options[choice-1] # -1 because an array starts at 0
return NODES[opt[1]]
# Start the story.
#
def run () :
# global initialisation
for node in NODES_LIST :
tag = node[0]
NODES[tag] = node
# find the starting node
node = NODES["start"]
# let's play now
while node :
node = process_node(node)
print "See you for a new exciting adventure!"
# --- GAME DATAS ---
# All your story goes here. A 'node' is a paragraph of the story and is made
# of three things:
#
# * a 'tag' identifying the node and used to jump from one to another
# (all tags must be unique)
#
# * the text of the paragraph
#
# * a list of user choices, each option being itself a list a two items:
# - a text to display
# - the name (tag) of the destination node
#
NODES_LIST =[ [ "start",
"You're in front of a door.",
[["open the door", "open_door"],
["go back", "go_back"]] ],
[ "open_door",
"Cool, you found the Holy Graal - YOU WON!",
[] ],
[ "go_back",
"A troll is in the way",
[["fight it", "fight_troll"],
["run away", "start"]] ],
[ "fight_troll",
"Sorry, it's stronger than you and you die - YOU LOST!",
[] ]
]
# start the game
run()
# ------------------------------------------------------------------
--
Cédric Lucantis
More information about the Tutor
mailing list