• 2016
• 2015
• 2014
• 2013
• December
• November
• October
• September
• February
• 2012
• 2011

# Blog 2013 February 18 CS151 Project 2

In our second project, A Shape Collection, we created complicated scenes involving functions that created different objects from a few functions of simple shapes. The purpose of this project was to practice creating more complicated functions with multiple parameters.

In our second lab, we were asked to create a few functions that were copied into a python file, shapes.py. One of the functions, goto(x,y), made the cursor move to a specified location of coordinates x,y without drawing. This function is central to all of the functions that draw shapes. Another function, block() when called creates a block at coordinates x,y with variables width and height. A third function, bunchofblocks() called the block function three times, drawing a pile of three blocks at coordinates x,y and at a variable scale (with 1 being the natural size.)

I then created a new python file, main.py, which would call the functions in shapes.py. To call these functions we wrote a statement, "import shapes" in the main.py file that would allow us to import functions from this file by creating a secondary data table. I prepended each of these functions with "shapes." to let python know that it is using the data table from that file.

In part 2, in the shapes.py file I created two more functions. The first function when called draws an octagon. I wrote it so that the cursor would draw forward with a variable length and then turn 45 degrees a total of eight times. The second function when called draws a triangle with a variable length on its bottom side, with angles of 80, 80 and 20 degrees.

Picture 1

Picture 1 shows the result of running the functions bunchofblocks() octagon() and triangle() respectively from left to right.

For step 3 in the shapes.py file, I then created two more functions which draw a tree and a building at a variable x,y coordinate and a variable scale when called. For both of these functions, a scale of 1 would draw the shapes in their natural size. The tree() function draws a triangle function over a block function that looks like a stump. The building() function calls a series of 6 block() functions to create the outline of the building, windows and a door, as well as an octagon() function that serves as the door handle.

Picture 2

Picture 2 shows the result of running the tree and building functions at their natural size (scale = 1.)

Part 4 of the assignment required us to create an outdoor scene by calling the functions that we made in steps 2 and 3. For this scene, I included four tree() functions, two building() functions and an octagon() function in a new function, town1() all at coordinates that were placed in the parameters of each function. Different values for the "scale" parameters were used.

Picture 3

Picture 3 shows the result of running the town1() function: a drawing of a familiar town.

In part 5 of the assignment, I created a function that draws a scene of the inside of a restaurant with booths. For this function I had to create a few more functions in the shapes.py file. I created rightchair() (a right facing chair), leftchair() and table() functions which draw a series of rectangles with the block() function. A light() function used the block() and octagon() functions. When called, it would draw a japanese style light. A divider() function was used to separate the tables. Another function, diningset(), incorporated the functions that draw a table, a right facing chair, a left facing chair, a divider and a light when called. This function used a scale parameter where 1 is the natural size. In the main.py function I called for the diningset() function four times to create an image of four booths in a restaurant.

Picture 4

Picture 4 shows the result of running the town2() function.

I combined two extensions by making a new function town3(), which replicated the town2() function and added random colors and widths to each object. To make the function randomly color the objects, I imported the random functions by writing the command "import random" in the beginning of the shapes.py file. I called for random colors by generating a random integer from 1 to 7 by writing the command "random.randint(1,7)." I then used the "if" statement to call for a color that was assigned to an integer from 1 to 7.

Code Snapshot

This snapshot shows the commands that I used to generate an integer that corresponds to a color for each object.

I used another "if" statement to determine the thickness of the pen. If the random integer from 1 to 7 were greater than 3, then the pen would be 5 (times the "scale" parameter) pixels thick. I used the "else" command to set the width of the pen to a length of 1 pixel (times the "scale" parameter) if the random integer was less than or equal to 3.

Picture 5

Picture 5 shows the extension that involves using random colors and widths.

By doing this project, I became comfortable with using more complicated functions with multiple parameters. Also, I gained a familiarity with new commands involving random numbers and conditional statements.

Labels