Advertisement
JDpaste

RPG 103

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