City Skyline¶
By the end of this lab you'll be able to:
- Write a
building()function with multiple parameters - Compose a scene from many function calls with different arguments
- Draw windows with nested loops inside a drawing function
Ten skyscrapers of varied heights and shades line up across the canvas, their yellow windows glowing against the dark silhouettes.
Welcome to the City Skyline!
In this lab you'll write one function that draws an entire building — walls and windows —
then call it 10 times to build a full city. Let's code it together!
How the Scene Works¶
The building(x, w, h, color) function draws one filled rectangle at position x,
with width w, height h, and fill color. Then it loops to add yellow window dots.
After defining the function once, we call it 10 times with different arguments — each call produces a completely different skyscraper.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
What Do You Think Will Happen?
The building() function is called 10 times, each with different h (height) values.
Which building will be the tallest — the first one drawn or the last?
Scan the data list and make your guess — then click Run to find out!
Try It Now¶
The tallest building has h=200 — that's the 4th entry at x=-115. Were you right?
How It Works¶
for side in [w, h, w, h] is a clever loop over a list of lengths: right side, top, left side, bottom — drawing the rectangle without hard-coding four separate forward calls.
building(*d) uses the unpacking operator * to spread a tuple (x, w, h, color) into four separate arguments, so you don't have to type building(d[0], d[1], d[2], d[3]).
Explanation Table¶
| Line | What it does |
|---|---|
def building(x, w, h, color) |
Function with four parameters: position, width, height, color |
for side in [w, h, w, h] |
Draw four sides in one loop using a list |
h // 35 |
Integer division: number of window rows that fit |
w // 20 |
Number of window columns that fit |
building(*d) |
Unpack tuple d into function arguments |
Learning Check¶
Your Turn — Add a Rooftop
The building() function below draws walls and windows but has no rooftop detail.
Add two lines after end_fill() to draw a thin red rectangle (height = 10) across the roof.
After end_fill() add: monty.penup(); monty.goto(x, -180+h) then draw a red filled w × 10 rectangle.
Experiments¶
-
Add a sky. Before calling any buildings, draw a large dark-blue filled rectangle covering the whole canvas background. You'll know it worked when the skyline sits against a night sky.
-
Add a moon. After the sky, add
monty.penup(); monty.goto(100, 100); monty.dot(60, 'lightyellow'). You'll know it worked when a glowing circle appears above the skyline. -
Change the window color. Replace
'yellow'with'white'. You'll know it worked when the windows look like fluorescent office lights. -
Make a wider city. Add 5 more buildings to the
datalist. You'll know it worked when the skyline extends further across the canvas.
Fantastic Work!
You composed an entire scene from a single reusable function!
That's exactly how game engines, CAD tools, and map renderers work under the hood.
Up next: Snowflake Arms — using a function to build six-fold symmetry.