Advertisement
TorroesPrime

room.py

Aug 8th, 2020 (edited)
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. from dungeon import top_level_delimiter, second_level_delim
  2. class Room:
  3. name = ""
  4. desc = ""
  5. been_here =""
  6. exits = []
  7. def __init__(self):
  8. self.been_here = False
  9. def manuel_room(self,name,desc,exits):
  10. """manual instanstiator for room objects. Requires a string for the name of the room, a string that is a description of the room, and a list of one or more exits"""
  11. self.name = name
  12. self.desc = desc
  13. self.add_exit(exits)
  14. return self
  15.  
  16. def add_exts(self, exits):
  17. """takes a list of exits and adds each one to the room's exits list"""
  18. for exit in exits:
  19. self.add_exit(exit)
  20. def scanner_room(self,f):
  21. """Read from the .zork filename passed, and instantiate a Room object based on it."""
  22. self.__init__()
  23. self.name = f.readline()
  24. if(self.name == top_level_delimiter):
  25. raise Exception("Invalid dungeon format")
  26. lines_Of_desc = f.readline()
  27. while lines_Of_desc != second_level_delim & lines_Of_desc != top_level_delimiter:
  28. self.desc = self.desc+lines_Of_desc+"\n"
  29. lines_of_desc = f.readline()
  30. if lines_of_desc != second_level_delim:
  31. raise Exception(f"Invalid dungeon format: No {second_level_delim} after room")
  32. return self
  33.  
  34. #def RoomMaker(self, f):
  35. # return self.RoomScan(f)
  36. def store_state(self,save_file):
  37. """Stores the current state of this room to the filename passed to it."""
  38. if self.been_here:
  39. save_file.write(self.name+":")
  40. save_file.write("been_here:true")
  41. save_file.write(second_level_delim)
  42.  
  43. def restore_state(self, f):
  44. line = f.readline().split(":")
  45. if line[0]!="been_here":
  46. raise Exception("No been_here value")
  47. if line[1]=="true":
  48. self.been_here = True
  49. else:
  50. self.been_here = False
  51.  
  52. def describe(self):
  53. description = ""
  54. if(self.been_here):
  55. description = self.name
  56. else:
  57. description = self.name+"\n"+self.desc+"\n"
  58. for exit in self.exits:
  59. description = description + "\n"+exit.describe()
  60. self.been_here = True
  61. return description
  62.  
  63. def leave_by(self, direction):
  64. for exit in self.exits:
  65. if exit.getdirection() == direction:
  66. return exit.getDest()
  67. return None
  68.  
  69. def add_exit(self, exit):
  70. self.exits.append(exit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement