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

Re: [pygame] Text-Based RPG





On 6/18/07, Jonah Fishel <globalparadox@xxxxxxxxxxx> wrote:
I've been away from Python for several months. I just came back to
it, to start working on a text-based RPG. To get started, I decided
to move by rooms. I have separate text files for each major message,
to conserve file space. What troubles me is that when I tested it,
whenever I enter anything into the command line, it says 'Invalid
command' which I included as an error message. Here is where I think
the trouble is:

def command_line():
     command = raw_input('>>> ')
def command(command, current_room):
     if current_room == anthro_tavern:
         if command == 'north':
             print '\n\nThat room does not exist yet. Please try
again.\n\n'
         elif command == 'south':
             print '\n\nThat room does not exist yet. Please try
again.\n\n'
         elif command == 'down':
             print 'That room does not exist yet. Please try again./n/n'
         elif command == 'talk to garrick':
             speech = open('garricks_speech.txt', 'r')
             print speech.read()
             speech.close()
         elif command == 'talk to janet':
             speech = open('janets_speech.txt', 'r')
             print speech.read()
             speech.close()
         elif command == 'talk to stranger':
             print 'This feature is coming soon.'
         else:
             print 'Invalid command.'
         anthro_tavern()
     else:
         print 'Error 1: Non existent room being requested/nfrom. If
this error appears, something/nis very wrong.'
def anthro_tavern():
     text_file = open("swamp_tavern.txt", "r")
     print text_file.read()
     text_file.close()
     command(command_line(), anthro_tavern)
     return current_room

anthro_tavern()

I originally had different commands for each action, with variations
on capitalization so you didn't have to enter the same command every
time, but then all it did was the message for the first 'if' line.
Does anyone see a problem with this?

Besides the suggestion to use .strip(), you could also use .split(). For example:

a = 'say hello'
print a.split()

prints:
['say', 'hello']

Then you have a list. So you can use the first argument as the command and have a function for each command that parses the other arguments and produces the desired result.

So if you go:
a = 'say hello'
b = a.split()

then you can go:
if b[0] = 'say':
    say(b)

I hope you understand what I'm saying :)

Also you should look up how 'class'es work. Using them would make this easier.