Advertisement
Guest User

game.py

a guest
Feb 23rd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. import os
  2.  
  3. os.system("cls")
  4.  
  5. class Gamestate:
  6. def __init__(self):
  7. self.rooms = {}
  8. self.import_rooms()
  9. self.commands = {}
  10. self.make_commands()
  11. self.player = Player(name="blank", health=100, damage=15, location=1, work_experiance=0)
  12. self.get_player_room()
  13. self.configure_rooms()
  14.  
  15. def make_commands(self):
  16. self.commands = {"go": Commands(name="go", function=self.going_places, type="movement", description="To go to places"),
  17. "north": Commands(name="go", function=self.going_places, type="movement", description="To move to a north room"),
  18. "stats": Commands(name="stats", function=self.print_stats, type="verbosity", description="the print the stats of something"),
  19. "player": Commands(name="player", function=self.print_player_stats, type="verbosity", description="to print the players stats")}
  20.  
  21. def configure_rooms(self):
  22. # configure north south east and west
  23. for room in self.rooms.keys():
  24. exits = []
  25. nsew = self.rooms[room].nsew
  26. for number in nsew:
  27. exits.append(int(number))
  28. self.rooms[room].nsew = exits
  29.  
  30. for room in self.rooms.keys():
  31. north = self.rooms[room].nsew[0]
  32. south = self.rooms[room].nsew[1]
  33. east = self.rooms[room].nsew[2]
  34. west = self.rooms[room].nsew[3]
  35. self.rooms[room].north = north
  36. self.rooms[room].south = south
  37. self.rooms[room].east = east
  38. self.rooms[room].west = west
  39.  
  40. # working on exits now
  41. for room in self.rooms.keys():
  42. exits = []
  43. for exit in self.rooms[room].exits:
  44. exits.append(int(exit))
  45. self.rooms[room].exits = exits
  46.  
  47. def going_places(self, user_input):
  48. if "north" in user_input:
  49. if self.player.location.north != 0:
  50. for room in self.rooms.keys():
  51. if self.player.location.north == self.rooms[room].rnum:
  52. self.player.location = self.rooms[room]
  53. if "south" in user_input:
  54. if self.player.location.south != 0:
  55. for room in self.rooms.keys():
  56. if self.player.location.south == self.rooms[room].rnum:
  57. self.player.location = self.rooms[room]
  58. if "east" in user_input:
  59. if self.player.location.east != 0:
  60. for room in self.rooms.keys():
  61. if self.player.location.east == self.rooms[room].rnum:
  62. self.player.location = self.rooms[room]
  63. if "west" in user_input:
  64. if self.player.location.west != 0:
  65. for room in self.rooms.keys():
  66. if self.player.location.west == self.rooms[room].rnum:
  67. self.player.location = self.rooms[room]
  68. # lets see if the user entered a room name instead of north south east or west
  69. if self.player.location.north != 0:
  70. for room in self.rooms.keys():
  71. if self.player.location.north == self.rooms[room].rnum:
  72. room_name = self.rooms[room].rname.lower().split()
  73. x = 0
  74. for word in user_input:
  75. for name in room_name:
  76. if word == name:
  77. x += 1
  78. if x == len(room_name):
  79. self.player.location = self.rooms[room]
  80. if self.player.location.south != 0:
  81. for room in self.rooms.keys():
  82. if self.player.location.south == self.rooms[room].rnum:
  83. room_name = self.rooms[room].rname.lower().split()
  84. x = 0
  85. for word in user_input:
  86. for name in room_name:
  87. if word == name:
  88. x += 1
  89. if x == len(room_name):
  90. self.player.location = self.rooms[room]
  91. if self.player.location.east != 0:
  92. for room in self.rooms.keys():
  93. if self.player.location.east == self.rooms[room].rnum:
  94. room_name = self.rooms[room].rname.lower().split()
  95. x = 0
  96. for word in user_input:
  97. for name in room_name:
  98. if word == name:
  99. x += 1
  100. if x == len(room_name):
  101. self.player.location = self.rooms[room]
  102. if self.player.location.west != 0:
  103. for room in self.rooms.keys():
  104. if self.player.location.west == self.rooms[room].rnum:
  105. room_name = self.rooms[room].rname.lower().split()
  106. x = 0
  107. for word in user_input:
  108. for name in room_name:
  109. if word == name:
  110. x += 1
  111. if x == len(room_name):
  112. self.player.location = self.rooms[room]
  113. # lets assume the user wanted to enter an exit location
  114. if self.player.location.exits[0] != 0:
  115. for exit in self.player.location.exits:
  116. for room in self.rooms.keys():
  117. if exit == self.rooms[room].rnum:
  118. exit_name = self.rooms[room].rname.lower().split()
  119. x = 0
  120. for word in user_input:
  121. for name in exit_name:
  122. if word == name:
  123. x += 1
  124. if x == len(exit_name):
  125. self.player.location = self.rooms[room]
  126. general_name = self.rooms[room].general_location.lower().split()
  127. z = 0
  128. for word in user_input:
  129. for name in general_name:
  130. if word == name:
  131. z += 1
  132. if z == len(general_name):
  133. self.player.location = self.rooms[room]
  134.  
  135. def get_player_room(self):
  136. for room in self.rooms.keys():
  137. if self.player.location == self.rooms[room].rnum:
  138. self.player.location = self.rooms[room]
  139.  
  140. def take_input(self):
  141. user_input = input("\nCommand: ").lower().split()
  142. self.going_places(user_input)
  143.  
  144. def do_look(self):
  145. # printing out the current room
  146. print("\n[Location]")
  147. print("Room name: %s" % (self.player.location.rname))
  148. print("Room description: %s" % (self.player.location.rdesc))
  149.  
  150. # print the exits of leaving general location
  151. if self.player.location.exits[0] != 0:
  152. print("\n[Exits]")
  153. for room in self.rooms.keys():
  154. for exit in self.player.location.exits:
  155. if exit == self.rooms[room].rnum:
  156. print("%s: %s" % (self.rooms[room].general_location, self.rooms[room].rname))
  157.  
  158. # print the room exits
  159. print("\n[Room Exits]")
  160. if self.player.location.north != 0:
  161. for room in self.rooms.keys():
  162. if self.player.location.north == self.rooms[room].rnum:
  163. print("North: %s" % (self.rooms[room].rname))
  164. if self.player.location.south != 0:
  165. for room in self.rooms.keys():
  166. if self.player.location.south == self.rooms[room].rnum:
  167. print("South: %s" % (self.rooms[room].rname))
  168. if self.player.location.east != 0:
  169. for room in self.rooms.keys():
  170. if self.player.location.east == self.rooms[room].rnum:
  171. print("East: %s" % (self.rooms[room].rname))
  172. if self.player.location.west != 0:
  173. for room in self.rooms.keys():
  174. if self.player.location.west == self.rooms[room].rnum:
  175. print("West: %s" % (self.rooms[room].rname))
  176.  
  177.  
  178. def import_rooms(self):
  179. f = open("rooms.txt", "r")
  180. lines = f.readlines()
  181.  
  182. loop = 0
  183.  
  184. while loop <= len(lines) - 1:
  185. self.rooms[lines[loop+1]] = Room(rnum=int(lines[loop]), rname=lines[loop+1].strip("\n"), rdesc=lines[loop+2].strip("\n"), general_location=lines[loop+3].strip("\n"), nsew=lines[loop+4].split(), exits=lines[loop+5].split())
  186. loop += 6
  187.  
  188. class Commands:
  189. def __init__(self, name, function, type, description):
  190. self.name = name
  191. self.function = function
  192. self.type = type
  193. self.description = description
  194.  
  195. class Player:
  196. def __init__(self, name, health, damage, location, work_experiance):
  197. self.name = name
  198. self.health = health
  199. self.damage = damage
  200. self.location = location
  201. self.work_experiance = work_experiance
  202.  
  203. class Room:
  204. def __init__(self, rnum=None, rname=None, rdesc=None, general_location=None, nsew=None, north=None, south=None, east=None, west=None, exits=None):
  205. self.rnum = rnum
  206. self.rname = rname
  207. self.rdesc = rdesc
  208. self.general_location = general_location
  209. self.nsew = nsew
  210. self.north = north
  211. self.south = south
  212. self.east = east
  213. self.west = west
  214. self.exits = exits
  215.  
  216. gamestate = Gamestate()
  217.  
  218. def main():
  219. gamestate.player.name = input("\nWhats your name?\nName: ")
  220. print("\nThank you for playing my game %s" % (gamestate.player.name))
  221. while True:
  222. gamestate.do_look()
  223. gamestate.take_input()
  224.  
  225. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement