Advertisement
brendan-stanford

RPI_RPG+cheats

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