Advertisement
xavicano

Untitled

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