Let's practice Python functions by building and exploring a fun 2D map. Each task is broken into easy steps, with hints and examples. You can use code from workshop_7 for help.
- Write a function to make a 2D grid (list of lists) with any number of rows and columns.
- Each cell should randomly be one of these areas:
'grass','pond','hill','tree'. - Use emojis for each area:
'grass'= 'π©','pond'= 'π¦','hill'= 'ποΈ','tree'= 'π²'. - HINT: Use the
randommodule andrandom.choice(). - Write another function to print the map nicely.
- Example for a 3x3 map:
π©π¦π²
ποΈπ©π©
π¦π²π©
- Try changing the size and see what happens!
- Make a dictionary:
emojis = {
1: 'π©', # grass
2: 'π¦', # pond
3: 'ποΈ', # hill
4: 'π²' # tree
}- Write a function to make a grid with numbers (1-4) instead of emojis.
- Let the user choose the width and height. Example:
generate_map(width, height). - Write a function to turn the number map into an emoji map and print it.
- Example:
game_map = [
[1, 2, 4],
[3, 1, 1],
[2, 4, 1]
]Prints:
π©π¦π²
ποΈπ©π©
π¦π²π©
- HINT: Use loops and the
emojisdictionary.
- Write a function to search the map for a specific number (e.g., 2 for pond).
- Return a list of coordinates (row, column) where you find it.
- Example: Searching for ponds (2) might return:
Found pond at: (0, 1), (2, 0) - HINT: Use nested loops and
enumerate().
- Make a grid for a farm, with empty cells as 'π«'.
- Let the user "plant" crops (e.g., 'π½' for corn) by entering row and column numbers.
- After each planting, show the updated farm.
- HINT: Use
input()and loops. - Example after planting:
π«π«π«π«
π«π½π½π«
π«π«π«π«
π«π«π½π«
- Try adding your own crop emoji!
- Add your own area or crop types and emojis.
- Try making a bigger map, or a map with obstacles.
- Share your favorite map with a friend!
- Use comments to explain your code.
- Test each function with different inputs.
- Ask for help if you get stuckβcoding is teamwork!