maroph

RPG2

Nov 11th, 2019
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.17 KB | None | 0 0
  1. import json
  2. import sys
  3.  
  4. try:
  5.     print("Retrieving player details")
  6.     with open("gamedata2.json", "r") as f:
  7.         gamedata = json.load(f)
  8.         version = gamedata["version"]
  9.         name = gamedata["playername"]
  10.         health = gamedata["playerhealth"]
  11.         currentRoom = gamedata["playercurrentRoom"]
  12.         inventory = gamedata["inventory"]
  13. except:
  14.     print("No (valid) gamedata found, creating a new gamedata file")
  15.     version = 2
  16.     name = None
  17.     health = 5
  18.     currentRoom = 'Hall'
  19.     inventory = []
  20.     with open("gamedata2.json", "w") as f:
  21.         json.dump(gamedata, f)
  22.  
  23. gamedata_valid = True
  24.  
  25. if not isinstance(version, int):
  26.     print("no valid version information found")
  27.     gamedata_valid = False
  28.  
  29. if version != 2:
  30.     print("version of gamedata unknown [version:" + str(version) + "]")
  31.     print("expected version 2")
  32.     gamedata_valid = False
  33.  
  34. if not isinstance(name, str):
  35.     print("no valid player name found")
  36.     gamedata_valid = False
  37.  
  38. if name == '':
  39.     print("no valid player name found")
  40.     gamedata_valid = False
  41.  
  42. if not gamedata_valid:
  43.     print("the retrieved game data are not valid - start a new game")
  44.     version = 2
  45.     name = None
  46.     health = 5
  47.     currentRoom = 'Hall'
  48.     inventory = []
  49.  
  50. if health <= 0:
  51.     print('You are already dead - but we give you a second chance!')
  52.     version = 2
  53.     health = 5
  54.     currentRoom = 'Hall'
  55.     inventory = []
  56.  
  57. if health > 5:
  58.     for i in range(10):
  59.         print("***** YOU ARE A CHEATER *****")
  60.     sys.exit(1)
  61.  
  62. if currentRoom != 'Hall':
  63.     if health == 5:
  64.         for i in range(10):
  65.             print("***** YOU ARE A CHEATER *****")
  66.         sys.exit(1)
  67.  
  68. if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
  69.     print('You already escaped the house')
  70.     resp = input('Do you want to give it another try? [y/n]>')
  71.     if resp =='Y' or resp == 'y':
  72.         version = 2
  73.         health = 5
  74.         currentRoom = 'Hall'
  75.         inventory = []
  76.     else:
  77.         sys.exit(0)
  78.  
  79. def showInstructions():
  80.     # Print a main menu and the commands
  81.     print('''
  82. RPG Game
  83. ========
  84.  
  85. Get to the Garden with a key and a potion.
  86. Avoid the monsters!
  87.  
  88. You are getting tired; each time you move you lose 1 health point.
  89.  
  90. Commands:
  91.  go [direction]
  92.  get [item]
  93.  quit
  94. ''')
  95.  
  96.  
  97. def showStatus():
  98.     # Print the player's current status
  99.     print('---------------------------')
  100.     print(name + ' is in the ' + currentRoom)
  101.     print("Health : " + str(health))
  102.     # Print the current inventory
  103.     print("Inventory : " + str(inventory))
  104.     # Print an item if there is one
  105.     if "item" in rooms[currentRoom]:
  106.         if rooms[currentRoom]['item'] not in inventory:
  107.             print('You see a ' + rooms[currentRoom]['item'])
  108.     print("---------------------------")
  109.  
  110.  
  111. gamedata = {
  112.     "version": version,
  113.     "playername": name,
  114.     "playerhealth": health,
  115.     "playercurrentRoom": currentRoom,
  116.     "inventory": inventory
  117. }
  118.  
  119. with open("gamedata2.json", "w") as f:
  120.     json.dump(gamedata, f)
  121.  
  122. # A dictionary linking a room to other room positions
  123. rooms = {
  124.     'Hall': {'south': 'Kitchen',
  125.              'east': 'Dining Room',
  126.              'item': 'key'
  127.              },
  128.  
  129.     'Kitchen': {'north': 'Hall',
  130.                 'item': 'monster'
  131.                 },
  132.  
  133.     'Dining Room': {'west': 'Hall',
  134.                     'south': 'Garden',
  135.                     'item': 'potion'
  136.                     },
  137.  
  138.     'Garden': {'north': 'Dining Room'}
  139. }
  140.  
  141. # Ask the player their name
  142. if name is None:
  143.     name = input("What is your name, Adventurer? ")
  144.     showInstructions()
  145.  
  146. # Loop forever
  147. while True:
  148.  
  149.     showStatus()
  150.  
  151.     move = ''
  152.     while move == '':
  153.         move = input('>')
  154.  
  155.     move = move.lower().split()
  156.  
  157.     # Get the player's next 'move'
  158.     # .split() breaks it up into an list array
  159.     # e.g. typing 'go east' would give the list:
  160.     # ['go','east']
  161.  
  162.     if move[0] == 'quit':
  163.         break
  164.  
  165.     # If they type 'go' first
  166.     if move[0] == 'go':
  167.         health = health - 1
  168.         # Check that they are allowed wherever they want to go
  169.         if len(move) > 1:
  170.             if move[1] in rooms[currentRoom]:
  171.                 # Set the current room to the new room
  172.                 currentRoom = rooms[currentRoom][move[1]]
  173.             # or, if there is no door (link) to the new room
  174.             else:
  175.                 print('You can\'t go that way!')
  176.  
  177.     # If they type 'get' first
  178.     if move[0] == 'get':
  179.         if len(move) > 1:
  180.             # If the room contains an item, and the item is the one they want to get
  181.             if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
  182.                 # Add the item to their inventory
  183.                 inventory += [move[1]]
  184.                 # Display a helpful message
  185.                 print(move[1] + ' got!')
  186.                 # Delete the item from the room
  187.                 del rooms[currentRoom]['item']
  188.             # Otherwise, if the item isn't there to get
  189.             else:
  190.                 # Tell them they can't get it
  191.                 print('Can\'t get ' + move[1] + '!')
  192.         else:
  193.             print('Can\'t get ' + move[1] + '!')
  194.  
  195.     # Player loses if they enter a room with a monster
  196.     if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
  197.         print('A monster has got you... GAME OVER!')
  198.         break
  199.  
  200.     if health <= 0:
  201.         print('You collapse from exhaustion... GAME OVER!')
  202.  
  203.     # Player wins if they get to the garden with a key and a potion
  204.     if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
  205.         print('You escaped the house... YOU WIN!')
  206.         break
  207.  
  208.     gamedata = {
  209.         "version": version,
  210.         "playername": name,
  211.         "playerhealth": health,
  212.         "playercurrentRoom": currentRoom,
  213.         "inventory": inventory
  214.     }
  215.     with open("gamedata2.json", "w") as f:
  216.         json.dump(gamedata, f)
  217.  
  218. gamedata = {
  219.     "version": version,
  220.     "playername": name,
  221.     "playerhealth": health,
  222.     "playercurrentRoom": currentRoom,
  223.     "inventory": inventory
  224. }
  225. with open("gamedata2.json", "w") as f:
  226.     json.dump(gamedata, f)
Advertisement
Add Comment
Please, Sign In to add comment