brendan-stanford

RPI_RPG+backpack

Jan 27th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 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. #if no file found, create one
  46. except FileNotFoundError:
  47. print("No player found; creating new gamedata")
  48. f = open("gamedata.json", "w")
  49. f.close()
  50. name = None
  51. health = 5
  52. currentRoom = 'Hall'
  53. inventory = []
  54.  
  55. # A dictionary linking a room to other room positions
  56. rooms = {
  57. 'Hall' : { 'south' : 'Kitchen',
  58. 'east' : 'Dining Room',
  59. 'item' : 'key'
  60. },
  61.  
  62. 'Kitchen' : { 'north' : 'Hall',
  63. 'item' : 'monster'
  64. },
  65.  
  66. 'Dining Room' : { 'west' : 'Hall',
  67. 'south' : 'Garden',
  68. 'item' : 'potion'
  69. },
  70.  
  71. 'Garden' : { 'north' : 'Dining Room' }
  72. }
  73.  
  74. # Ask the player their name
  75. if name is None:
  76. name = input("What is your name, Adventurer? ")
  77. showInstructions()
  78.  
  79. # Loop forever
  80. while True:
  81.  
  82. showStatus()
  83.  
  84. # Get the player's next 'move'
  85. # .split() breaks it up into an list array
  86. # e.g. typing 'go east' would give the list:
  87. # ['go','east']
  88. move = ''
  89. while move == '':
  90. move = input('>')
  91.  
  92. move = move.lower().split()
  93.  
  94. # If they type 'go' first
  95. if move[0] == 'go':
  96. health = health - 1
  97. # Check that they are allowed wherever they want to go
  98. if move[1] in rooms[currentRoom]:
  99. # Set the current room to the new room
  100. currentRoom = rooms[currentRoom][move[1]]
  101. # or, if there is no door (link) to the new room
  102. else:
  103. print('You can\'t go that way!')
  104.  
  105. # If they type 'get' first
  106. if move[0] == 'get' :
  107. # If the room contains an item, and the item is the one they want to get
  108. if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
  109. # Add the item to their inventory
  110. inventory += [move[1]]
  111. # Display a helpful message
  112. print(move[1] + ' got!')
  113. # Delete the item from the room
  114. del rooms[currentRoom]['item']
  115. # Otherwise, if the item isn't there to get
  116. else:
  117. # Tell them they can't get it
  118. print('Can\'t get ' + move[1] + '!')
  119.  
  120. # Player loses if they enter a room with a monster
  121. if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
  122. print('A monster has got you... GAME OVER!')
  123. break
  124.  
  125. if health == 0:
  126. print('You collapse from exhaustion... GAME OVER!')
  127.  
  128. # Player wins if they get to the garden with a key and a potion
  129. if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
  130. print('You escaped the house... YOU WIN!')
  131. break
  132.  
  133.  
  134.  
  135. #-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
  136. # Save game data to the file
  137. gamedata = {
  138. "playername": name,
  139. "playerhealth": health,
  140. "playercurrentRoom": currentRoom,
  141. "backpack": inventory
  142. }
  143.  
  144. with open("gamedata.json", "w") as f:
  145. json.dump(gamedata, f)
Advertisement
Add Comment
Please, Sign In to add comment