If you have a text game where a player can move somehow, you need to pick a system for keeping track of location. Here are some ideas.
Location Variable (one dimensional motion)
A road would be an example of a one dimensional path. An Oregon Trail style game could use a location variable.
location = 5 move = int(input('Go north or south?\n')) if move == 'north': location += 1 elif move == 'south': location -= 1 print(f'Your new location is {location}.')
Your location variable keeps track of the player’s location. If you want limits to how many places the player can go, you will need some if/else logic to limit the location variable to a range, such as 0 to 10. You can also use max()
and min()
functions to limit the locations to a range of values.
If you want something to happen when the player reaches a certain location, you can add code to make that happen.
if location == 8: print('You have arrived at a town.')
Two Location Variables (two dimensional map grid)
px = 4 # player starting x coordinate
py = 8 # player starting y coordinate
# use code to change the player’s location
# for a rectangular world, limit the x and y min/max
# for world “wrap” you can use if/else or modulus (%) tricks
In a system like this, you might want to print a grid to show the player’s current location.
Use a nested for loop.
# y_size and x_size are the map dimensions for y in range(y_size): for x in range(x_size): if x==px and y==py: print('P', end="") #player location else: print('-', end="") print() #line break at end of each row
This system doesn’t store any information about the individual locations in the world. If you need to do that, you will need a data structure to store that data.
With a 1-Dimensional map, you can use a list that stores information about each space in the grid. If you only need a single character of information about the map, the simplest map could be a string:
# R = road, F = forest ploc = 0 #player location world_map = "RRFFFFRRRFF" if world_map[ploc] == 'R': print('You are on a road.') elif world_map[ploc] == 'F': print('You are in a forest.')
Here the program is indexing (looking up a value from) the list using the player location variable as the index number.
A two dimensional version of this map system can be a list of strings.
Using a list map: if you need more information about each space, you can use a list to store more data about each space in the map. The type of data stored in the list should reflect the info you need to store about your world. For example:
-
- Store strings or numbers in your list if you only need a small piece of info about each location.
- Store a dictionary for each location if you need to look up several pieces of information about each location.
- Store the name of a function for each space if you want to have different behaviors happening for different locations.
- Store lists inside the list if you need to store a list of similar things for each location, such as a list of items or a list of characters or enemies.
- Store objects if you need more power and flexibility. For example, if enemies have their own variables and behaviors, store an object.
- Use a more complicated structure if you need to. For example, each location could be a dictionary that contains a list of objects, each of which is an item or enemy that can be found at that location. Or it could be an object that contains its own lists or functions as needed.
If you use a more complicated data structure and you want to build out a lot of game content, you can probably save yourself some time by learning how to load that data from a file (such as JSON or CSV) instead of hard coding every bit of game data.
Non-Linear Game Maps
Some maps are not simple grids. Instead, they are webs of interconnected locations. Here are some ways to think about those types of game worlds:
- If you don’t have very many locations, use if/elif/else logic and nest the structures as needed. Only use this for a handful of locations, because the code quickly gets cumbersome.
- Each location is a function that gives you options to go to certain other locations. Use if/elif/else logic to return the function for the next location to go to.
- Make a dictionary of locations. This would be a dictionary that contains other dictionaries. In each individual location dictionary, include a list of other locations that can be reached from there. This creates a web of interconnected locations.