[Tutor] my text adventure

david din22 at earthlink.net
Mon Dec 5 04:56:53 CET 2005


i have finally got save and restoring to work. here is my code for posterity. :)
i was (i think) putting the player in a room that wasnt the room from the pickle.
but somehow the same room. 
anyway comments welcome.

import sys
import string
import pickle
import os.path

world = {}
rooms = []
class Room:
    def __init__(self,coords):
        self.contents = []
        self.description = ''
        self.coords = coords
        world[tuple(coords)] = self
        rooms.append(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.coords
        print self.location.description

    def move(self,direction):
        type(direction)
        if self.location.exits.has_key(direction):
            self.location = self.location.exits[direction]
        else:
            print 'alas, you cannot go that way'
    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] = world[target]
           
            world[target].exits[opdir(direction)] = self.location
        
        else:
            world[target]=Room(target)
            self.location.exits[direction] = world[target]
            world[target].exits[opdir(direction)] = self.location
         
    def describeroom(self):
        self.location.description = raw_input('>>')
    def save(self):
        f = open('savefile', 'w')
        pickle.dump(world,f)
        f.close()
    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(verb)
        elif verb == 'quit':
            sys.exit()
        elif verb == 'i':
            for a in self.inventory:
                print a.name
        elif verb == 'dig':
            self.dig(target)
        elif verb == 'dr':
            self.describeroom()
        elif verb == 'save':
            self.save()
        elif verb == 'world':
            print world
        elif verb == 'rooms':
            print rooms
            for i in rooms:
                print i.description
                print i.exits
        else:
            print 'what?'

class Thing:
    def __init__(self,name):
        self.name = name


def opdir(direction):
    if direction == 'n':
        return 's'
    if direction == 's':
        return 'n'
    if direction == 'e':
        return 'w'
    if direction == 'w':
        return 'e'
    
        
p = Player('david')



sword = Thing('sword')
hat = Thing('hat')
p.inventory.append(sword)
p.inventory.append(hat)

if os.path.isfile('savefile'):
    f = open('savefile','r')
    world = pickle.load(f)
    f.close()
    
    p.location = world[0,0]
    while 1:
        p.do()
else:
    room1 = Room([0,0])
    p.location = room1
    while 1:
        p.do()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051204/d07f21c6/attachment.html


More information about the Tutor mailing list