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} class Object:
def __init__(self,name): self.name = name def
getname(self):
return self.name class Player:
def __init__(self,name): self.name = name self.location = None self.inventory = [] self.wielded = None 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) if self.location.exits.has_key(direction): print 'there is already an exit there' elif direction == 'n': ncoords[1] == lcoords[1] + 1 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@python.org http://mail.python.org/mailman/listinfo/tutor