game.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #
  2. #Game - contains the primary game code
  3. import os
  4. import error
  5. import esse
  6. from common import Directions
  7. class game:
  8. def __init__(self):
  9. self.board = gameBoard()
  10. self.player = self.board.getPlayer()
  11. def move(self, direction):
  12. self.player.face(direction)
  13. self.board.moveObject(self.player, direction)
  14. def interact(self):
  15. add = Directions.TUPS[self.player.getFacing()]
  16. targetX, targetY = self.player.getX() + add[0], self.player.getY() + add[1]
  17. self.board.getObject(targetX, targetY).interact(self)
  18. def load(self, map, entry='a'):
  19. self.player = None
  20. self.board.readPassMap(map, entry)
  21. self.player = self.board.getPlayer()
  22. def __str__(self):
  23. return self.board.__str__()
  24. class gameBoard:
  25. def __init__(self):
  26. self.readPassMap('main', 'a')
  27. def readPassMap(self, fileName, entry):
  28. self.theNothing = esse.nothing()
  29. fullPath = os.path.join('maps', fileName)
  30. self.player = None
  31. if os.path.exists(fullPath):
  32. f = open(fullPath)
  33. #Get the name
  34. self.name = f.readline()
  35. #Create new objects
  36. l = f.readline() #Ignore the #objects directive
  37. l = f.readline()
  38. newObjects = []
  39. while '#map' not in l:
  40. command = l.strip().split()
  41. if command[0] == 'entry' and command[1] != entry:
  42. print "Ignoring command: ", command
  43. else:
  44. newObjects.append(esse.esse_lookup[command[0]](command[1:]))
  45. if command[0] == 'entry':
  46. self.player = newObjects[-1]
  47. print "Added new: ", newObjects[-1]
  48. l = f.readline()
  49. #Get the pass data
  50. l = f.readline()
  51. self.passData, self.objectData = [], []
  52. n = 0
  53. while l != '':
  54. self.passData.append([])
  55. self.objectData.append([])
  56. for char in l:
  57. self.passData[n].append(char)
  58. self.objectData[n].append(self.theNothing)
  59. n += 1
  60. l = f.readline()
  61. #Populate object data
  62. for object in newObjects:
  63. self.objectData[object.getY()][object.getX()] = object
  64. else:
  65. error.error('file')
  66. def getPlayer(self):
  67. return self.player
  68. def getPass(self, x, y):
  69. if self.passData[y][x] == ' ':
  70. if self.objectData[y][x] == self.theNothing:
  71. return True
  72. return False
  73. def getObject(self, x, y):
  74. return self.objectData[y][x]
  75. def putObject(self, x, y, object):
  76. self.objectData[y][x] = object
  77. def removeObject(self, x, y):
  78. self.objectData[y][x] = self.theNothing
  79. def moveObject(self, object, dir):
  80. oldx, oldy = object.getX(), object.getY()
  81. add = Directions.TUPS[dir]
  82. x, y = oldx + add[0], oldy + add[1]
  83. print "Moving ", object, " to location ", x, y
  84. if self.getPass(x, y):
  85. print "That place is free!"
  86. object.setX(x)
  87. object.setY(y)
  88. self.putObject(x, y, object)
  89. self.removeObject(oldx, oldy)
  90. print "Move successful!"
  91. else:
  92. print "That place is blocked by ", self.getObject(x, y)
  93. print "It also has a floor of ", self.passData[y][x]
  94. error.error('block')
  95. def __str__(self):
  96. returnValue = ''
  97. y = 0
  98. for a in self.passData:
  99. x = 0
  100. for b in a:
  101. if self.passData[y][x] != ' ':
  102. returnValue += "X"
  103. else:
  104. returnValue += self.getObject(x, y).__str__()
  105. x += 1
  106. returnValue += '\n'
  107. y += 1
  108. return returnValue