Advertisement
johnpentyrch

2p9

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