Advertisement
Mori007

rpgdragon

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