[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Text-Based RPG
"Jonah Fishel" <globalparadox@xxxxxxxxxxx> wrote:
> tavern = Room()
[...]
> tavernbackroom = Room()
[...]
> tavern.room_north = tavernbackroom()
[...]
> When I try to run this test program, it says that "AttributeError:
> Room instance has no __call__ method"
The problem you're hitting here is that you're creating two instances of
the room class, tavern and tavernbackroom, and then you're attempting to
call tavernbackroom, and assign that to the room_north attribute of
tavern.
You probably would rather do
tavern.room_north = tavernbackroom
with no parens.
Another way you could implement this would be to make the exits a
dictionary instead of a list, and then hook up rooms like so:
tavern.exits['north']=tavernbackroom
We're sort of getting afield from PyGame discussion and into Python style,
but a few more suggestions:
> tavern.DisplayRoom(tavern)
I wouldn't do this - you're creating a class method and passing an
instance into it. Instead, I'd create an instance method, and call it
directly, like so:
def DisplayRoom(self):
"""Displays text for a room"""
print self.text
print self.exits.keys()
and then, the display would look more like:
tavern.DisplayRoom()
Also, the call to close() inside the display seems like it's in the wrong
place - it's best to have file reading code separated from display
(loading probably happens once, and display could happen many times), so
maybe you'd want to do something like:
def LoadDescription(self, filename):
file=open(filename,r)
self.text=file.read()
file.close()
and then your description text line would look more like this:
tavern.LoadDescription('swamp_tavern.txt')
-Dave LeCompte