Lab 7: Part 3. Making houses using Turtle and nested loops

A Series of Houses/Buildings

In this part, you will practice writing nested for loops to draw buildings with windows. Open the file houses.py in your lab07 folder.

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:

makeWindow(100, 'SlateGray', 'LightSkyBlue')

produces this drawing:

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.

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')

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:

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:

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:

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.

makeBuilding(2, 3, 50, 'SlateGray', 'LightSkyBlue') makeBuilding(5, 2, 50, 'SlateGray', 'LightSkyBlue')

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.

cityScape(4)
cityScape(4)

Table of Contents