Advertisement
JAS_Software

RPG

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