maroph

RPG

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