[Tutor] my text adventure
david
din22 at earthlink.net
Sat Dec 3 04:42:00 CET 2005
well i have got some things to work, thanks for the help.
at this point i am just making things worse so thats it for tonight.
any comments, suggestions, bug fixes, improvements, or ideas
on how my program can add to the quality of all our lives are,
as always, most greatly appreciated.
import sys
import string
world = {}
class Room:
def __init__(self,coords):
self.contents = []
self.name = ''
self.coords = coords
world[tuple(coords)] = self
self.exits = {}
def nextdoor(self,direction):
if direction == 'n':
nextdoor = (self.coords[0], self.coords[1] + 1)
return list(nextdoor)
elif direction == 's':
nextdoor = list((self.coords[0], self.coords[1] - 1))
return nextdoor
elif direction == 'e':
nextdoor = list((self.coords[0] +1, self.coords[1]))
return nextdoor
elif direction == 'w':
nextdoor = (self.coords[0] -1, self.coords[1])
return list(nextdoor)
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]
def wield(self,what):
self.wielded = what
def wear(self,what):
pass
def take(self,what):
pass
def drop(self,what):
pass
def dig(self,direction):
target = tuple(self.location.nextdoor(direction))
print target
if self.location.exits.has_key(target):
print 'there is already an exit to that room'
elif world.has_key(target):
print 'already a room there, attempt to make exits'
self.location.exits[direction] = Room(target)
else:
world[target]=Room(target)
self.location.exits[direction] = Room(target)
def do(self):
cmd = string.split(raw_input('>'))
verb = cmd[0]
if len(cmd) > 1:
target = cmd[1]
if verb == 'l':
self.look()
elif verb in ['n','s','e','w']:
self.move(cmd)
elif verb == 'quit':
sys.exit()
elif verb == 'i':
for a in self.inventory:
print a.name
elif verb == 'dig':
self.dig(target)
else:
print 'what?'
class Thing:
def __init__(self,name):
self.name = name
p = Player('david')
room1 = Room([0,0])
room1.name = 'startroom'
p.location = room1
sword = Thing('sword')
hat = Thing('hat')
p.inventory.append(sword)
p.inventory.append(hat)
while 1:
p.do()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051202/3624af3b/attachment.html
More information about the Tutor
mailing list