[Tutor] my text adventure

Kent Johnson kent37 at tds.net
Wed Nov 30 00:20:13 CET 2005


david wrote:
> hello again python tutors! my latest attempt to learn python.
> any comments or suggestions most greatly appreciated.
>  
> import sys
> import string
> ncoords = [0,0]
> class Room:
>     def __init__(self,name):
>         self.exits = {}
>         self.contents = []
>         self.name = name
>         self.coords = ()
> room1 = Room('room1')
> room2 = Room('room2')
> room3 = Room('room3')
> room1.coords = (0,0)
> room2.coords = (0,1)
> room3.coords = (0,-1)
> room1.exits = {'n':room2,'s':room3}
> room2.exits = {'s':room1}
> room3.exits = {'n':room1}
> world = { (0,0):room1, (0,1):room2, (0,-1):room3}

You might want to make coords an init parameter for Room, rather than assigning it separately. You could also have Rooms register themselves with world as part of their initialization, that would be better than having to repeat yourself in the definition of world.
>  
> class Object:
>     def __init__(self,name):
>         self.name = name
>  
>     def getname(self):
>         return self.name

Object is not such a great name, it is too easily confused with object, the base class of new-style classes. Maybe Item?

Right now this class isn't pulling its weight, it could be replaced by a simple string. Presumably you will add more functionality to it; if so it's worth keeping it around, otherwise get rid of it.

Common Python practice is *not* to define setters and getters, just access the attribute directly - so get maybe you don't need getname().

>  
> class Player:
>     def __init__(self,name):
>         self.name = name
>         self.location = None
>         self.inventory = []
>         self.wielded = None

Maybe location should be an init param?

>     def look(self):
>         print self.location.name
>  
>     def move(self,direction):
>         if self.location.exits.has_key(direction):
>             self.location = self.location.exits[direction]
>             print self.location.name
>         else: print 'alas, you cannot go that way'
>     def dig(self,direction):
>         lcoords = list(self.location.coords)

Why do you convert to a list? You can access tuple elements by index.

>         if self.location.exits.has_key(direction):
>             print 'there is already an exit there'
>        
>        
>         elif direction == 'n':
>             ncoords[1] == lcoords[1] + 1

You probably mean
             ncoords[1] = lcoords[1] + 1
but I think I would make ncoords a local variable and just say
             ncoords = (0, lcoords[1] + 1)

With ncoords as a global you have a bug, you only set one element here, the other one will be left over from the last time through.

Kent

>         elif direction == 's':
>             ncoords[1] == lcoords[1] - 1
>         elif direction == 'e':
>             ncoords[0] == lcoords[0] + 1
>         elif direction == 'w':
>             ncoords[0] == lcoords[0] - 1
>         elif world.has_key(tuple(ncoords)):
>             print "there is a room there"
>             #so add exits
>         else:
>             #make a room and add exits to and from
>             pass
>     def wield(self,what):
>         self.wielded = what
>     def wear(self,what):
>         pass
>     def take(self,what):
>         pass
>     def drop(self,what):
>         pass
>     def do(self):
>         cmd = raw_input('>')
>         if cmd == 'l':
>             self.look()
>         elif cmd in ['n','s','e','w']:          
>             self.move(cmd)
>         elif cmd == 'quit':
>             sys.exit()
>         elif cmd == 'i':
>             for a in self.inventory:
>                 print a.getname()
>         else:
>             print 'what?'
>  
> p = Player('david')
> sword = Object('sword')
> hat = Object('hat')
>  
> p.location = world[(0,0)]
> p.inventory.append(sword)
> p.inventory.append(hat)
> while 1:
>     p.do()
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list