[Tutor] my newbie program
Gregor Lingl
glingl@aon.at
Mon Nov 18 13:23:02 2002
alan.gauld@bt.com schrieb:
>>class room:
>> def __init__(self):
>> self.desc=""
>> self.exits=[]
>>
>>
>>room1=room()
>>room2=room()
>>room1.exits=[room2,0,0,0]
>>room1.desc="a big room"
>>room2.desc="an even bigger room"
>>
>>
>
>You could simplify that by using default args to the init method:
>
>class Room: #capitalised class names is the convention.
> def __init__(self, desc="", exits=[])
>
here a colon
is missing!
> self.desc = desc
> self.exits = exits
>
>room2 = Room("an even bigger room")
>room1 = Room("a big room", [room2,0,0,0])
>
>You need to watch the order to avoid using names of objects
>not yet created in the exi list but otherwise it saves effort....
>
>
>
>>class Player:
>>
>>
> self.commandlist = ['look','n']
>
I think this won't work. there is no name self here. So better
either delete the self. to create a class-variable and replace
self.commandlist by Player.commandlist
or put this statement into __init__
>
>
>> def __init__(self):
>> self.location=room1
>> def act(self):
>> cmd = raw_input('>')
>>
>>
> if cmd in self.commandlist: # bit of a sanity check
>
>
>> if cmd == 'look':
>> print self.location.desc
>> if cmd == 'n':
>> if self.location.exits[0] != 0:
>> self.location=self.location.exits[0]
>> else:
>> print "you can't go that way"
>>
>>
> else: print "invalid command: ", cmd
>
>
>
>>me = Player()
>>
>>
> while 1:
> me.act()
> if not map(lambda x: x != 0, me.location.exits):
> break
>
>
This is a little bit disturbing. Wasn't the original concept, that exits
should be
a four element list, containing exits (to rooms) in the four directions
if they exist and 0
otherwise?
So one should better add
room2.exits=[0,0,room1,0]
along with some amendments Magnus suggested -
which would make the game a little more interesting
Gregor
>OK, the map() is maybe overkill... :-)
>Otherwise it looks like it should work so far... What next?
>
>Alan g.
>
>_______________________________________________
>Tutor maillist - Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>