Advertisement
Trippy-Chords

play.py (v1.1)

Dec 7th, 2019
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.64 KB | None | 0 0
  1. # Visit https://www.youtube.com/user/thegrowers420 to see the tutorials maybe even subscribe! :D
  2. # Sorry for the last part, I made the account when I was 15 and forgot about that. If I get 100 subs, I can change it.
  3.  
  4. import character # Import character to access character types
  5. import time
  6. import world
  7. # Assign character objects to the name (a string)
  8. char_types = {
  9.     "mage": character.mage,
  10.     "thief": character.thief
  11. }
  12.  
  13. def char_select():
  14.     """
  15.    Allows the player to choose the type of character they would like to be
  16.    """
  17.     #Pull in the list 'char_types'
  18.     global char_types
  19.     print("Please choose a character type\n")
  20.    
  21.     #Loop through the dictionary 'char_types' to list the character types and stats assigned to them
  22.     for char in char_types:
  23.         print(char.title()) #print the name of the character type in title case (Which capitalizes the first letter of each word in a line)
  24.         # Just to make it look better, print a line of dashes
  25.         print("------------------------------------------------------")
  26.         # loop through the character type stats for printing
  27.         for stat in char_types[char].stats.keys():
  28.             #list the stats assigned to the character type
  29.             print(stat, ":", char_types[char].stats[stat])
  30.        
  31.         # Seperate character types and their stats with an empty line
  32.         print("\n")
  33.  
  34.     # Prompt the player to chose a character by entering its name
  35.     choice = input("Please choose a character by typing its name\n")
  36.  
  37.     # Loop through char_types to check which character the player chose
  38.     for char in char_types.keys():
  39.  
  40.         # If their choice is equal to the current key (Iteration 1 is "mage") then assign that character object to 'player'
  41.         if choice.lower() == char:
  42.            
  43.             # Assign the character object to the variable 'player'
  44.             player = char_types[char]
  45.            
  46.             # Allow them to give their character a name
  47.             name = input("Please give your character a name\n")
  48.            
  49.             # Confirm that they entered their desired name correctly
  50.             conf = input(f"You entered {name}, is this correct? y/n \n")
  51.            
  52.             # If they are satisfied with the name they entered the first time, assign the name and return the player object
  53.             if conf.lower() == 'y':
  54.                 player.name = name
  55.                 print(f"You are now {name} the {char}")
  56.                 return player            
  57.            
  58.             # If they did not enter the name correctly, use a while loop to ask until they are satisfied with their entry
  59.             while conf.lower() == 'n':
  60.                 name = input("Please give your character a name\n")
  61.                 conf = input(f"You entered {name}, is this correct? y/n \n")
  62.                
  63.                 #If they are satisfied with the name they gave, assign the name to the attribute 'name' of the character object (which is assigned to 'player' on line 40)
  64.                 if conf.lower() == 'y':
  65.                     player.name = name
  66.                     print(f"You are now {name} the {char}")
  67.  
  68.                     # Return the complete player object
  69.                     return player
  70.            
  71.  
  72.  
  73. # Assign the return of char_select() to the player variable
  74. # Note: When assigning a function call to a variable, we are actually assigning the return of the function to the variable
  75. player = character.mage # For faster testing, assign a character type to player
  76. player.name = "Something"
  77. # If there is no return from char_select() the player variable is set to None.
  78. # So we use a while loop to keep prompting them to select a character until char_select() has a return value
  79. while player == None:
  80.     # Tell them that they didn't enter a listed character type or they had a typo
  81.     print("!!!You did not enter a listed character name or had a typo, please try again!!!")
  82.    
  83.     # Give them 2 seconds to read the warning
  84.     time.sleep(2)
  85.    
  86.     # Assign the return of char_select() to the player variable (which will call the function)
  87.     player = char_select()
  88.  
  89.  
  90. ######################################################
  91. ##############New function############################
  92. ######################################################
  93. def play_game(player):
  94.     # Load the tiles (map)
  95.     world.load_tiles()
  96.  
  97.     #Start the player at the spawn location assigned to starting_position located in world.py
  98.     player.location_x, player.location_y = world.starting_position
  99.  
  100.     # Get the tile at the players location
  101.     tile = world.tile_exists(player.location_x, player.location_y)
  102.     # Tell the player they can check commands and may also quit
  103.     print("You may enter 'commands' at any time to see available commands; You may also enter 'q' to quit")
  104.  
  105.     # Start the game loop...Check if the player is alive and has not won yet
  106.     while player.is_alive() and not player.victory:
  107.         # Once again get the tile at the player location
  108.         # Note: We do this first in the loop so if the player moves, we get the tile before anything else
  109.         tile = world.tile_exists(player.location_x, player.location_y)
  110.  
  111.         # Attempt to modify the player (using the modify_player function assigned to the tile they're on)
  112.         try:
  113.             tile.modify_player(player)
  114.         # Catch an error. There more than likely won't be one, but just in case there is, catch it
  115.         except Exception as e:
  116.             print("Error!" e)
  117.  
  118. # Check if the player is still alive and has not won (This can change depending on what the tiles modify_player function does to the player)
  119.         if player.is_alive() and not player.victory:
  120.             # If the player is in a village, tell the 'available_actions' function to get specific actions (will be coded later)
  121.             if tile.name == "Village":
  122.                 available_actions = tile.available_actions(in_town=True)
  123.             # Otherwise just get the default actions(such as movement)
  124.             else:
  125.                 available_actions = tile.available_actions()
  126.  
  127.             # Loop through the available actions and print out the keywords that will let the player use the action
  128.             for action in available_actions:
  129.                 print(action.keyword)
  130.            
  131.             # Allow the player to enter a listed action keyword
  132.             action_input = input("Action: ")
  133.  
  134.             # If the player entered 'q' thank them for playing and quit the game
  135.             if action_input.lower() == 'q':
  136.                 print("Thanks for playing")
  137.                 quit()
  138.  
  139. # Call the play_game() function to start the game
  140. play_game(player)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement