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

Re: [pygame] Text-Based RPG



In a message of Tue, 19 Jun 2007 15:38:22 EDT, Jonah Fishel writes:
>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

<snip>

>
>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__?

It can be.  In Python you are allowed to write your own __call__
method.  But it is very unlikely that you will ever need to in the
course of this game.   Most things that you want to call will
already be callable.  functions are callable.  methods of
classes are callable.  You don't need to do anything to make them
that way -- they come that way right out of the box.

Classes, however, aren't callable.  And somewhere up there you have
tried to call one.  The thing to look for is a set of () that
do not belong.  (It could be a (with, some, arguments, inside)
but it's usually a spurious set of ().  

Aha, there it is ...

tavern.room_north = tavernbackroom()

tavernbackroom is an instance of the Room class.  You cannot call it.
take away the parens and things will work fine.

Laura