Project 2: A Shape Collection
The goal of this assignment is to give you practice in creating and using functions and function parameters in Python. You'll continue to create shapes using turtle graphics, just like we did in lab.
The end result of the assignment should be a representation of a scene depicting something related to a museum, real or imaginary. You have wide latitude in your choice of subject, and the scene can be as simple or complex as you like. What is important is to properly structure your code and include good comments.
Remember that you need to write the code yourself. You can discuss the assignment and ideas regarding the assignment with others in natural language, but not Python. If you have questions about your Python code (other than very simple syntax questions), please ask one of the lab assistants or professors. Please see the syllabus for guidelines on collaboration and attribution.
- Set up your shape library code
Make a copy of your lab2.py file and rename it shapelib.py (for "Shape Library").
Delete the any code not inside a function from the file so that only the import statements and the functions remain. You should not have any top level code (code not inside a function) in the shapelib.py file. It is meant to form a library of shape-drawing functions that other code files will be able to import and use.
Your first task is to create a main.py file that imports your shapelib.py functions. To do this, create a file called main.py in a text editor, and at the top (after your name, date, and description) put the following.
The import statement lets you call any functions in your shapelib.py file, such as block, using the syntax shapelib.block. You can rename shapelib as something more concise, if you wish in the import statement.
In your main.py file, make a function called museum1. Put a single call to the museum1 function at the end of the file, followed by an input call.
Write some python code in your museum1 function that calls some of the functions from your shapelib.py file to draw a simple image (like a few blocks). Note that to call a function from your shapelib file you have to prepend shapelib. in front of the function name. For example shapelib.block( 10, 10, 20, 30 ) would be how you would call the block function.
You can run your main.py file using:
You don't need to save this image, as you'll be replacing the code in the museum1 function later on.
- Write functions to draw basic shapes
In your shapelib.py file, make 2 more functions for drawing basic shapes like the block function from lab. Basic shapes should take in at least an x location, y location, and size information. For example, you could make a triangle function or a hexagon function with the size value being the length of a side, in pixels. Put a print statement at the beginning of each function and test them out by calling the functions in your main.py function.
Each of your shapes should draw properly no matter where it is drawn on the screen, what the size parameter is, or what the orientation of the turtle happens to be when the shape function executes. Test this out for all of your functions.
In your report, include a picture demonstrating that your shape functions work properly. This is required picture 1.
When you have a picture or scene you want to save, make a screen capture of it on a mac using the key combination Command-Shift-4. The picture will automatically be saved to a file on your Desktop. You can then move and rename it to a more appropriate location. Make sure the entire scene is visible in the image.
- Write functions that make more complex things
In the shapelib.py file, make 2 functions that draw objects/things that relate to museums (e.g. columns, vases on pedestal, framed artwork, tables, windows, archways, items on exhibit (dinosaurs, airships, etc.), statues, etc.). You can pick small objects, or big things. The objects should be built from several of the simpler shapes (triangles, blocks) from your library.
When you define these aggregate shape functions, they should all take at least three parameters. The first two (x0, y0) will define the starting location for the shape and the third should define the scale of the shape. A scale factor of 1.0 should draw the shape in its natural size. Feel free to add additional parameters as you like to control other aspects of the shape. As with the basic shapes, your shapes should draw properly at any scale, location, or turtle orientation.
For a little more detail on making aggregate shapes, see the geometric thinking page.
In both of of your new functions, use one or more of the basic shape functions to draw the shape. For example, a house shape might incorporate the block and triangle functions. Test out your new functions before proceeding.
In your report, include a picture demonstrating that each of your aggregate shape functions work properly. That means drawing it in several locations at several scales. This is required picture 2.
- Design and create a scene
Now it is time to write the real code for your museum1 function. Using the functions you created in steps 2 and 3, make a scene with something related to a museum. In your scene, make use of the fact that your functions can draw the shapes with different sizes in different locations. You can also use the random package and for loops to make more complex scenes, such as windows, collections of framed paintings, or to exhibit items at randomized locations in the room.
Save the image and include it in your writeup. This is required picture 3.
- Design and create a second scene
Make a second, different scene in a function museum2 in your main.py file.
Save the image and include it in your writeup. This is required picture 4.
- Does breaking a scene into pieces make it easier to create?
- What is a for loop and why are they useful?
- What is the difference between using "from turtle import *" and "import turtle"? Explain from both a practical use point of view and how they differentially affect the global symbol table.
- What is your favorite museum?
Projects are your opportunity to learn by doing. They are also an opportunity to explore and try out new things. Rather than list out every activity for a project, the defined part of each project will be about 85% of the assignment. You are free to stop there and hand in your work. If you do all of the required tasks and do them well, you will earn a B/B+.
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.
- Make additional basic or complex shape functions that make use of your other shape functions.
- Make additional scenes that show creative use of functions, loops, or randomness.
- Make use of the for loop control structure from the lab (you can also read Python documentation to teach yourself additional features).
Make use of the random package to make your scene more interesting. For example, if you import the random package into your program:
Then you can use it to generate random integers using the following expression, where a and b are the upper and lower bounds of the random numbers you want.
Just adding randomness is mildly interesting. Adding controlled/intentional randomness is much more interesting.
- Make use of other turtle properties such as line width, color, and fill capability to make your shapes more interesting.
- Add additional parameters to your shapes to control properties like line width and color.
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 CS152, 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.
- Is your name at the top of each code file?
- Does every function have a comment or docstring specifying what it does?
- 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
cs151f19project2 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.
A brief summary of the project, in your own words. This should be no more than a few sentences. Give the reader context and identify the key purpose of the assignment.
Writing an effective abstract is an important skill. Consider the following questions while writing it.
- Does it describe the CS concepts of the project (e.g. writing well-organized and efficient code)?
- Does it describe the specific project application?
- Does it describe your the solution or how it was developed (e.g. what code did you write/circuits did you build)?
- Does it describe the results or outputs (e.g. did your code work as expected)?
- Is it concise?
- Are all of the terms well-defined?
- Does it read logically and in the proper order?
- A description of your solution to the tasks, including images you created (including the required images mentioned above). This should be a description of the form and functionality of your final code. Note any unique computational solutions you developed or any insights you gained from your code's output. You may want to incorporate code snippets in your description to point out relevant features. Code snippets should be small segments of code--usually less than a whole function--that demonstrate a particular concept. If you find yourself including more than 5-10 lines of code, it's probably not a snippet.
- A description of any extensions you undertook, including text output or images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
- The answers to any follow-up questions (there will be 3-4 for each project).
- A brief description (1-3 sentences) of what you learned. Think about the answer to this question in terms of the stated purpose of the project. What are some specific things you had to learn or discover in order to complete the project?
- A list of people you worked with, including TAs and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
- Put the label cs151f19project2 on your wiki page.