Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Visit https://www.youtube.com/user/thegrowers420 to see the tutorials maybe even subscribe! :D
- # 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.
- import actions
- level = {} # The dictionary to hold the map/coordinates and tile objects
- starting_position = (0,0)
- class MapTile:
- # This is a base class and is what allof your maptiles will inherit from
- def __init__(self, name, x, y, intro, description):
- # This is an initializer function, it basically tells the class what information will be stored
- # The arguments for this function come from the child classes (Classes that inherit from this one) in a thing called super()
- # 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
- self.name = name
- self.x = x
- self.y = y
- self.intro = intro
- self.description = description
- def intro_text(self):
- # 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")
- return self.intro
- #################################################################
- ############This was modified###################################
- ################################################################
- def adjacent_moves(self):
- # This uses our previously made (part 1) function that checks if a tile exists and if it does, it returns the tile object
- # This assigns the name of each tile at the given coordinates to its respective direction.
- # If the tile to a given direction is not None, then assign that tile to that direction
- if tile_exists(self.x, self.y - 1) != None:
- north = tile_exists(self.x, self.y - 1).name
- # If it is None, assign None to the direction
- # Repeat for each direction
- else:
- north = None
- if tile_exists(self.x , self.y + 1) != None:
- south = tile_exists(self.x , self.y + 1).name
- else:
- south = None
- if tile_exists(self.x + 1, self.y) != None:
- east = tile_exists(self.x + 1, self.y).name
- else:
- east = None
- if tile_exists(self.x - 1, self.y) != None:
- west = tile_exists(self.x - 1, self.y).name
- else:
- west = None
- # Returns all move actions for adjacent tiles
- moves = []
- # If a given direction is not None, assign the corresponding action to the direction.
- # Repeat for each direction
- # Note: We have to instantiate the class with () to have access to its variables (like keyword and methof)
- if north != None:
- moves.append(actions.MoveNorth())
- if south != None:
- moves.append(actions.MoveSouth())
- if east != None:
- moves.append(actions.MoveEast())
- if west != None:
- moves.append(actions.MoveWest())
- return moves # Return the moves list for use in game
- #################################################################
- ############End modifications###################################
- ################################################################
- def available_actions(self, **kwargs):
- # This appends all available actions to the moves list for in game use
- # kwargs is for optional parameters (so it is not required to pass arguments when calling this function)
- # example for using kwargs available_actions(tile="A tile")
- # 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)
- if kwargs:
- # Make an empty list to add moves to
- moves = []
- # loop through the kwargs dictionary where the keys are the keywords and values are what is assigned to them (example above)
- for key, value in kwargs.items():
- #This will be used later in the game development and I'll have a video on this soon
- # If in_battle was passed as a keyword argument then execute this code
- if key == "in_battle":
- if value == True:
- # If the value assigned to in_battle is True, exxecute this code
- player = kwargs['player']
- enemy = kwargs['enemy']
- # Make battle actions
- pass
- # If in_town was passed as a keyword argument
- if key == "in_town":
- if value == True:
- # If the value assigned to in_town is True, execute this code
- print("In town, from available actions")
- # make town actions
- pass
- else:
- # If we did not pass any keyword arguments then append the return of adjacent_moves() to the move list and return it
- moves = self.adjacent_moves()
- # append basic actions
- return moves
- class SpawnPoint(MapTile):
- # This is a child class of MapTile and as such has access to everything inside of MapTile and basically extends the MapTile template
- def __init__(self, x, y):
- self.x = x # x cooridnate
- self.y = y # y coordinate
- self.name = "Spawn"
- self.intro = "A spawn"
- self.description = "Spawn place"
- # Super allows us to pass the above information into the init of MapTile
- super().__init__(
- name = self.name,
- x = self.x,
- y = self.y,
- intro = self.intro,
- description = self.description
- )
- def modify_player(self, player):
- # We will use this later for making elemental effects or just messing with the player
- pass
- class ForestTile(MapTile):
- # You can literally copy and paste one of these classes and just change the name, description and intro accordingly to make other tiles quickly
- def __init__(self, x, y):
- self.x = x
- self.y = y
- self.name = "Forest"
- self.intro = "You walk through An overgrown, seemingly empty mystical forest"
- self.description = "An overgrown, seemingly empty mystical forest"
- super().__init__(
- name=self.name,
- intro=self.intro,
- description=self.description,
- x=self.x,
- y=self.y
- )
- def modify_player(self, player):
- pass
- class DenseForestTile(MapTile):
- def __init__(self, x, y):
- self.x = x
- self.y = y
- self.name = "Dense Forest"
- self.intro = "You walk through An overgrown, very dark and dense forest."
- self.description = "An overgrown, dense and dark segment of forest"
- super().__init__(
- name=self.name,
- intro=self.intro,
- description=self.description,
- x=self.x,
- y=self.y
- )
- def modify_player(self, player):
- pass
- class VillageTile(MapTile):
- # I will be explaining how to add individual towns in a later video when we get to player movement
- def __init__(self, x, y):
- self.x = x
- self.y = y
- self.name = "Village"
- self.intro = "You come upon a village named..." # we will be making different village names in a later video
- self.description = "A village that is teaming with life"
- super().__init__(
- name=self.name,
- intro=self.intro,
- description=self.description,
- x=self.x,
- y=self.y
- )
- def modify_player(self, player):
- pass
- def load_tiles():
- """Parses a file that describes the world space into the _world object"""
- # 'with' means to automatically close the file we are working with when the program is done with it
- # open the file in read mode (which is r) as f so we can refer to the file as f
- with open('map.txt', 'r') as f:
- rows = f.readlines() # Read the lines of the file
- 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) --
- #-- 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
- # loop as many times, as rows... so if we have 5 rows, we loop 5 times
- for y in range(len(rows)):
- 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--
- # -- this makes a list
- try:
- # We use a try and except in case we run into an error
- 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--
- # -- the list produced by split() )
- # if the element we are at is a new line character (\n) then we replace it with empty quotes
- tile_name = cols[x].replace('\n', '') # Windows users may need to replace '\r\n'
- # 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
- # (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) )
- if tile_name == '':
- level[(x, y)] = None
- # if the tile name is not empty quites, then we check what the current tile name is
- else:
- if tile_name == 'SpawnTile':
- # assign the SpawnPoint class as a value to the coordinate tuple (the key) in the level dictionary
- level[(x,y)] = SpawnPoint(x, y)
- global starting_position # pull in the starting_position for reassignment
- starting_position = (x, y) # assign spawn point coordinates to the starting position
- # Repeat the process for each tile type.
- if tile_name == 'DenseForestTile':
- level[(x,y)] = DenseForestTile(x, y)
- if tile_name == 'ForestTile':
- level[(x,y)] = ForestTile(x, y)
- if tile_name == "VillageTile":
- level[(x,y)] = VillageTile(x,y)
- # Catch any errors
- except Exception as e:
- if e == IndexError:
- print("Index does not exist") # This will very likely happen but it shouldn't cause problems
- continue # If there is an error, keep looping
- else:
- print(e) # Print the error if it's not an index error
- #print(level) # Finally, to make sure the tiles have been loaded, print the map (the level dictionary)
- # Make a function to see if a certain tile exists
- def tile_exists(x,y):
- # .get() is a built in function that ny dictionary has access to, if the thing we're trying to get does not exist --
- # --it returns False; if it does, it returns the value
- return level.get((x,y))
- #load_tiles() # run the load_tiles() function
- # Get spawn location
- for key in level.keys():
- if level[key] != None:
- if level[key].name == "Spawn":
- print(key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement