# #Game - contains the primary game code import os import error import esse from common import Directions class game: def __init__(self): self.board = gameBoard() self.player = self.board.getPlayer() def move(self, direction): self.player.face(direction) self.board.moveObject(self.player, direction) def interact(self): add = Directions.TUPS[self.player.getFacing()] targetX, targetY = self.player.getX() + add[0], self.player.getY() + add[1] self.board.getObject(targetX, targetY).interact(self) def load(self, map, entry='a'): self.player = None self.board.readPassMap(map, entry) self.player = self.board.getPlayer() def __str__(self): return self.board.__str__() class gameBoard: def __init__(self): self.readPassMap('main', 'a') def readPassMap(self, fileName, entry): self.theNothing = esse.nothing() fullPath = os.path.join('maps', fileName) self.player = None if os.path.exists(fullPath): f = open(fullPath) #Get the name self.name = f.readline() #Create new objects l = f.readline() #Ignore the #objects directive l = f.readline() newObjects = [] while '#map' not in l: command = l.strip().split() if command[0] == 'entry' and command[1] != entry: print "Ignoring command: ", command else: newObjects.append(esse.esse_lookup[command[0]](command[1:])) if command[0] == 'entry': self.player = newObjects[-1] print "Added new: ", newObjects[-1] l = f.readline() #Get the pass data l = f.readline() self.passData, self.objectData = [], [] n = 0 while l != '': self.passData.append([]) self.objectData.append([]) for char in l: self.passData[n].append(char) self.objectData[n].append(self.theNothing) n += 1 l = f.readline() #Populate object data for object in newObjects: self.objectData[object.getY()][object.getX()] = object else: error.error('file') def getPlayer(self): return self.player def getPass(self, x, y): if self.passData[y][x] == ' ': if self.objectData[y][x] == self.theNothing: return True return False def getObject(self, x, y): return self.objectData[y][x] def putObject(self, x, y, object): self.objectData[y][x] = object def removeObject(self, x, y): self.objectData[y][x] = self.theNothing def moveObject(self, object, dir): oldx, oldy = object.getX(), object.getY() add = Directions.TUPS[dir] x, y = oldx + add[0], oldy + add[1] print "Moving ", object, " to location ", x, y if self.getPass(x, y): print "That place is free!" object.setX(x) object.setY(y) self.putObject(x, y, object) self.removeObject(oldx, oldy) print "Move successful!" else: print "That place is blocked by ", self.getObject(x, y) print "It also has a floor of ", self.passData[y][x] error.error('block') def __str__(self): returnValue = '' y = 0 for a in self.passData: x = 0 for b in a: if self.passData[y][x] != ' ': returnValue += "X" else: returnValue += self.getObject(x, y).__str__() x += 1 returnValue += '\n' y += 1 return returnValue