[Tutor] my newbie program
david
din22@cox.net
Sat Dec 14 10:10:02 2002
hello everyone and thanks for all your help and patience so far.
i finally understood about getting my rooms to share a map
object. now its a world object because there is a python function
called map. y'all probably knew that. anyway i am working on
my dig method and i am thinking my exits are all messed up.
i will be wanting to add doors both locked and unlocked next.
so if anyone can help with a better room exit. or any comments
at all.
import string
import sys
class World:
def __init__(self):
self.grid={}
def addRoom(self,room,x,y):
if self.grid.has_key((x,y)):
raise KeyError,"location occupied"
self.grid[(x,y)]=room
class Room:
world = World()
def __init__(self,x,y):
self.desc="a nondescript room"
self.exits=[]
self.world.addRoom(self,x,y)
self.coords=(x,y)
def dig(self,dir):
pass
class Parser:
pass
class Actor:
def __init__(self,location):
self.location=location
def DoCommand(self,cmd):
if cmd == 'look':
print self.location.desc
elif cmd == 'exit':
sys.exit()
else:
print 'unknown command'
class Player(Actor):
def act(self):
cmd = string.lower(raw_input('>'))
self.DoCommand(cmd)
startroom=Room(0,0)
newroom=Room(0,1)
me=Player(startroom)
while 1:
me.act()