The goal of this project is to incorporate loops and conditionals into your code as well as provide more practice in encapsulating code and concepts for later re-use and manipulation.



Create a new file called better_shapelib.py. Copy the goto and block functions from your project2/shapelib.py file into better_shapelib.py. Add a parameter called fill to the block() function.

Better blocks


First, re-write the block function to take advantage of looping, if you have not already done so.


Second, edit the block function so that if the fill variable has the value True, then the block should be filled. If the fill variable has the value False, then the block should not be filled. You can use an if statement before and after the drawing commands for your block to control whether the turtle's fill function gets called.


You may also want to add an optional parameter for color to your block function. You can specify colors for the turtle using one of two methods: as a string, or as an rgb-tuple.

The strings you can use to specify colors are given here.

An rgb-tuple is simply three values in the range [0.0, 1.0] as a comma-separated list surrounded by parentheses. For example:

(0.15, 0.6, 0.2)

makes a nice artificial grass green. You can use an rgb-tuple or a string when calling the turtle.color function. The following two calls create identical colors.

turtle.color("Forest Green")
turtle.color( (0.13, 0.55, 0.13) )


Don't forget to appropriately comment your functions. In each function's docstring, state what shape it draws, and where and how big it will be (e.g. if the scale is 1.0, then this function will draw a house with its lower left corner at (x,y) and will be 200 pixels high and 150 pixels wide).

Better shapes

Improved basic shapes

For at least 2 more of your basic shape functions from Project 2, copy them to your better_shapelib.py file, edit them to use loops wherever it makes sense, and give them a parameter (e.g. fill) that controls whether the shape is filled (possibly partly filled) or not. As with the block function, you may also want to add a parameter for color.

Improved aggregate shapes

For at least 2 of your aggregate shapes from Project 2, copy them to your better_shapelib.py file, rewrite them using the new functions with the fill parameter, and take advantage of looping wherever possible. The goal is to make your code as efficient as possible in terms of the number of lines of code and the simplicity of that code.

Probabilistic shapes

If you wish, use conditional statements to enable variations on the complex shapes. For example, you can make any function call dependent upon a random number using the following type of test. In the example below, the block will be drawn 70% of the time.

    if random.random() < 0.7:
        block( x, y, w, h, True )

Better scenes

Pick one of your scenes from Project 2 (or create a brand new scene, if you wish). Copy it to your better_shapelib.py file and re-write it so the entire scene is parameterized by an (x, y) location and a scale parameter. In other words, you should be able to have your scene draw anywhere on the screen at any size.

If you have a scene that looks like the following:

def myscene():
    block(5, 10, 50, 100)
    triangle( 5, 100, 50 )
    goto( 30, 30 )
    turtle.forward( 10 )
    turtle.left( 90 ) 
    turtle.circle( 20 )

You can easily convert it to a scalable, moveable scene using the following rules. First, change the function to have parameters x, y, and scale.

Angles do not change. Following the above rules, the myscene function would be the following.

def myscene(x, y, scale):
    block(x + 5*scale, y + 10*scale, 50*scale, 100*scale)
    triangle( x + 5*scale, y + 100*scale, 50*scale )
    goto( x + 30*scale, y + 30*scale )
    turtle.forward( 10*scale )
    turtle.left( 90 ) 
    turtle.circle( 20*scale )

Better scene demo

Create a task5.py file that imports better_shapelib.py and uses the scene function to draw several versions of the scene of different sizes and in different locations. Note that you can assign a nickname to any package you import. For example, the following imports better_shapelib, but assigns the module the name bsl instead of better_shapelib.

import better_shapelib.py as bsl

That means you can type bsl.myshape(x, y, scale) instead of better_shapelib.myshape(x, y, scale).

The first required picture is an image with three differently sized and positioned versions of your first scene.

Nested scenes

Create a task2.py file that creates a second scene that incorporates your first scene at least once. For example, you could make your scene appear as a window or painting within the new scene. Make sure the new scene is also created inside a function.

In keeping with the space theme, the new scene could be the Smithsonian Air and Space Museum, or it could be something like a cockpit window looking out of a spaceship. Be creative.

The second required picture is an image with your first scene located at least once inside a second scene.

User-controlled scenes

Edit at least one of your two scenes so that some aspect of the scene (e.g. the size or number of an element, or the color of an object) depends on a new parameter to the scene function. It does not have to be fancy.

Set up your overall program so that the value of the parameter comes from a command line argument. Create two images showing how the command line argument affects the appearance of your scene. For example, the command-line argument might determine whether a particular object is red or blue.

The third and fourth required images should be examples of one of your scenes drawn using two different values for the command line argument.


Each assignment will have a set of suggested extensions. The required tasks and writeup constitute about 83% of the assignment. If you do only the required tasks and writeup -- and to them well -- you will earn a B. To earn a higher grade, you need to undertake one or more extensions. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. One complex extension, done well, or 2-3 simple extensions are typical.

These are only examples to help you start thinking of the unlimited possible ways you could extend the project. You are strongly encouraged to design your own extensions to suit your interests and show off your computational thinking skills.

Whichever extensions you choose, be sure to discuss your motivation, design process, implementation, and results in the writeup. A screenshot of your results is usually a great idea.

Writeup and Hand-in

Turn in your code by putting it into your private handin directory on the Courses server. All files should be organized in a folder titled project3. Include only those files necessary to run the program. We will grade all files and only the files in the project3 directory, so please do not turn in old, non-working, versions of your code.

Make a new wiki page for your assignment. Put the label cs151s18project3 in the label field on the bottom of the page, and give the page a meaningful title (e.g. Caitrin's Project 3).

In general, your intended audience for your write-up is your peers not in the class. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it. You can assume they have read your prior project reports. Follow the outline below.

© 2018 Caitrin Eaton.