Build a Mini-Map

Objective: Build a Mini-Map

Let's begin by reviewing our objective.

Our goal is to create a Python script that demonstrates a mini-map: set up a canvas, define and use at least two functions for different landmark types, use a loop to draw multiple instances of one landmark type, and label at least one landmark using turtle.write().

Check the Specification

Next, we'll run pytest to see the failing tests. This confirms the engineering specification we need to meet.

Run the tests now and observe the output.

Test Results:

  • test_mini_map_requirements

Implement: `main.py`

Now, let's build the solution by following the TODO comments in the skeleton code for main.py. Each task references relevant documentation sections.

Step by step checklist:

  1. In the _draw_building_shape function, set the turtle's drawing color using the provided color parameter.
  2. Begin the filled shape for the building.
  3. Draw the rectangle shape for the building using the given width and height.
  4. End the filled shape for the building.
  5. In the _draw_tree_shape function, set the turtle's drawing color using the provided leaf color parameter.
  6. Begin the filled shape for the tree.
  7. Draw the circle shape for the tree using the given radius.
  8. End the filled shape for the tree.
  9. In the create_mini_map function, implement a loop to draw the specified number of trees.
  10. Inside the loop, calculate the position (x, y) for the current tree.
  11. Position the turtle at the calculated tree location using penup, goto, and pendown.
  12. Call the _draw_tree_shape function with the turtle, radius, and color to draw the tree.
  13. Position the turtle at the specified coordinates for the building's bottom-left corner and call the _draw_building_shape function with the turtle, dimensions, and color.
  14. Position the turtle for the label above the building, set the text color, and use the write method to display the text "Town Hall" with appropriate alignment and font.

The following documentation sections are going to be helpful:

  • Functions with Parameters
  • Looping with range()
  • Running Scripts
    1. Setting Up the Canvas (Screen)
    1. The Turtle (Pen)
    1. Movement and Drawing
    1. Filled Shapes
    1. Drawing with Functions
    1. Drawing Multiple Shapes with Loops
    1. Adding Text Labels

Validate the Solution

With the code in place, let's run the tests again to validate our work.

Run the tests now and observe the output.

Test Results:

  • test_mini_map_requirements All tests passed!

Debrief: Code Walkthrough

Great job! You've successfully implemented the mini-map.

Now, prepare for the final step: a code review with your mentor. Be ready to explain your code, especially how you used functions, loops, and the write() method to create the scene.

Documentation

# Python Fundamentals Refresher

## Functions with Parameters

Functions are blocks of reusable code. Parameters are like placeholders for values that a function needs to do its job.

*   **Defining a function:**
    ```python
    def greet(name: str, greeting: str):
        # Function code uses name and greeting
        print(f"{greeting}, {name}!")
    ```
*   **Calling a function:**
    ```python
    greet("Alice", "Hello")
    # Output: Hello, Alice!
    ```
*   **Formatting Strings (f-strings):**
    A convenient way to embed expressions inside string literals.
    *   `f"{variable}"`: Inserts the value of `variable`.
    *   `f"{float_variable:.2f}"`: Formats a floating-point number to two decimal places.

## Looping with `range()`

`for` loops are used to repeat a block of code multiple times. `range()` generates a sequence of numbers.

*   **Basic loop:**
    ```python
    for i in range(3):  # i will be 0, 1, 2
        print(i)
    # Output:
    # 0
    # 1
    # 2
    ```
*   **Using the loop variable for numbered lists:**
    `range(len(my_list))` iterates through indices of a list.
    ```python
    items = ["Apple", "Banana", "Cherry"]
    for i in range(len(items)):
        # i starts at 0, so add 1 for 1-based numbering
        print(f"{i + 1}. {items[i]}")
    # Output:
    # 1. Apple
    # 2. Banana
    # 3. Cherry
    ```

## Running Scripts

The `if __name__ == "__main__":` block:
Code inside this block only runs when the script is executed directly (e.g., `python your_script.py`), not when it's imported as a module into another script.

