Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- #=================================================================
- def showInstructions():
- # Print a main menu and the commands
- print('''
- RPG Game
- ========
- Get to the Garden with a key and a potion.
- Avoid the monsters!
- You are getting tired; each time you move you lose 1 health point.
- Commands:
- go [direction]
- get [item]
- ''')
- #=================================================================
- # Set up the game
- def resetStatus():
- global name, health, currentRoom, inventory
- name = None
- health = 5
- currentRoom = 'Hall'
- inventory = []
- #=================================================================
- def showStatus():
- # Print the player's current status
- print('---------------------------')
- print(name + ' is in the ' + currentRoom)
- print("Health : " + str(health))
- # Print the current inventory
- print("Inventory : " + str(inventory))
- # Print an item if there is one
- if "item" in rooms[currentRoom]:
- print('You see a ' + rooms[currentRoom]['item'])
- print("---------------------------")
- #=================================================================
- # Load data from the file
- try:
- print("Retrieving player details")
- with open("gamedata.json", "r") as f:
- gamedata = json.load(f)
- name = gamedata["playername"]
- health = gamedata["playerhealth"]
- currentRoom = gamedata["playercurrentRoom"]
- inventory = gamedata["playerbackpack"]
- except FileNotFoundError:
- #print("No player found, creating a new gamedata file")
- resetStatus()
- # A dictionary linking a room to other room positions
- rooms = {
- 'Hall' : { 'south' : 'Kitchen',
- 'east' : 'Dining Room',
- 'item' : 'key'
- },
- 'Kitchen' : { 'north' : 'Hall',
- 'item' : 'monster'
- },
- 'Dining Room' : { 'west' : 'Hall',
- 'south' : 'Garden',
- 'item' : 'potion'
- },
- 'Garden' : { 'north' : 'Dining Room' }
- }
- # Remove item from each room if item in backpack
- for thisRoom in rooms:
- if rooms[thisRoom].get('item') in inventory:
- del rooms[thisRoom]['item']
- # Ask the player their name
- if name is None:
- name = input("What is your name, Adventurer? ")
- showInstructions()
- # Loop forever .. until break
- while True:
- gameOver = False
- showStatus()
- # Get the player's next 'move'
- move = ''
- while move == '':
- move = input('>').strip()
- move = move.lower().split()
- if len( move ) >= 2 :
- # If they type 'go' first
- if move[0] == 'go':
- health = health - 1
- goDirection = move[1]
- # Check that they are allowed wherever they want to go
- if goDirection in rooms[currentRoom]:
- # Set the current room to the new room
- currentRoom = rooms[currentRoom][ goDirection ]
- # or, if there is no door (link) to the new room
- else:
- print('You can\'t go that way!')
- # If they type 'get' first
- if move[0] == 'get' :
- # If the room contains an item, and the item is the one they want to get
- #if 'item' in rooms[currentRoom] and move[1] in rooms[currentRoom]['item']:
- itemRequested = move[1]
- #if 'item' in rooms[currentRoom] and rooms[currentRoom]['item'] == itemRequested :
- if rooms[currentRoom].get('item') == itemRequested :
- # Add the item to their inventory
- #inventory += [itemRequested]
- inventory.append( itemRequested )
- # Display a helpful message
- print( itemRequested + ' got!')
- # Delete the item from the roomplayerbackpack
- del rooms[currentRoom]['item']
- # Otherwise, if the item isn't there to get
- else:
- # Tell them they can't get it
- print('Can\'t get ' + itemRequested + '!')
- #-------------------------------------------------
- # Player wins if they get to the garden with a key and a potion
- if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
- print('You escaped the house... YOU WIN!')
- print('*********************************')
- gameOver = True
- resetStatus()
- elif rooms[currentRoom].get('item') == 'monster':
- # Player loses if they enter a room with a monster
- print('A monster has got you... GAME OVER!')
- gameOver = True
- resetStatus()
- elif health <= 0:
- print('You collapse from exhaustion... GAME OVER!')
- gameOver = True
- resetStatus()
- # Save game data to the file
- gamedata = {
- "playername" : name,
- "playerhealth" : health,
- "playercurrentRoom": currentRoom,
- "playerbackpack" : inventory
- }
- with open("gamedata.json", "w") as f:
- json.dump( gamedata, f )
- if gameOver == True:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement