Advertisement
Trippy-Chords

world.py(v1.1)

Dec 7th, 2019
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.65 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 actions
  5.  
  6. level = {} # The dictionary to hold the map/coordinates and tile objects
  7.  
  8. starting_position = (0,0)
  9.  
  10. class MapTile:
  11.     # This is a base class and is what allof your maptiles will inherit from
  12.    
  13.     def __init__(self, name, x, y, intro, description):
  14.         # This is an initializer function, it basically tells the class what information will be stored
  15.         # The arguments for this function come from the child classes (Classes that inherit from this one) in a thing called super()
  16.         # We do things like 'self.name = name' so the class knows that anytime it sees self.name, it is to refer to what was assigned to the name argument
  17.         self.name = name
  18.         self.x = x
  19.         self.y = y
  20.         self.intro = intro
  21.         self.description = description
  22.  
  23.     def intro_text(self):
  24.         # This returns the intro you have assigned to your tile (example being, if you have a forest tile with an intro of "This is a forest" this will return "This is a forest")
  25.         return self.intro
  26.    
  27. #################################################################
  28. ############This was modified###################################
  29. ################################################################
  30.     def adjacent_moves(self):
  31.         # This uses our previously made (part 1) function that checks if a tile exists and if it does, it returns the tile object
  32.         # This assigns the name of each tile at the given coordinates to its respective direction.
  33.        
  34.         #  If the tile to a given direction is not None, then assign that tile to that direction
  35.         if tile_exists(self.x, self.y - 1) != None:
  36.             north = tile_exists(self.x, self.y - 1).name
  37.         # If it is None, assign None to the direction
  38.         # Repeat for each direction
  39.         else:
  40.             north = None
  41.         if tile_exists(self.x , self.y + 1) != None:
  42.             south = tile_exists(self.x , self.y + 1).name
  43.         else:
  44.             south = None
  45.         if tile_exists(self.x + 1, self.y) != None:
  46.             east = tile_exists(self.x + 1, self.y).name
  47.         else:
  48.             east = None
  49.         if tile_exists(self.x - 1, self.y) != None:
  50.             west = tile_exists(self.x - 1, self.y).name
  51.         else:
  52.             west = None
  53.         # Returns all move actions for adjacent tiles
  54.         moves = []
  55.  
  56.         # If a given direction is not None, assign the corresponding action to the direction.
  57.         # Repeat for each direction
  58.         # Note: We have to instantiate the class with () to have access to its variables (like keyword and methof)
  59.         if north != None:
  60.             moves.append(actions.MoveNorth())
  61.  
  62.         if south != None:
  63.             moves.append(actions.MoveSouth())
  64.        
  65.         if east != None:
  66.             moves.append(actions.MoveEast())
  67.        
  68.         if west != None:
  69.             moves.append(actions.MoveWest())      
  70.         return moves # Return the moves list for use in game
  71. #################################################################
  72. ############End modifications###################################
  73. ################################################################
  74.     def available_actions(self, **kwargs):
  75.         # This appends all available actions to the moves list for in game use
  76.             # kwargs is for optional parameters (so it is not required to pass arguments when calling this function)
  77.             # example for using kwargs available_actions(tile="A tile")
  78.             # kwargs is a dictionary, the key in the kwargs dictionary is the keyword (tile is the key word) and the value is what is assigned to that keyword ("A tile" is the value)
  79.         if kwargs:
  80.             # Make an empty list to add moves to
  81.             moves = []
  82.             # loop through the kwargs dictionary where the keys are the keywords and values are what is assigned to them (example above)
  83.             for key, value in kwargs.items():
  84.                
  85.                 #This will be used later in the game development and I'll have a video on this soon
  86.                 # If in_battle was passed as a keyword argument then execute this code
  87.                 if key == "in_battle":
  88.                     if value == True:
  89.                         # If the value assigned to in_battle is True, exxecute this code
  90.                         player = kwargs['player']
  91.                         enemy = kwargs['enemy']
  92.                         # Make battle actions
  93.                         pass
  94.                 # If in_town was passed as a keyword argument
  95.                 if key == "in_town":
  96.                     if value == True:
  97.                         # If the value assigned to in_town is True, execute this code
  98.                         print("In town, from available actions")
  99.                         # make town actions
  100.                         pass
  101.         else:
  102.             # If we did not pass any keyword arguments then append the return of adjacent_moves() to the move list and return it
  103.             moves = self.adjacent_moves()
  104.             # append basic actions
  105.             return moves
  106.  
  107. class SpawnPoint(MapTile):
  108.     # This is a child class of MapTile and as such has access to everything inside of MapTile and basically extends the MapTile template
  109.     def __init__(self, x, y):
  110.         self.x = x # x cooridnate
  111.         self.y = y # y coordinate
  112.         self.name = "Spawn"
  113.         self.intro = "A spawn"
  114.         self.description = "Spawn place"
  115.         # Super allows us to pass the above information into the init of MapTile
  116.         super().__init__(
  117.             name = self.name,
  118.             x = self.x,
  119.             y = self.y,
  120.             intro = self.intro,
  121.             description = self.description
  122.         )
  123.    
  124.     def modify_player(self, player):
  125.         # We will use this later for making elemental effects or just messing with the player
  126.         pass
  127.  
  128. class ForestTile(MapTile):
  129.     # You can literally copy and paste one of these classes and just change the name, description and intro accordingly to make other tiles quickly
  130.     def __init__(self, x, y):
  131.         self.x = x
  132.         self.y = y
  133.         self.name = "Forest"
  134.         self.intro = "You walk through An overgrown, seemingly empty mystical forest"
  135.         self.description = "An overgrown, seemingly empty mystical forest"
  136.         super().__init__(
  137.             name=self.name,
  138.             intro=self.intro,
  139.             description=self.description,
  140.             x=self.x,
  141.             y=self.y
  142.         )
  143.     def modify_player(self, player):
  144.         pass
  145.  
  146. class DenseForestTile(MapTile):
  147.     def __init__(self, x, y):
  148.         self.x = x
  149.         self.y = y
  150.         self.name = "Dense Forest"
  151.         self.intro = "You walk through An overgrown, very dark and dense forest."
  152.         self.description = "An overgrown, dense and dark segment of forest"
  153.         super().__init__(
  154.             name=self.name,
  155.             intro=self.intro,
  156.             description=self.description,
  157.             x=self.x,
  158.             y=self.y
  159.         )
  160.     def modify_player(self, player):
  161.         pass
  162.  
  163. class VillageTile(MapTile):
  164.     # I will be explaining how to add individual towns in a later video when we get to player movement
  165.     def __init__(self, x, y):
  166.         self.x = x
  167.         self.y = y
  168.         self.name = "Village"
  169.         self.intro = "You come upon a village named..." # we will be making different village names in a later video
  170.         self.description = "A village that is teaming with life"
  171.         super().__init__(
  172.             name=self.name,
  173.             intro=self.intro,
  174.             description=self.description,
  175.             x=self.x,
  176.             y=self.y
  177.         )
  178.     def modify_player(self, player):
  179.         pass
  180.  
  181. def load_tiles():
  182.     """Parses a file that describes the world space into the _world object"""
  183.    
  184.     # 'with' means to automatically close the file we are working with when the program is done with it
  185.     # open the file in read mode (which is r) as f so we can refer to the file as f
  186.     with open('map.txt', 'r') as f:
  187.         rows = f.readlines() # Read the lines of the file
  188.    
  189.     x_max = len(rows[0].split('\t')) # Get the length of the first row split at the tabs (this is why your map must be symmetrical) --
  190.     #-- using split() makes a list, the elements in the list are the tiles in your map. each is one element and adds one to the length
  191.  
  192.     # loop as many times, as rows... so if we have 5 rows, we loop 5 times
  193.     for y in range(len(rows)):
  194.        
  195.         cols = rows[y].split('\t') # the columns, this takes the row at the y index (first loop is 0, then 1, etc) and splits it at the tabs--
  196.         # -- this makes a list
  197.  
  198.         try:
  199.             # We use a try and except in case we run into an error
  200.             for x in range(x_max): # loop the number of times that x_max is (inthis case x_max is 5 because that is how many elements are in--
  201.                 # -- the list produced by split() )
  202.  
  203.                 # if the element we are at is a new line character (\n) then we replace it with empty quotes
  204.                 tile_name = cols[x].replace('\n', '') # Windows users may need to replace '\r\n'
  205.                
  206.                 # if the tile_name is a pair of empty quotes, set the key value pair to the current coordinates to a tuple of the coordinates paired to None
  207.                 #  (the iteration we are on; so if this is the first loop of y but the second loop of x the coordinates are (0, 1) )
  208.                 if tile_name == '':
  209.                     level[(x, y)] = None
  210.                
  211.                 # if the tile name is not empty quites, then we check what the current tile name is
  212.                 else:
  213.                     if tile_name == 'SpawnTile':
  214.                         # assign the SpawnPoint class as a value to the coordinate tuple (the key) in the level dictionary
  215.                         level[(x,y)] = SpawnPoint(x, y)
  216.                         global starting_position # pull in the starting_position for reassignment
  217.                         starting_position = (x, y) # assign spawn point coordinates to the starting position
  218.                    
  219.                         # Repeat the process for each tile type.
  220.                     if tile_name == 'DenseForestTile':
  221.                         level[(x,y)] = DenseForestTile(x, y)
  222.                    
  223.                     if tile_name == 'ForestTile':
  224.                         level[(x,y)] = ForestTile(x, y)
  225.                    
  226.                     if tile_name == "VillageTile":
  227.                         level[(x,y)] = VillageTile(x,y)
  228.        
  229.         # Catch any errors
  230.         except Exception as e:
  231.             if e == IndexError:
  232.                 print("Index does not exist") # This will very likely happen but it shouldn't cause problems
  233.                 continue # If there is an error, keep looping
  234.             else:
  235.                 print(e) # Print the error if it's not an index error
  236.  
  237.         #print(level) # Finally, to make sure the tiles have been loaded, print the map (the level dictionary)
  238.  
  239. # Make a function to see if a certain tile exists
  240. def tile_exists(x,y):
  241.     # .get() is a built in function that ny dictionary has access to, if the thing we're trying to get does not exist --
  242.     # --it returns False; if it does, it returns the value
  243.     return level.get((x,y))
  244.  
  245. #load_tiles() # run the load_tiles() function
  246.  
  247. # Get spawn location
  248. for key in level.keys():
  249.     if level[key] != None:
  250.         if level[key].name == "Spawn":
  251.             print(key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement