Advertisement
JAS_Software

RPG2

May 8th, 2021
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. import os
  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 one health point.
  14.  
  15. Commands:
  16.    go [north | south | east | west ]
  17.    get [item]
  18.    map
  19.    save
  20.    quit
  21. """)
  22.  
  23. def showStatus(health,currentRoom,inventory,rooms):
  24.     # Print the player's current status
  25.     print("---------------------------")
  26.     print(name + " is in the " + currentRoom)
  27.     print("Health : " + str(health))
  28.     # Print the current inventory
  29.     print("Inventory : " + str(inventory))
  30.     # Print an item if there is one
  31.     if "item" in rooms[currentRoom]:
  32.         print("You see a " + rooms[currentRoom]["item"])
  33.     displayDirections(rooms,currentRoom)
  34.     print("---------------------------")
  35.  
  36. def displayDirections(rooms,currentRoom):
  37.     directions = ''
  38.     if 'north' in rooms[currentRoom]:
  39.         directions = 'North > {}'.format(rooms[currentRoom]['north']) + '; '
  40.     if 'south' in rooms[currentRoom]:
  41.         directions += 'South > {}'.format(rooms[currentRoom]['south']) + '; '
  42.     if 'east' in rooms[currentRoom]:
  43.         directions += 'East > {}'.format(rooms[currentRoom]['east']) + '; '
  44.     if 'west' in rooms[currentRoom]:
  45.         directions += 'West > {}'.format(rooms[currentRoom]['west']) + '; '
  46.     print(directions)
  47.  
  48. def getDefaults():
  49.     name = None
  50.     health = 5
  51.     currentRoom = "Hall"
  52.     inventory = []
  53.     return name,health,currentRoom,inventory
  54.  
  55. def getRooms():
  56. # A dictionary linking a room to other room positions
  57.     rooms = {
  58.           "Hall" : { "south" : "Kitchen",
  59.                      "east"  : "Dining Room",
  60.                      "item"  : "key"
  61.                    },
  62.  
  63.           "Kitchen" : { "north" : "Hall",
  64.                         "item"  : "monster"
  65.                       },
  66.  
  67.           "Dining Room" : { "west"  : "Hall",
  68.                             "south" : "Garden",
  69.                             "item"  : "potion"
  70.                           },
  71.  
  72.           "Garden" : { "north" : "Dining Room" }
  73.         }
  74.     return rooms
  75.  
  76. def getAction():
  77.     action = ''
  78.     while action == '':
  79.         action = input('>')
  80.     return action
  81.  
  82. def game(health,currentRoom,inventory,rooms):
  83.     play = True
  84.     result = ""
  85.     while play:
  86.         showStatus(health,currentRoom,inventory,rooms)
  87.         action = getAction()
  88.  
  89.         action = action.lower().split()
  90.         if action[0] == 'save':
  91.             saveGame(name,health,currentRoom)
  92.             print('Game Saved')
  93.  
  94.         if action[0] == 'quit':
  95.             result = 'quit'
  96.             play = False
  97.  
  98.         if action[0] == 'map':
  99.             displayMap(rooms)
  100.  
  101.         if action[0] == "go":
  102.             health = health - 1
  103.             # Check that they are allowed wherever they want to go
  104.             if action[1] in rooms[currentRoom]:
  105.                 # Set the current room to the new room
  106.                 currentRoom = rooms[currentRoom][action[1]]
  107.                 # or, if there is no door (link) to the new room
  108.             else:
  109.                 print("You can't go that way!")
  110.  
  111.  
  112.         # If they type "get" first
  113.         if action[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 action[1] in rooms[currentRoom]["item"]:
  116.                 # Add the item to their inventory
  117.                 inventory += [action[1]]
  118.                 # Display a helpful message
  119.                 print(action[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 " + action[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.             result = 'monster'
  130.             play = False
  131.  
  132.         if health == 0:
  133.             result = 'health'
  134.             play = False
  135.  
  136.         # Player wins if they get to the garden with a key and a potion
  137.         if currentRoom == "Garden" and "key" in inventory and "potion" in inventory:
  138.             result = 'win'
  139.             play = False
  140.  
  141.     return result
  142.  
  143. def displayMap(rooms):
  144.     print(rooms)
  145.  
  146. def displayResult(result):
  147.     if result == 'monster':
  148.         print("A monster has got you ... GAME OVER!")
  149.     elif result == 'health':
  150.         print("You collapse from exhaustion ... GAME OVER!")
  151.     elif result == 'win':
  152.         print("You escaped the house ... YOU WIN!")
  153.     elif result == 'quit':
  154.         print("Goodbye, thanks for playing")
  155.     else:
  156.         print("Something went awry")
  157.  
  158. def loadSavedGame():
  159.     response = input('Load Save Game (y/n)? ').strip()
  160.     if response == 'y':
  161.         load = True
  162.     else:
  163.         load = False
  164.     return load
  165.  
  166. def loadGame():
  167.     try:
  168.         with open('gameData.json','r') as f:
  169.             gameData = json.load(f)
  170.             health = gameData['playerhealth']
  171.             name = gameData['playername']
  172.             currentRoom = gameData['playercurrentroom']
  173.             inventory  = []
  174.         if health < 0 or health > 5:
  175.             name,health,currentRoom,inventory = getDefaults()
  176.             os.remove('gameData.json')
  177.             print('You have been cheating!')
  178.             print('Game Data is being deleted!')
  179.  
  180.     except FileNotFoundError:
  181.         name,health,currentRoom,inventory = getDefaults()
  182.     return name,health,currentRoom,inventory
  183.  
  184. def saveGame(name,health,currentRoom):
  185.     gameData = {
  186.         'playername':name,
  187.         'playerhealth':health,
  188.         'playercurrentroom':currentRoom
  189.         }
  190.     with open('gameData.json','w') as f:
  191.         json.dump(gameData,f)
  192.  
  193. rooms = getRooms()
  194. name,health,currentRoom,inventory = getDefaults()
  195. if loadSavedGame():
  196.     name,health,currentRoom,inventory  = loadGame()
  197.  
  198. if name is None:
  199.     name = input("What is your name, Adventurer? ")
  200.     showInstructions()
  201. result = game(health,currentRoom,inventory,rooms)
  202. displayResult(result)
  203.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement