Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. class Behavior():
  2. def receiveCommand(self,command):
  3. playerCoordinates = self.game.getPlayerCoordinates()
  4. x = playerCoordinates[0]
  5. y = playerCoordinates[1]
  6. availableBehaviors = self.behaviors[x][y]
  7. if command in list(availableBehaviors.keys()):
  8. actionFunc = availableBehaviors[command]
  9. actionFunc()
  10. else:
  11. if command in ['north','east','west','south']:
  12. # the direction wasn't an applied behavior
  13. self.game.invalidDirection()
  14. else:
  15. # the command didn't match any behavior
  16. print('Unknown command, type "help" for list of commands')
  17.  
  18. def north(self):
  19. return {'north':self.game.moveNorth}
  20. def east(self):
  21. return {'east':self.game.moveEast}
  22. def west(self):
  23. return {'west':self.game.moveWest}
  24. def south(self):
  25. return {'south':self.game.moveSouth}
  26. def help(self):
  27. return {"help":self.game.help}
  28. def keyNotFound(self):
  29. return {"get key":self.game.keyNotFound}
  30. def keyFound(self):
  31. return {"get key":self.game.keyFound}
  32.  
  33. def createStandardBehavior(self):
  34. # create a standard set of
  35. standardBehavior = {}
  36. standardBehavior.update(self.help())
  37. standardBehavior.update(self.keyNotFound())
  38. return standardBehavior
  39.  
  40. def applyBehaviors(x,y,behaviorsToApply):
  41. for behavior in behaviorsToApply:
  42. self.behaviors[x][y].apply(behavior)
  43.  
  44. def createBehaviors(self):
  45. # this applies full movement to the first 5 square of the top row
  46. applyBehaviors(0,0,[north,east,west,south])
  47. applyBehaviors(0,1,[north,east,west,south])
  48. applyBehaviors(0,2,[north,east,west,south])
  49. applyBehaviors(0,3,[north,east,west,south])
  50. applyBehaviors(0,4,[north,east,west,south])
  51.  
  52. # this applies full movement to the first 5 square of the 15th row
  53. applyBehaviors(14,0,[north,east,west,south])
  54. applyBehaviors(14,1,[north,east,west,south])
  55. applyBehaviors(14,2,[north,east,west,south])
  56. applyBehaviors(14,3,[north,east,west,south])
  57. applyBehaviors(14,4,[north,east,west,south])
  58.  
  59. def __init__(self,game):
  60. self.game = game
  61. self.createBehaviors()
  62. # create a dictionary of behaviors for each game square (16 by 16)
  63. gridSize = self.game.getGridSize()
  64. row = [self.createStandardBehavior() for i in range(gridSize['x'])]
  65. self.behaviors = [row for i in range(gridSize['y'])]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement