[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Text-Based RPG



On Tue, June 19, 2007 7:38 pm, Jonah Fishel wrote:
> def ProcessCommand(current_room): """CommandLine processor"""
> words = comm.split() if words[0] == 'north': MoveNorth(current_room)

About this section: Try typing a blank line as your command and you'll get
an error, because words will be a blank list( [] ) and words[0] will be
outside the allowed range. I suggest sanity-checking by counting the
number of words:
number_of_words = len(words)
if number_of_words > 1:
    ## Do stuff that requires more than one word

> elif words[0] == 'south': MoveSouth(current_room)
> elif words[0] == 'west': MoveWest(current_room)
> elif words[0] == 'east': MoveEast(current_room)
> elif words[0] == 'up': MoveUp(current_room)
> elif words[0] == 'down': MoveDown(current_room)

These probably don't need unique functions. Why not one function that
takes a direction parameter? Or have a class that keeps track of the
current room, so there's no need to pass the current room each time?

> if room_to_go_n.exits[0] == 'north':

This line won't work if the exits are stored as a dictionary, because
you're trying to access it like a list. If you want the keys in a
dictionary:
>>> dict = {"bacon":True,"eggs":True,"spam":True}
>>> dict.keys()
["bacon","eggs","spam"]

But dictionaries don't store the keys in any predictable order, so don't
count on the "first" key being the one you want. How about:
if room.exits.get("north"):
    ## Then the dictionary has an entry like:
    ## {"north":True}
    ## If the value for "north" is False, or if there _is_ no north, the if
    ## statement will be false. You could also ask, "if room.exits['north']"
    ## but that'll crash if the key doesn't exist.

Or if every room can only have exits in standard directions like "north"
instead of unique exits like "stairs", you could use a list where entry 0
is always north, 1 is east, and so on. That might make more sense than a
dictionary. I would then have constants like:
NORTH = 0

so that you can then say:
if room.exits[NORTH]: ## ...

Yet another way to navigate would be to have each room have XYZ
coordinates, so that if the current room is (0,0,0) and the player wants
to go north, you might check for an existing room with coords (0,-1,0).

> tavernbackroom.exits = ['none', 'south', 'none', 'none', 'none', 'none']
> tavernbackroom.text = open('tavern_backroom.txt', 'r')
> tavernbackroom.current_room = tavernbackroom

I suspect you're misusing the open statement. That command creates an open
"file object," but doesn't actually give you its contents. If you just
want to pull the text out of the file, try:
f = open("whatever.txt","r")
text = f.read() ## Get the file's text
f.close()

In Windows at least, I find that if my program crashes while I've got a
file open, the OS complains if I try to delete the file.