Antypas

RPG data validation

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