Advertisement
Antypas

RPG game Saving progress

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