```python
def setup():
    print("Setting up...")

def run_program():
    print("Running program logic...")

if __name__ == "__main__":
    print("Script is being run directly.")
    setup()
    run_program()

Turtle Graphics

1. Setting Up the Canvas (Screen)

The turtle module draws on a window called the "screen".

  • Get the screen:
    import turtle
    screen = turtle.Screen()
    
  • Set screen size:
    screen.setup(width=600, height=400) # Set window dimensions
    
  • Set background color:
    screen.bgcolor("lightblue") # Use color names or hex codes
    
  • Control screen updates (for faster drawing): Turn off updates before complex drawing, turn on and update when finished.
    screen.tracer(0) # Turn off updates
    # ... drawing code ...
    screen.update()  # Show the drawing
    screen.tracer(1) # Turn updates back on (optional, often done implicitly by done())
    
  • Keep the window open:
    turtle.done() # Or screen.mainloop()
    

Example of a turtle screen with a background color

2. The Turtle (Pen)

The "turtle" is the cursor that draws on the screen. You can have multiple turtles.

  • Create a Turtle:
    artist = turtle.Turtle()
    
  • Hide the Turtle icon:
    artist.hideturtle()
    
  • Set Speed: Speed can be 1-10 (slowest to fastest) or 0 (no animation, instant drawing).
    artist.speed(0) # Fastest
    artist.speed(5) # Medium speed
    
  • Set Pen Color / Fill Color:
    artist.color("red")      # Sets both pen and fill color
    artist.pencolor("blue")  # Sets only pen color
    artist.fillcolor("green")# Sets only fill color
    
  • Set Pen Size (Thickness):
    artist.pensize(3) # Set pen thickness to 3 pixels
    
  • Pen Control:
    • Lift the pen (stop drawing when moving):
      artist.penup()
      
    • Put the pen down (start drawing when moving):
      artist.pendown()
      

Example showing a turtle icon and a line drawn by it

3. Movement and Drawing

Commands to move the turtle and draw lines or shapes. The screen's center is (0, 0).

  • Go to a Specific Position: Move the turtle to coordinates (x, y). Draws a line if the pen is down.
    artist.goto(100, 50) # Move to x=100, y=50
    
  • Move Forward/Backward: Move the turtle in its current direction.
    artist.forward(100) # Move 100 units forward
    artist.backward(50) # Move 50 units backward
    
  • Turning: Change the turtle's direction.
    artist.left(90)      # Turn left by 90 degrees
    artist.right(45)     # Turn right by 45 degrees
    artist.setheading(0) # Set direction to 0 degrees (East)
                         # 0: East, 90: North, 180: West, 270: South
    
  • Drawing Shapes (Examples):
    • Square:
      # Assuming pen is down and turtle is at a corner facing along an edge
      side = 50
      for _ in range(4):
          artist.forward(side)
          artist.left(90)
      
    • Circle: Draws a circle with the given radius. The turtle's current position is the bottom of the circle if facing East.
      radius = 30
      artist.circle(radius)
      

Example of a turtle drawing a square or triangle

4. Filled Shapes

Draw shapes that are filled with a solid color.

  • Set Fill Color: Use fillcolor() or the color() command (which sets both pen and fill).
    artist.fillcolor("blue")
    
  • Start and End Fill: Call begin_fill() before drawing the shape, and end_fill() after the shape is complete.
    artist.fillcolor("yellow")
    artist.begin_fill()
    # ... drawing commands for the shape (e.g., square, circle, polygon) ...
    artist.end_fill()
    

Example of a filled shape drawn by turtle

5. Drawing with Functions

Organize your drawing code into functions for reusability.

  • Defining a drawing function: A function can take parameters like position (x, y), size, or color to make it flexible. It often takes the turtle object itself as a parameter.
    def draw_dot_at(t, x, y, dot_color, dot_size):
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.dot(dot_size, dot_color) # Draws a filled circle
    
  • Calling a drawing function:
    my_turtle = turtle.Turtle()
    draw_dot_at(my_turtle, 100, 100, "purple", 20) # Draw a purple dot
    draw_dot_at(my_turtle, -50, 0, "orange", 10)  # Draw an orange dot
    

6. Drawing Multiple Shapes with Loops

Use loops to draw the same shape multiple times at different locations or with variations.

  • Looping through coordinates: Store positions in a list and iterate through the list.
    positions = [(100, 100), (-50, 0), (0, -150)]
    my_turtle = turtle.Turtle()
    my_turtle.speed(0) # Draw fast
    
    for pos_x, pos_y in positions:
        # Call a drawing function for each position
        draw_dot_at(my_turtle, pos_x, pos_y, "green", 15)
    
  • Calculating positions in a loop: Use the loop counter to calculate coordinates.
    my_turtle = turtle.Turtle()
    my_turtle.speed(0)
    
    for i in range(5): # Draw 5 circles
        x = -200 + i * 100 # Space circles 100 units apart horizontally
        y = 0
        my_turtle.penup()
        my_turtle.goto(x, y - 20) # Position for circle bottom (radius 20)
        my_turtle.pendown()
        my_turtle.circle(20)
    

Example of a loop drawing multiple shapes

7. Adding Text Labels

Use the write() method to display text on the canvas.

  • Writing text: The text is written at the turtle's current position.
    artist.penup()
    artist.goto(0, 150) # Move to position for text
    artist.color("black") # Set text color
    artist.write("My Map Title", align="center", font=("Arial", 16, "bold"))
    
  • write() parameters:
    • arg: The string to write.
    • move (optional, boolean): If True, the turtle moves to the end of the text. Default is False.
    • align (optional, string): Alignment of the text relative to the turtle's position ("left", "center", or "right"). Default is "left".
    • font (optional, tuple): A tuple (name, size, style). name is a font family string (e.g., "Arial", "Courier", "Times New Roman"). size is an integer font height. style is a string ("normal", "bold", "italic").

Example of text written on a turtle canvas