Advertisement
xavicano

Untitled

May 13th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 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 1 health point.
  13.  
  14. Commands:
  15.  go [direction]
  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. # Set up the game
  32. #name = None
  33. #health = 5
  34. #currentRoom = 'Hall'
  35. #inventory = []
  36.  
  37. #-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
  38. # Load data from the file
  39.  
  40.  
  41. try:
  42.     print("Retrieving player details")
  43.     with open("gamedata.json", "rb") as f:
  44.         gamedata = json.load(f)
  45.         name = gamedata["playername"]
  46.         health = gamedata["playerhealth"]
  47.         if health <= 0:
  48.             print('You collapse from exhaustion... GAME OVER!')
  49.         if health > 5:
  50.             for i in range(10):
  51.                 print("You have cheated")
  52.         currentRoom = gamedata["playercurrentRoom"]
  53.         inventory = gamedata["playerinventory"]
  54. except FileNotFoundError:
  55.     print("No player found, creating a new gamedata file")
  56.     f = open('gamedata.json', 'w')
  57.     f.close()
  58.     name = None
  59.     health = 5
  60.     currentRoom = 'Hall'
  61.     inventory = []
  62.    
  63.  
  64.  
  65. # A dictionary linking a room to other room positions
  66. rooms = {
  67.     'Hall' : { 'south' : 'Kitchen',
  68.                'east'  : 'Dining Room',
  69.                'item'  : 'key'
  70.              },
  71.     'Kitchen' : { 'north' : 'Hall',
  72.                   'item'  : 'monster'
  73.              },
  74.     'Dining Room' : { 'west'  : 'Hall',
  75.                       'south' : 'Garden',
  76.                       'item'  : 'potion'
  77.                     },
  78.      'Garden' : { 'north' : 'Dining Room' }
  79.  }
  80.  
  81. # Ask the player their name
  82. if name is None:
  83.   name = input("What is your name, Adventurer? ")
  84.   showInstructions()
  85.  
  86. # Loop forever
  87. while True:
  88.  
  89.     showStatus()
  90.  
  91.   # Get the player's next 'move'
  92.   # .split() breaks it up into an list array
  93.   # e.g. typing 'go east' would give the list:
  94.   # ['go','east']
  95.     move = ''
  96.     while move == '':
  97.         move = input('>')
  98.  
  99.     move = move.lower().split()
  100.  
  101.   # If they type 'go' first
  102.     if move[0] == 'go':
  103.         health = health - 1
  104.     # Check that they are allowed wherever they want to go
  105.         if move[1] in rooms[currentRoom]:
  106.       # Set the current room to the new room
  107.             currentRoom = rooms[currentRoom][move[1]]
  108.     # or, if there is no door (link) to the new room
  109.         else:
  110.             print('You can\'t go that way!')
  111.  
  112.   # If they type 'get' first
  113.     if move[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 move[1] in rooms[currentRoom]['item']:
  116.       # Add the item to their inventory
  117.             inventory += [move[1]]
  118.       # Display a helpful message
  119.             print(move[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 ' + move[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.         print('A monster has got you... GAME OVER!')
  130.         break
  131.  
  132.     if health == 0:
  133.         print('You collapse from exhaustion... GAME OVER!')
  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.         print('You escaped the house... YOU WIN!')
  138.         break
  139.  
  140.       #-# CODE WILL BE ADDED HERE IN THE NEXT STEP #-#
  141.       # Save game data to the file
  142. gamedata = {
  143.       "playername": name,
  144.       "playerhealth": health,
  145.       "playercurrentRoom": currentRoom,
  146.       "playerinventory":inventory}
  147.  
  148. with open("gamedata.json", "wb") as f:
  149.     json.dump(gamedata, f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement