CS 151: Project #3

Project 3: Scenes within Scenes

Main course page

The purpose of this assignment 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.


For this lab you will be re-writing a number of functions from your shapes.py file from project 2. It's probably best to start with an empty file and copy over only those functions that don't have to change. The goto function is an example of a function you can copy and paste into the new file.

  1. Create a new file called things.py. Copy over the goto and block functions from your shapes.py file. Add a parameter called fill to the block function.

    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 to call the turtle's fill function.

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

    Don't forget to appropriately comment your functions. At the very least, put a sentence above each function describing what it does.

  2. For at least 2 more of your basic shape functions, copy them to your things.py file, edit them to use loops wherever it makes sense and give them a fill parameter that controls whether the shape is filled or not. As with the block function, you may also want to add a color argument.
  3. For at least 2 of your more complex shapes, copy them to your things.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.

    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 )
  4. Pick one of your Colby scenes from project 2 (or create a new scene, if you wish). Copy it to your things.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.

    • 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:

      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 )

    Create a task1.py file that imports things.py and uses the scene function to draw several versions of the scene. The first required picture is an image with three differently sized versions of your Colby scene.

  5. Create a task2.py file that creates a new image. The new image should incorporate your Colby scene at least once. For example, you could make your Colby scene appear as a window or painting within the new scene. You could also make the Colby scene appear like a picture in a brochure. Make sure the new scene is created inside a function. This image is the second required picture.

  6. Edit one of your two scenes so that some aspect of the scene depends on a parameter. Set up your 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. These are the third and fourth required images.



Make a new wiki page for your assignment. Put the label cs151S11proj3 on the page. Each of you needs to make your own writeup.

In addition to making the wiki page writeup, put the python files you wrote on the Academics server in your handin directory.

Colby Wiki

In general, your writeup should follow the outline below.