[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
RE: [pygame] Text-Based RPG
> -----Original Message-----
> From: owner-pygame-users@xxxxxxxx [mailto:owner-pygame-users@xxxxxxxx] On
> Behalf Of Jonah Fishel
> Sent: Monday, June 18, 2007 10:30 AM
> To: pygame-users@xxxxxxxx
> Subject: [pygame] Text-Based RPG
>
> 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?
You'll probably get a bunch of suggestions for how to improve the overall
structure of things here, but the reason why your sample isn't working like
you expect it to is because your command_line() function isn't returning a
value. Since it doesn't have an explicit return, the value your command()
function is seeing is None, which is what would be printed if you put a
'print command' line at the top of your command() function.
Try this instead:
def command_line():
return raw_input('>>> ')
As a suggestion for dealing with capitilization variations, take a look at
the lower string method.
---------
John Krukoff
helot@xxxxxxxxxxx