Climate Quest - Task 9
Objectives
- Understand the concept of 2D lists in Python and their usefulness in storing and managing more complex collections of data.
- Learn how to create, access, and modify 2D lists.
- Practice iterating over 2D lists using nested loops.
- Apply these concepts to enhance the Climate Quest game.
Incorporating 2D lists into the Climate Quest text adventure game can add complexity and depth to the game's data structures. For example, we can use 2D lists to manage multiple attributes for various game locations, challenges, or resources.
Changing the way in which the data is being held means we can also simplify the challenge functions by removing the functions for each location i.e. forest_challenge()
, city_challenge()
etc into just one function and use the location as a parameter to that function!
Warning
This challenge will involve a restructuring of the way the game data has been handled before. It may be best to start a new game file for your Python code
Here are some ways to incorporate 2D lists into the game:
- Game Map: Use a 2D list to represent a map of the game world, with different locations having various attributes (e.g., name, environmental health impact).
- Challenge Outcomes: Store possible outcomes of challenges in a 2D list where each row represents a different challenge and each column represents possible outcomes.
- Resource Management: Use a 2D list to keep track of resources, where each resource has multiple attributes (e.g., type, quantity, impact).
Instructions
-
Create a 2D List to Represent the Game Map:
-
Each sublist represents a location with attributes like name, description, and environmental health impact.
-
Create a 2D list where each inner list represents a location. The inner list will contain the following elements:
- Location Name: A string representing the name of the location.
- Opening Statement: A string that describes the scenario at the location.
- Options: A further list of possible actions the player can take.
Our top-level list is called
game_map
. Thegame_map
is a list of locations. -
-
Refactor the challenge functions
- Instead of having separate functions for each challenge, we can create a generic function that processes any location using the data from the game_map.
- The parameter to this function,
location_data
, will be one of the items in thegame_data
list. - Line 8: pulls out the first item in that list, the title of the location
- Line 9: pulls out the second item in that list, the location description
- Line 10: A new loop function
enumerate()
. As the title suggests this loop function also provides a number for each of the items in the list it is looping over and this is stored in thei
variable. Really useful! - Complete the code for this function by processing the choice and displaying the relevant outcome message to the screen from the
location_data
-
Update the main function
- When processing the user choice for the location we now only need to call the
display_challenge()
function and pass in the correct location data from thegame_map
list:
- Line 6: A more concise way of saying
if choice >= 1 and choice <= 5:
- When processing the user choice for the location we now only need to call the
-
Other opportunities
- This approach can be developed further by keeping a list of the resources gathered by the user in each location, where each resource (if/when) used can have an impact on the
environmental_health
score.
- This approach can be developed further by keeping a list of the resources gathered by the user in each location, where each resource (if/when) used can have an impact on the
Note
Notice how, by changing how our data is being stored, we can simplify the algorithm and code needed to process that data.