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 one health point.
- Commands:
- go [north | south | east | west ]
- get [item]
- map
- save
- quit
- """)
- def showStatus(health,currentRoom,inventory,rooms):
- # 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"])
- displayDirections(rooms,currentRoom)
- print("---------------------------")
- def displayDirections(rooms,currentRoom):
- directions = ''
- if 'north' in rooms[currentRoom]:
- directions = 'North > {}'.format(rooms[currentRoom]['north']) + '; '
- if 'south' in rooms[currentRoom]:
- directions += 'South > {}'.format(rooms[currentRoom]['south']) + '; '
- if 'east' in rooms[currentRoom]:
- directions += 'East > {}'.format(rooms[currentRoom]['east']) + '; '
- if 'west' in rooms[currentRoom]:
- directions += 'West > {}'.format(rooms[currentRoom]['west']) + '; '
- print(directions)
- def getDefaults():
- name = None
- health = 5
- currentRoom = "Hall"
- inventory = []
- return name,health,currentRoom,inventory
- def getRooms():
- # 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" }
- }
- return rooms
- def getAction():
- action = ''
- while action == '':
- action = input('>')
- return action
- def game(health,currentRoom,inventory,rooms):
- play = True
- result = ""
- while play:
- showStatus(health,currentRoom,inventory,rooms)
- action = getAction()
- action = action.lower().split()
- if action[0] == 'save':
- saveGame(name,health,currentRoom)
- print('Game Saved')
- if action[0] == 'quit':
- result = 'quit'
- play = False
- if action[0] == 'map':
- displayMap(rooms)
- if action[0] == "go":
- health = health - 1
- # Check that they are allowed wherever they want to go
- if action[1] in rooms[currentRoom]:
- # Set the current room to the new room
- currentRoom = rooms[currentRoom][action[1]]
- # 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 action[0] == "get" :
- # If the room contains an item, and the item is the one they want to get
- if "item" in rooms[currentRoom] and action[1] in rooms[currentRoom]["item"]:
- # Add the item to their inventory
- inventory += [action[1]]
- # Display a helpful message
- print(action[1] + " got!")
- # Delete the item from the room
- 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 " + action[1] + "!")
- # Player loses if they enter a room with a monster
- if "item" in rooms[currentRoom] and "monster" in rooms[currentRoom]["item"]:
- result = 'monster'
- play = False
- if health == 0:
- result = 'health'
- play = False
- # 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:
- result = 'win'
- play = False
- return result
- def displayMap(rooms):
- print(rooms)
- def displayResult(result):
- if result == 'monster':
- print("A monster has got you ... GAME OVER!")
- elif result == 'health':
- print("You collapse from exhaustion ... GAME OVER!")
- elif result == 'win':
- print("You escaped the house ... YOU WIN!")
- elif result == 'quit':
- print("Goodbye, thanks for playing")
- else:
- print("Something went awry")
- def loadSavedGame():
- response = input('Load Save Game (y/n)? ').strip()
- if response == 'y':
- load = True
- else:
- load = False
- return load
- def loadGame():
- try:
- with open('gameData.json','r') as f:
- gameData = json.load(f)
- name = gameData['playername']
- health = gameData['playerhealth']
- currentRoom = gameData['playercurrentroom']
- inventory = []
- except FileNotFoundError:
- name,health,currentRoom,inventory = getDefaults()
- return name,health,currentRoom,inventory
- def saveGame(name,health,currentRoom):
- gameData = {
- 'playername':name,
- 'playerhealth':health,
- 'playercurrentroom':currentRoom
- }
- with open('gameData.json','w') as f:
- json.dump(gameData,f)
- rooms = getRooms()
- name,health,currentRoom,inventory = getDefaults()
- if loadSavedGame():
- name,health,currentRoom,inventory = loadGame()
- if name is None:
- name = input("What is your name, Adventurer? ")
- showInstructions()
- result = game(health,currentRoom,inventory,rooms)
- displayResult(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement