[Tutor] A Couple of Questions

Doug Stanfield DOUGS@oceanic.com
Fri, 23 Feb 2001 16:16:13 -1000


[Britt Green wrote:]
> Here is my situation: Ever since playing Zork way back when, 
> I've really 
> wanted to write my own text adventure. I've also wanted to 
> learn to code. So 
> I'm combining the two and learning Python by, amongst other 
> things, writing 
> up a very simple text adventure.

Its a great way to start.  I think I did something similar, pre- OOP
paradigm in some dark age.

> I'm still in the pre-coding stages of it. There's a couple of 
> problems I've 
> encountered that I'm hoping some of you can help me with.

The first problem is that you're in the PRE-coding stage instead of jumping
right in.  ;-) There is nothing like programming to learn how to program.

> The first is classes. I'm thinking of having everything in my 
> game fit 
> within five classes: Player, Rooms, Objects, Mobs and Game. 
> The first four 
> of these are self-explanatory. The Game class is sort of a 
> catch-all for 
> different things, like the UI and maybe networking code and 
> whatnot. Can 
> anyone think of a better way to break things up?

What you are describing is what you should be trying.  The level of analysis
you've done is sufficient to begin writing code.  There is an old axiom of
programming that you write at least one to throw away.  In other words, in
the analysis and design stages you'll never think of all the right ways to
do it.  Begin, and assume you'll have to start over and then you won't be
disapointed when it happens.

The advent of object oriented methodologies has made this a lot less painful
than it used to be.  There is even some formalism arising around it.  At
some point do yourself a favor and look into unit testing, refactoring, and
Extreme Programming.  Even if you don't do professional programming the
style espoused is good to know about.

If I were you I'd begin by putting together a module file with exactly those
classes you have above.  Start adding attributes to the classes and methods
as you think of the need.  Make sure the file runs from the very first and
don't add more code than you can get to run something minimally useful or
fun.  Share your specific questions here as you progress.  This may lead you
to redefine the whole thing with a different set of classes, but at least
you'll have some basis to discuss the answers the next time you ask about it
here.

>   Secondly, if I want to put the information in a text file, 
> rather than 
> hard-coded into the program, what would be a good format for 
> it, and how can 
> I make Python read in each class. For example, if I had a 
> text file with a 
> list of all the rooms, what would the code look like to have 
> Python read in 
> each room, and assign that information into an instance of 
> the Room class.

I'd suggest plain ascii text strings are a great start.  Python has some
great string manipulation functions.  The solution du jour seems to be XML,
but that would be excessive and in my opinion counter to this being a
learning to program project.

For instance, you might have an attribute of a room object that is called
items (Objects might not be such a good class name maybe).  You set it to a
list of item instances.  Each item is going to have a set of attributes.
When its time to save the game state you'll need a method of the item class
that can create a stringified version of the class instance.  Reading it
back then just becomes an exercise in pulling the info out of the formatting
you added when you wrote it:

class Item:
    #  init code and other functions ...
    # __repr__ is a magic class method that is useful for this kind of thing
    def __repr__(self):
        template = "[item]%s:%s:%s" # printf formatting, very useful, sorta
obscure
        return template % (self.name,self.description,self.id) # assume
they're defined
    def load(self,stuff): # 'stuff' being that which was returned from
__repr__
        spam = string.split(stuff[1:],']') # take the [item] off the front
        # a good program would check to make sure spam[0] == "item" :->
        self.name,self.description,self.id = string.split(spam[1],':')

Usage is left as an exercise, but I'd guess the room instance would be
calling these functions of each of the items in its items list.  I hope this
wasn't too obtuse or preachy.  Its hard to answer these kind of general
questions.  Much easier when you've got some code examples you're asking for
help to make work.  My having posted some (untested) code here might
generate some debate amongst others of the best way to do code this kind of
thing.
 
> If anyone can point me in the right direction, I'd be grateful!

HTH.  By the way, 'Mobs' as a class isn't self-explanatory to me.  What is
that?

-Doug-