Advertisement
khrislewis

Adventure Game

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