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

Re: [pygame] Text-Based RPG




You guys are really awesomely smart BUT I'm dumb, so:
class Room:
    def __init__(self):
exits = {} # Create a dictionary of exits- depending on ID, there could be different exits self.Id = None # ID starts at 0, but different rooms have different ID
        self.name = None # ^^^
        self.exits = []
        self.text = None
        self.comm = None
        self.current_room = None
    def DisplayRoom(current_room):
        """Displays text for a room"""
        print current_room.text.read
        print current_room.exits
        current_room.text.close()
    def CommandLine():
        comm = raw_input('>>> ')
        return comm
        ProcessCommand()
    def ProcessCommand(current_room):
        """CommandLine processor"""
        words = comm.split()
        if words[0] == 'north':
            MoveNorth(current_room)
        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)
        else:
            raise 'Error. Invalid command.'
    def MoveNorth(room_to_go_n):
        if room_to_go_n.exits[0] == 'north':
            room_north()
        else:
            print "You can't go that way."

tavern = Room()
tavern.Id = 1
tavern.name = 'Anthropophagi Tavern'
tavern.exits = ['north', 'south', 'none', 'none', 'none', 'down']
tavern.text = open('swamp_tavern.txt', 'r')
tavern.current_room = tavern()

tavernbackroom = Room()
tavernbackroom.Id = 2
tavernbackroom.name = 'Tavern Backroom'
tavernbackroom.exits = ['none', 'south', 'none', 'none', 'none', 'none']
tavernbackroom.text = open('tavern_backroom.txt', 'r')
tavernbackroom.current_room = tavernbackroom

tavern.room_north = tavernbackroom()

tavern.DisplayRoom(tavern)
tavern.CommandLine()

When I try to run this test program, it says that "AttributeError: Room instance has no __call__ method"
Is __call__ like __init__?

You certainly could make a Tavern Room subclass of the Room class, but it probably makes more sense to make a generic Location class, and then use data (loaded from text files like I think you're already doing, or loading from a database, or... any number of other data-driven solutions) to give it the specific characteristics that you want (Dark Room, Tavern, Cave, In
Front of House, Flood Control Gate #8).