Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- This file is used in a simple adventure game to teach the use of classes.
- To run this application you need 4 files:
- SimpleAdventureProgram.py: https://pastebin.com/jm9aHBWR
- (mimics C# Program.cs class with Main() function as entry point)
- SimpleAdventureLocation.py: https://pastebin.com/xREn6WB9
- (Class containing properties of exits to/from other locations, name and description)
- SimpleAdventureShared.py: https://pastebin.com/9hRT5XA2
- (Static class containing project-scope global variables and various methods)
- GetInput.py: https://pastebin.com/UceAJsaV
- (Static class with functions to get user input and display menus)
- '''
- import time
- import random
- import SimpleAdventureShared as Shared
- from SimpleAdventureLocation import *
- from GetInput import *
- '''
- module level variable needed is dictionary of Locations.
- This will need 'global' declaration in any function that changes them!!!!
- (This is not required in C# and Java)
- To avoid the confusion, all global variables are stored in 'Shared.py' and prefixed with Shared.
- e.g. Shared.dictLocations = {} holds a dictionary of Location objects available to any file via 'import Shared'
- '''
- def CreateLocations():
- ret_value = True
- #******************************************************************************************
- # Start of code you can alter.
- '''
- AddToLocations("location name", "location display name", "location description")
- SetLocationConnections("location name", "north exit", "east exit", "south exit", "west exit") # N E S W use empty string if no exit
- '''
- AddToLocations("room", "a hotel room", "smelling of stale cigarettes\n(the room, not you..)")
- SetLocationConnections("room", "", "coridoor", "", "")
- AddToLocations("coridoor", "a coridoor", "a dimly lit passage with a stained carpet")
- SetLocationConnections("coridoor", "portal", "lift", "", "room")
- AddToLocations("lift", "a lift", "a dangerous structure with a hand-operated sliding door")
- SetLocationConnections("lift", "", "", "", "coridoor")
- AddToLocations("portal", "a magic portal", "Freedom at last!")
- SetLocationConnections("portal", "", "", "", "") #note no exits: end game!
- Shared.SetCurrentLocation("room")
- # End of code you can alter
- #******************************************************************************************
- #check if Locations are all correctly spelled and listed by the programmer:
- key_errors = Shared.CheckLocations()
- if len(key_errors) > 0:
- print("Errors found in Location settings: (SetLocations())")
- print(key_errors)
- print("Correct spelling or add / remove Locations above")
- print("And try again")
- ret_value = False
- return ret_value
- def AddLines(numLines):
- print ("\n" * numLines)
- def AddToLocations(key_name, displayName, description):
- Shared.dictLocations.update({key_name: Location(key_name, displayName, description)})
- def CreateMainMenu():
- lstMenu = []
- lstInstructions = []
- if Shared.currentLocation.LocationToNorth != "":
- lstMenu.append("Go north to " + Shared.GetNextLocationName("n"))
- lstInstructions.append("n")
- if Shared.currentLocation.LocationToEast != "":
- lstMenu.append("Go east to " + Shared.GetNextLocationName("e"))
- lstInstructions.append("e")
- if Shared.currentLocation.LocationToSouth != "":
- lstMenu.append("Go south to " + Shared.GetNextLocationName("s"))
- lstInstructions.append("s")
- if Shared.currentLocation.LocationToWest != "":
- lstMenu.append("Go west to " + Shared.GetNextLocationName("w"))
- lstInstructions.append("w")
- lstMenu.append("Quit")
- lstInstructions.append("quit")
- return lstMenu, lstInstructions
- def DoMenuChangeLocation(direction):
- AddLines(30)
- lstMenu = []
- lstInstructions = []
- # move location
- Shared.MoveLocation(direction)
- def PlayGame():
- '''
- Gameplay is a series of menus allowing the player to make a choice:
- n e s w add directions
- quit
- '''
- quit = Shared.currentLocation.Display()
- if not quit:
- lstDirections = ["n", "e", "s", "w"]
- lstMenu, lstInstructions = CreateMainMenu()
- choice = menu("\nMake your choice", lstMenu)
- if lstInstructions[choice] in lstDirections:
- DoMenuChangeLocation(lstInstructions[choice])
- elif lstInstructions[choice] == "quit": # quit
- quit = True
- return quit
- def SetLocationConnections(locationName, locationToNorth, locationToEast, locationToSouth, locationToWest):
- Shared.dictLocations[locationName].SetLocations(locationToNorth, locationToEast, locationToSouth, locationToWest)
- def main():
- if CreateLocations(): # false means errors in locations
- quit = False
- while not quit:
- quit = PlayGame()
- print("Thank you for playing")
- # Program runs from here:
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement