scipiguy

Futurelearn Files - Adventure JSON

Nov 25th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  1. import json
  2.  
  3. try:
  4.     print("Retreiving player details")
  5.     with open("gamedata.json", "r") as f:
  6.         gamedata = json.load(f)
  7.         name = gamedata["playername"]
  8.         health = gamedata["playerhealth"]
  9.         currentRoom = gamedata["playercurrentRoom"]
  10.         inventory = ["inventory"]
  11. except:
  12.     print("No player found, creating a new gamedata file")
  13.     f = open('gamedata.json', 'w')
  14.     f.close()
  15.     name = None
  16.     health = 5
  17.     currentRoom = "Hall"
  18.     inventory = []
  19.        
  20. def showInstructions():
  21.     # Print a main menu and the commands
  22.     print('''
  23. RPG Game
  24. ========
  25.  
  26. Get to the Garden with a key and a potion.
  27. Avoid the monsters!
  28.  
  29. You are getting tired; each time you move you lose 1 health point.
  30.  
  31. Commands:
  32.  go [direction]
  33.  get [item]
  34. ''')
  35.  
  36. def showStatus():
  37.   # Print the player's current status
  38.   print('---------------------------')
  39.   print(name + ' is in the ' + currentRoom)
  40.   print("Health : " + str(health))
  41.   # Print the current inventory
  42.   print("Inventory : " + str(inventory))
  43.   # Print an item if there is one
  44.   if "item" in rooms[currentRoom]:
  45.       if rooms[currentRoom]['item'] not in inventory:
  46.           print('You see a ' + rooms[currentRoom]['item'])
  47.   print("---------------------------")
  48.  
  49.  
  50. # Load data from the file
  51.  
  52. gamedata = {
  53.     "playername": name,
  54.     "playerhealth": health,
  55.     "playercurrentRoom": currentRoom,
  56.     "inventory": inventory
  57.     }
  58.  
  59. with open("gamedata.json", "w") as f:
  60.     json.dump(gamedata, f)
  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.   if health == 0:
  133.     print('You collapse from exhaustion... GAME OVER!')
  134.  
  135.   # Player wins if they get to the garden with a key and a potion
  136.   if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
  137.     print('You escaped the house... YOU WIN!')
  138.     break
  139.  
  140.   # Save game data to the file
  141.   gamedata = {
  142.       "playername": name,
  143.       "playerhealth": health,
  144.       "playercurrentRoom": currentRoom,
  145.       "inventory": inventory
  146.       }
  147.   with open("gamedata.json", "w") as f:
  148.       json.dump(gamedata, f)
Add Comment
Please, Sign In to add comment