CS 151: Project 3

Title image Project 3
Fall 2019

Project 3: Scenes within Scenes

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.


  1. Set up your workspace

    Make a new project3 directory. Open a terminal and cd to the directory.

  2. Make a better block function

    Create a new file called better_shapelib.py. Copy the goto and block functions from your project2/shapelib.py file into better_shapelib.py.

    Modify your block function so that it has a fill parameter. Then change your function so if fill is true, the block is drawn filled.

    + (more detail)

    Add a parameter called fill to the block function definition. It should come after x, y, width, and height in the parameter list.

    To create a filled shape, call turtle.begin_fill() immediately before drawing the shape, draw the shape, and then call turtle.end_fill() immediately after completing the shape. Make sure to start filling after the call to goto that puts the turtle in the proper starting location.

    If the value of fill is True, then turn on filling just before the block is drawn and turn off filling just after the block is drawn. You can use an if-statement before and and an if-statement after the drawing commands for your block to implement this.

    If you find yourself writing code like

    if fill == True:
       # do something

    think carefully about whether you need to compare the value of fill to True. An if statement test needs to generate a boolean value. If fill is already a boolean, do you need to compare it to True?

    Optimize your block function so it uses loops, if it does not already do so.

    You may also want to add an optional parameter for color to your block function.

    + (more detail)

    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 each of your functions. In each function's doc string, state what shape it draws, and where and how big it will be (e.g. if the scale is 1, 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).

  3. Improve two more shape functions

    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.

  4. Update two 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 (and possibly color) 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.

    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 )
  5. Update a scene to be parameterized

    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.

    + (more detail)

    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 convert it to a scalable, moveable scene using the following rules. First, change the scene function to have parameters x, y, and scale.

    • For calls to a function that takes (x, y, size) or (x, y, width, height):

      block( a, b, c, d ) becomes block( x+a*scale, y+b*scale, c*scale, d*scale )

    • For calls to a goto function (if any):

      goto( a, b ) becomes goto( x + a*scale, y + b*scale )

    • For calls to the turtle forward or circle functions:

      turtle.forward( a ) becomes turtle.forward( a * 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 )
  6. Create an image with multiple copies of a scene

    Create a task1.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 menmonic 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.

  7. Create a second scene incorporating your first

    Create a task2.py file that creates a second scene that incorporates your first scene at least once. Stick with our museum theme, broadly defined. Be creative.

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

  8. Incorporate variation in a scene

    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. The variation 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.

Follow-up Questions

  1. What is a command-line argument, and why are they useful?
  2. What does an if-statement do?
  3. What is the difference between = and == in Python?
  4. Within a function, how would you control the number of times a for loop executes using a function parameter? Give an example.


Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own. Be sure to describe any extensions you complete in your report. Include pictures.

Submit your code

Turn in your code (all files ending with .py) by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, a directory with your user name. Within this directory is a directory named private. Files that you put into that private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, create a new directory, such as project1, and then copy your code into the project directory for that week. Please submit only code that you want to be graded.

When submitting your code, double check the following.

  1. Is your name at the top of each code file?
  2. Does every function have a comment or docstring specifying what it does?
  3. Is your handin project directory inside your Private folder on Courses?

Write Your Project Report

If you haven't already made a new page for this report on the wiki, then make one now (Log into the wiki, goto your Personal space by selecting "Personal Space" on the menu under the Person icon, then make the page using the "Create" button. Put the label cs151f19project3 in the label field on the bottom of the page. But give the page a meaningful title.

Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. 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.

Your project report should contain the following elements.