Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import sys
- try:
- print("Retrieving player details")
- with open("gamedata2.json", "r") as f:
- gamedata = json.load(f)
- version = gamedata["version"]
- name = gamedata["playername"]
- health = gamedata["playerhealth"]
- currentRoom = gamedata["playercurrentRoom"]
- inventory = gamedata["inventory"]
- except:
- print("No (valid) gamedata found, creating a new gamedata file")
- version = 2
- name = None
- health = 5
- currentRoom = 'Hall'
- inventory = []
- with open("gamedata2.json", "w") as f:
- json.dump(gamedata, f)
- gamedata_valid = True
- if not isinstance(version, int):
- print("no valid version information found")
- gamedata_valid = False
- if version != 2:
- print("version of gamedata unknown [version:" + str(version) + "]")
- print("expected version 2")
- gamedata_valid = False
- if not isinstance(name, str):
- print("no valid player name found")
- gamedata_valid = False
- if name == '':
- print("no valid player name found")
- gamedata_valid = False
- if not gamedata_valid:
- print("the retrieved game data are not valid - start a new game")
- version = 2
- name = None
- health = 5
- currentRoom = 'Hall'
- inventory = []
- if health <= 0:
- print('You are already dead - but we give you a second chance!')
- version = 2
- health = 5
- currentRoom = 'Hall'
- inventory = []
- if health > 5:
- for i in range(10):
- print("***** YOU ARE A CHEATER *****")
- sys.exit(1)
- if currentRoom != 'Hall':
- if health == 5:
- for i in range(10):
- print("***** YOU ARE A CHEATER *****")
- sys.exit(1)
- if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
- print('You already escaped the house')
- resp = input('Do you want to give it another try? [y/n]>')
- if resp =='Y' or resp == 'y':
- version = 2
- health = 5
- currentRoom = 'Hall'
- inventory = []
- else:
- sys.exit(0)
- 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]
- quit
- ''')
- 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]:
- if rooms[currentRoom]['item'] not in inventory:
- print('You see a ' + rooms[currentRoom]['item'])
- print("---------------------------")
- gamedata = {
- "version": version,
- "playername": name,
- "playerhealth": health,
- "playercurrentRoom": currentRoom,
- "inventory": inventory
- }
- with open("gamedata2.json", "w") as f:
- json.dump(gamedata, f)
- # 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'}
- }
- # Ask the player their name
- if name is None:
- name = input("What is your name, Adventurer? ")
- showInstructions()
- # Loop forever
- while True:
- showStatus()
- move = ''
- while move == '':
- move = input('>')
- move = move.lower().split()
- # Get the player's next 'move'
- # .split() breaks it up into an list array
- # e.g. typing 'go east' would give the list:
- # ['go','east']
- if move[0] == 'quit':
- break
- # If they type 'go' first
- if move[0] == 'go':
- health = health - 1
- # Check that they are allowed wherever they want to go
- if len(move) > 1:
- if move[1] in rooms[currentRoom]:
- # Set the current room to the new room
- currentRoom = rooms[currentRoom][move[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 move[0] == 'get':
- if len(move) > 1:
- # 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']:
- # Add the item to their inventory
- inventory += [move[1]]
- # Display a helpful message
- print(move[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 ' + move[1] + '!')
- else:
- print('Can\'t get ' + move[1] + '!')
- # Player loses if they enter a room with a monster
- if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
- print('A monster has got you... GAME OVER!')
- break
- if health <= 0:
- print('You collapse from exhaustion... GAME OVER!')
- # 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!')
- break
- gamedata = {
- "version": version,
- "playername": name,
- "playerhealth": health,
- "playercurrentRoom": currentRoom,
- "inventory": inventory
- }
- with open("gamedata2.json", "w") as f:
- json.dump(gamedata, f)
- gamedata = {
- "version": version,
- "playername": name,
- "playerhealth": health,
- "playercurrentRoom": currentRoom,
- "inventory": inventory
- }
- with open("gamedata2.json", "w") as f:
- json.dump(gamedata, f)
Advertisement
Add Comment
Please, Sign In to add comment