Advertisement
Inksaver

SimpleAdventureProgram

Oct 8th, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. '''
  2. This file is used in a simple adventure game to teach the use of classes.
  3. To run this application you need 4 files:
  4. SimpleAdventureProgram.py:    https://pastebin.com/jm9aHBWR
  5.  (mimics C# Program.cs class with Main() function as entry point)
  6. SimpleAdventureLocation.py:    https://pastebin.com/xREn6WB9
  7.  (Class containing properties of exits to/from other locations, name and description)
  8. SimpleAdventureShared.py:    https://pastebin.com/9hRT5XA2
  9.  (Static class containing project-scope global variables and various methods)
  10. GetInput.py: https://pastebin.com/UceAJsaV
  11.  (Static class with functions to get user input and display menus)
  12. '''
  13. import time
  14. import random
  15. import SimpleAdventureShared as Shared
  16. from SimpleAdventureLocation import *
  17. from GetInput import *
  18.  
  19. '''
  20. module level variable needed is  dictionary of Locations.
  21. This will need 'global' declaration in any function that changes them!!!!
  22. (This is not required in C# and Java)
  23. To avoid the confusion, all global variables are stored in 'Shared.py' and prefixed with Shared.
  24. e.g. Shared.dictLocations = {} holds a dictionary of Location objects available to any file via 'import Shared'
  25. '''
  26.        
  27. def CreateLocations():
  28.     ret_value = True
  29.     #******************************************************************************************
  30.     # Start of code you can alter.
  31.     '''
  32.    AddToLocations("location name", "location display name", "location description")
  33.    SetLocationConnections("location name", "north exit", "east exit", "south exit", "west exit") # N E S W use empty string if no exit
  34.    '''
  35.    
  36.     AddToLocations("room", "a hotel room", "smelling of stale cigarettes\n(the room, not you..)")
  37.     SetLocationConnections("room", "", "coridoor", "", "")
  38.    
  39.     AddToLocations("coridoor", "a coridoor", "a dimly lit passage with a stained carpet")
  40.     SetLocationConnections("coridoor", "portal", "lift", "", "room")
  41.    
  42.     AddToLocations("lift", "a lift", "a dangerous structure with a hand-operated sliding door")
  43.     SetLocationConnections("lift", "", "",  "", "coridoor")
  44.    
  45.     AddToLocations("portal", "a magic portal", "Freedom at last!")
  46.     SetLocationConnections("portal", "", "", "", "") #note no exits: end game!
  47.  
  48.     Shared.SetCurrentLocation("room")  
  49.     # End of code you can alter
  50.     #******************************************************************************************
  51.     #check if Locations are all correctly spelled and listed by the programmer:
  52.     key_errors = Shared.CheckLocations()
  53.     if len(key_errors) > 0:
  54.         print("Errors found in Location settings: (SetLocations())")
  55.         print(key_errors)
  56.         print("Correct spelling or add / remove Locations above")
  57.         print("And try again")
  58.         ret_value = False
  59.    
  60.     return ret_value
  61.  
  62. def AddLines(numLines):
  63.     print ("\n" * numLines)
  64.    
  65. def AddToLocations(key_name, displayName, description):
  66.     Shared.dictLocations.update({key_name: Location(key_name, displayName, description)})
  67.  
  68. def CreateMainMenu():
  69.     lstMenu = []
  70.     lstInstructions = []
  71.  
  72.     if Shared.currentLocation.LocationToNorth != "":
  73.         lstMenu.append("Go north to " + Shared.GetNextLocationName("n"))
  74.         lstInstructions.append("n")
  75.     if Shared.currentLocation.LocationToEast != "":
  76.         lstMenu.append("Go east to " + Shared.GetNextLocationName("e"))
  77.         lstInstructions.append("e")
  78.     if Shared.currentLocation.LocationToSouth != "":
  79.         lstMenu.append("Go south to " + Shared.GetNextLocationName("s"))
  80.         lstInstructions.append("s")
  81.     if Shared.currentLocation.LocationToWest != "":
  82.         lstMenu.append("Go west to " + Shared.GetNextLocationName("w"))
  83.         lstInstructions.append("w")
  84.     lstMenu.append("Quit")
  85.     lstInstructions.append("quit")
  86.  
  87.     return lstMenu, lstInstructions
  88.  
  89. def DoMenuChangeLocation(direction):
  90.     AddLines(30)
  91.     lstMenu = []
  92.     lstInstructions = []   
  93.     # move location
  94.     Shared.MoveLocation(direction)  
  95.  
  96. def PlayGame():
  97.     '''
  98.    Gameplay is a series of menus allowing the player to make a choice:
  99.    n e s w   add directions
  100.    quit
  101.    '''
  102.  
  103.     quit = Shared.currentLocation.Display()
  104.     if not quit:
  105.         lstDirections = ["n", "e", "s", "w"]
  106.         lstMenu, lstInstructions = CreateMainMenu()
  107.         choice = menu("\nMake your choice", lstMenu)
  108.         if lstInstructions[choice] in lstDirections:
  109.             DoMenuChangeLocation(lstInstructions[choice])
  110.         elif lstInstructions[choice] == "quit":  # quit
  111.             quit = True
  112.  
  113.     return quit
  114.  
  115. def SetLocationConnections(locationName, locationToNorth, locationToEast, locationToSouth, locationToWest):
  116.     Shared.dictLocations[locationName].SetLocations(locationToNorth, locationToEast, locationToSouth, locationToWest)
  117.                    
  118. def main():
  119.     if CreateLocations(): # false means errors in locations
  120.         quit = False
  121.         while not quit:
  122.             quit = PlayGame()
  123.         print("Thank you for playing")
  124.  
  125. # Program runs from here:
  126. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement