Advertisement
SteveJeff

Prog 103 - RPG02

Nov 12th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. # prog 103 course
  2. # save progress in Role Play Game - 12 Nov 19
  3.  
  4. import json
  5.  
  6. try:
  7. print("Retrieving your details")
  8. with open("gamedata.json", "r") as f:
  9. gamedata = json.load(f)
  10. name = gamedata["playername"]
  11. health = gamedata["playerhealth"]
  12. currentRoom = gamedata["payercurrentRoom"]
  13. inventory = gamedata["inventory"]
  14.  
  15. except:
  16. print("No player found, creating a new gamedata file")
  17. f = open("gamedata.json", "w")
  18. f.close()
  19. name = None
  20. health = 5
  21. currentRoom = "Hall"
  22. inventory = []
  23.  
  24.  
  25. def showInstructions():
  26. # Print a main menu and the commands
  27. print('''
  28. RPG Game
  29. ========
  30.  
  31. Get to the Garden with a key and a potion.
  32. Avoid the monsters!
  33.  
  34. You are getting tired; each time you move you lose 1 health point.
  35.  
  36. Commands:
  37. go [direction]
  38. get [item]
  39. ''')
  40.  
  41. def showStatus():
  42. # Print the player's current status
  43. print('---------------------------')
  44. print(name + ' is in the ' + currentRoom)
  45. print("Health : " + str(health))
  46. # Print the current inventory
  47. print("Inventory : " + str(inventory))
  48. # Print an item if there is one
  49. if "item" in rooms[currentRoom]:
  50. print('You see a ' + rooms[currentRoom]['item'])
  51. print("---------------------------")
  52. '''
  53. # Set up the game
  54. name = None
  55. health = 5
  56. currentRoom = 'Hall'
  57. inventory = []
  58. '''
  59. #-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
  60. # Load data from the file
  61.  
  62. gamedata = {
  63. "playername": name,
  64. "playerhealth": health,
  65. "playercurrentRoom": currentRoom,
  66. "inventory": inventory
  67. }
  68.  
  69. with open("gamedata.json", "w") as f:
  70. json.dump(gamedata, f)
  71.  
  72. # A dictionary linking a room to other room positions
  73. rooms = {
  74. 'Hall' : { 'south' : 'Kitchen',
  75. 'east' : 'Dining Room',
  76. 'item' : 'key'
  77. },
  78.  
  79. 'Kitchen' : { 'north' : 'Hall',
  80. 'item' : 'monster'
  81. },
  82.  
  83. 'Dining Room' : { 'west' : 'Hall',
  84. 'south' : 'Garden',
  85. 'item' : 'potion'
  86. },
  87.  
  88. 'Garden' : { 'north' : 'Dining Room' }
  89. }
  90.  
  91. # Ask the player their name
  92. if name is None:
  93. name = input("What is your name, Adventurer? ")
  94. showInstructions()
  95.  
  96. # Loop forever
  97. while True:
  98.  
  99. showStatus()
  100.  
  101. # Get the player's next 'move'
  102. # .split() breaks it up into an list array
  103. # e.g. typing 'go east' would give the list:
  104. # ['go','east']
  105. move = ''
  106. while move == '':
  107. move = input('>')
  108.  
  109. move = move.lower().split()
  110.  
  111. # If they type 'go' first
  112. if move[0] == 'go':
  113. health = health - 1
  114. # Check that they are allowed wherever they want to go
  115. if move[1] in rooms[currentRoom]:
  116. # Set the current room to the new room
  117. currentRoom = rooms[currentRoom][move[1]]
  118. # or, if there is no door (link) to the new room
  119. else:
  120. print('You can\'t go that way!')
  121.  
  122. # If they type 'get' first
  123. if move[0] == 'get' :
  124. # If the room contains an item, and the item is the one they want to get
  125. if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
  126. # Add the item to their inventory
  127. inventory += [move[1]]
  128. # Display a helpful message
  129. print(move[1] + ' got!')
  130. # Delete the item from the room
  131. del rooms[currentRoom]['item']
  132. # Otherwise, if the item isn't there to get
  133. else:
  134. # Tell them they can't get it
  135. print('Can\'t get ' + move[1] + '!')
  136.  
  137. # Player loses if they enter a room with a monster
  138. if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
  139. print('A monster has got you... GAME OVER!')
  140. break
  141.  
  142. if health == 0:
  143. print('You collapse from exhaustion... GAME OVER!')
  144.  
  145. # Player wins if they get to the garden with a key and a potion
  146. if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
  147. print('You escaped the house... YOU WIN!')
  148. break
  149.  
  150. #-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
  151. # Save game data to the file
  152.  
  153. gamedata = {
  154. "playername": name,
  155. "playerhealth": health,
  156. "PlayercurrentRoom": currentRoom,
  157. "inventory": inventory
  158. }
  159. with open("gamedata.json", "w") as f:
  160. json.dump(gamedata, f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement