@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): {{----}} A side view of four buildings, each of which has a symmetric triangular roof, and is is a simple rectangle below that roof. The rectangles are divided horizontally into rows, and each row contains a number of squares (windows) across the width of the building. The buildings are touching each other at the sides, but have different heights and widths, and their rows/columns are different sizes. The first building has 3 columns of windows and 6 rows (storeys). The second has two columns and 3 rows, and each row/column is slightly smaller. The third has 6 columns but only 2 rows, with much smaller dimensions. The fourth and final building has only 2 columns but has 7 rows, with windows about the same size as the second building. The roofs of each building, and each floor within the building each have random colors, and the windows in each row also have a shared random color. 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):
A SlateGray 100px by 100px square, with a LightSkyBlue 50px by 50px square centered inside of it. The turtle is shown at the center of both squares and is facing east. The same image with annotations showing the width of the outer square and the names of the two colors.
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.
makeBuilding(2, 3, 50, 'SlateGray', 'LightSkyBlue') makeBuilding(5, 2, 50, 'SlateGray', 'LightSkyBlue')
Two rows of 3 windows (copies of the picture shown above for makeWindow) stacked to make a 2×3 building. A 2-column 5-row tall and skinny building.
## 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)
A row of 8 windows, with a large symmetrical triangle on top that serves as the roof (instructions for adding rooves are given later). This time, each window has random colors for both the inner and outer square. Another randomly-colored building, this time with 5 columns and 4 rows.
## 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)
A 3-column, 6-row building, using random colors except now the colors are consistent within each row, instead of being different for each window of the building. Another building with randomly-colored rows, this time with two rows and 5 columns.
## 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)
A 3-column 6-row building with random colors, but now the colors are consistent within each column, instead of being consistent across each row. Another column-striped building, with 5 rows and 2 columns.
## 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.
makeBuilding(2, 3, 50, 'SlateGray', 'LightSkyBlue') makeBuilding(5, 2, 50, 'SlateGray', 'LightSkyBlue')
A 3-column 2-row building with a triangular roof, without random colors (the winodws and roof are LightSkyBlue while the walls are SlateGray). Another building with a roof, this time with 2 columns and 5 rows.
## 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) ``` Four buildings of different sizes, with random colors for the walls and windows in each row. The buildings are 2 rows by 4 columns, 7 rows by 5 columns, 4 rows by 3 columns, and 7 rows by 6 columns. Each also has different-sized window cells, so their rows and columns are different heights/widths. ```py cityScape(4) ``` Another cityscape with 4 buildings that have: 6 rows and 3 columns, 3 rows and 2 columns, 2 rows and 6 columns, and 7 rows and 2 columns. These buildings also have randomized colors for each row, and randomized sizes. @include('/labs/lab08/_toc') @stop