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

Re: [pygame] Text-Based RPG



"Jonah Fishel" <globalparadox@xxxxxxxxxxx> is working on a text adventure:

Other folks have talked about returning values, which is good, but this
line makes me uneasy:

> def command(command, current_room):

I'd stay away from using the same name for more than one thing. In this
case, you're naming the argument the same thing as the function - it's
probably OK in this case where the scoping does the right thing, but it's
way too easy to get name collisions in Python - accidentally blocking a
global by assigning a local value to it.

One problem you'd get into with this particular example would be if you
wanted to implement a "tell" command that operated something like this:

>>> tell sailor, go north

You might implement that as a recursive call that pulls out the name of
the individual the player's talking to, and takes the rest of the line
(leaving aside the issues of making sure the punctuation's cleaned up) and
does a recursive call - you'd want to call the function "command" from
within itself, but the argument "command" keeps the function "command"
from being seen.

I'd rewrite it a little like this:

def parse_command(command_string, cur_room):

and you should be in good shape.


-Dave LeCompte