@extends('template')
@section('title')
Lab 8: Graphics Using Nested For Loops
@stop
@section('head')
@stop
@section('content')
# [OPTIONAL] Lab 8: Making houses using Turtle and nested loops
{{--
This part of the lab has a video walkthrough. If you get stuck or want extra
context, you can watch the video below, or click the download link to download
it and watch it later. The full video description on YouTube includes a
breakdown of all of the topics in the video with links to each one.
[Download Video](https://sakai.wellesley.edu/access/content/group/77109fc3-0650-48b0-aeb4-22c7e2e94cba/Lab%209%3A%20Nested%20Loops%20%2B%20Sorting/lab9_buildings.mp4) | [Download Captions](https://sakai.wellesley.edu/access/content/group/77109fc3-0650-48b0-aeb4-22c7e2e94cba/Lab%209%3A%20Nested%20Loops%20%2B%20Sorting/lab9_buildings.sbv)
--}}
## A Series of Houses/Buildings
**NOTE: Download a copy of the `lab08` folder from the cs server
to complete this task.** Open the file `houses.py` in your `lab08` folder.
For this part of lab, you will practice writing nested for loops to draw buildings
with windows.
The ultimate goal is to create a series of buildings that might look like this
(note that colors, height and width of each building are randomly selected):
{{----}}
Let's start with just a single block that we will call a `window`. You are given
a function called `makeWindow` that creates this, given the size, outer color
and inner color. Here is a sample invocation:
```py
makeWindow(100, 'SlateGray', 'LightSkyBlue')
```
produces this drawing (on the right is an annotated drawing of the same window):
Note that the turtle starts and ends in the center of the window, facing East.
## Task 1. `makeBuilding(rows, columns, size, color1, color2)`
This function draws a building consisting of a grid of `rows` x `columns`
windows of `size`. Each window has frame color `color1` and inner color
`color2`.
Recall that you are given the `makeWindow` function above that draws a
window for you. Note also that `makeWindow` leaves the turtle in the same
location and direction as it was prior to the call to `makeWindow`.
This means that if you call `makeWindow`, you'll need to move your turtle (`leap` comes
in handy here) to a different position before drawing another window. Otherwise,
your windows will draw directly on top of each other and you will only see the topmost one.
Hint: you might consider drawing your buildings from the bottom to the
top (it makes it easier to align a row of buildings in the future). Below
are two invocations.
## Task 2. Write `makeRandomColorBuilding(rows, columns, size)`
This function draws a building consisting of a grid of rows x columns windows of size.
Each window has a randomly selected frame color color1 and inner color color2.
For now, ignore the roof (you may get to that later below).
**Hints**:
+ Note that the new version of `turtleBeads.py` includes three random
color functions: `randomMutedColor`, `randomPastelColor`, and
`randomVibrantColor`. The images in this lab use `randomMutedColor` for
the buildings, and `randomPastelColor` for the windows and roofs.
+ You might consider drawing your buildings from the bottom to the top
(it makes it easier to align a row of buildings in the future).
makeRandomColorBuilding(1, 8, 30)
makeRandomColorBuilding(4, 5, 60)
## Task 3. Write `makeRowBuilding(rows, columns, size)`
This function draws a building consisting of a grid of rows x columns windows of size.
Each **row** of windows has the same randomly selected frame and inner color.
As above, For now, ignore the roof (you may get to that later below).
**Hints**:
+ Think carefully about where to change the window colors to newly chosen random colors.
+ The turtle will always start and finish at the center of the lower left window, facing East
makeRowBuilding(6, 3, 50)
makeRowBuilding(2, 5, 50)
## Task 4. Write `makeColumnBuilding(rows, columns, size)`
This function draws a building consisting of a grid of rows x columns windows of size.
Each **column** of windows has the same randomly selected frame and inner color.
As above, For now, ignore the roof (you may get to that later below).
**Hints**:
+ Think carefully about where to change the window colors to newly chosen random colors.
+ The turtle will always start and finish at the center of the lower left window, facing East
makeColumnBuilding(6, 3, 50)
makeColumnBuilding(2, 5, 50)
## Task 5. Incorporate the roof
You are given a function called `drawRoof(windowSize, numCols, rcolor)`.
`drawRoof` draws a triangular roof. Assumes starting position is at the upper
left corner of the building, facing East. Note that `drawRoof` maintains a position and heading invariant.
Change your `makeBuilding` function so that it now draws roofs on each building,
as shown in the drawings below.
## Task 6: Make a city scape
Write a function called `cityScape` that takes an integer argument that
specifies how many buildings are to be drawn in your city scape. Each
time in your loop, you can invoke one of your functions above that draws
a building. The rows, columns and colors of each building can be randonly
generated (start with a fixed size to make sure it works). You might need
to make some adjustments to ensure that your buildings are neatly aligned
along the bottom most row, as shown in the examples below.
```py
cityScape(4)
```
```py
cityScape(4)
```
@include('/labs/lab08/_toc')
@stop