[Tutor] my newbie program
alan.gauld@bt.com
alan.gauld@bt.com
Mon Nov 18 12:04:03 2002
> 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=[])
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']
> 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
OK, the map() is maybe overkill... :-)
Otherwise it looks like it should work so far... What next?
Alan g.