Project 2: Turtle Graphics
The purpose of this project is to give you practice in designing and writing an algorithm. In this case, you'll be designing an algorithm to draw a representation of a building of arbitrary size and location using turtle graphics. The end result of the assignment will be an image of a cityscape.
For this assignment you will be create a function that draws a building of a certain size and then use that function to draw several different scenes. In addition, you can create functions that draw other pieces of a cityscape. Please put your building function and any other shape functions you create in a single file called cityshapes.py. Each scene you create should have its own python file with the names noted below for the required images.
- Create a new file in BBEdit, or whatever editor you wish to use. Call the file cityshapes.py. All of the functions you create for drawing shapes should go into this file.
Download the turtleUtils.py module and
put it in the same directory as the rest of your files for this
project. You will want to use the import statement:
from turtleUtils import *
at the top of each program file you create.
The turtleUtils module contains two functions. The first is called turtleSetup(). This function can take four arguments, which are the width and height of the window and the location of the upper left corner relative to the deskop. If you don't pass it any arguments, it creates a window that is 400x400 near the upper left corner of the screen.
The second function is called turtleWait(). If you call this function, your turtle window will stick around, but you can't do anything else to it, so don't call this function until you are done drawing. When you want the window to go away, select the close option from the menu.
Note that you are free to take a look at the turtleUtils.py file to see how it works. Looking at code is an extremely common way to learn how a programming language works or how to solve certain problems, just as reading books is a good way to learn history.
Note that you can look at the source html for this web page if you select View Source from the View menu in Safari, or any other web browser. It's a handy way to see how to do various things.
- Sketch (pencil and paper, possibly graph paper) the outline of a building and some features on it (e.g. windows, doors, decorations). Start simple (i.e. 1-2 features). The idea is to build a model of what you want to draw before you start writing code.
- Call the length of one side of the building dx and try to describe the length of all of the outer edges of the building as some multiplier times dx. Repeat the exercise for all of the features within the building.
- Pick a corner of the building as the starting location for the turtle. The starting orientation for the turtle should be pointing horizontally to the right.
- In your cityshapes.py file, begin a function called building that takes one argument called dx, which should correspond to the dx you defined above. Then write the series of turtle operations required to draw the outside edge of the building. Don't forget to save your work often.
In a terminal, go to your working directory. Make sure you can see
the cityshapes.py file using the ls command. Begin
the python interpreter, then import the cityshapes modules.
from cityshapes import *
Call the reset() function to bring up a turtle window and then call your building() function with a reasonable parameter value for dx. Make sure it does the right thing. The process of writing a little code, testing it, and then writing more is a good way of avoiding mistakes. Get in the habit of following this practice.
- Finish coding up your building. Make sure you keep everything parameterized relative to dx so that you can scale the size of the building by simply changing the parameter to the building() function.
- To make the first required image, create a new file called scene1.py. This python file should import turtle, turtleUtils, and cityshapes. It should contain code that draws two buildings next to one another in the center of the image by using the building() function you created in cityshapes. One building should be twice the size of the other. Have your code call the turtleWait() function when you are done drawing.
- To make the second required image, create a new file called scene2.py. This file should draw a collection of buildings arranged artfully (as best you can do) of different sizes and locations. Make at least 5 buildings in this scene, but no more than 20. Have your code call the turtleWait() function when you are done drawing.
- Capture images of your scenes by using the screen capture function on the mac. If you hold down command-shift-4 then the mouse will turn into a crosshair. You can then select a portion of the screen by clicking and dragging. Whatever box you select is saved as an image to the Desktop. The first image you capture will be called Picture 1.png. You can then rename the image and include it in a web page.
As noted in the syllabus, to earn some flavor of A on a project you need to extend the project in some way. Starting with this assignment, the required work is considered about 85% of the total project, which gives you some scale for the extensions (not large). Each project description will include some examples of extensions, but you are also free to pursue your own interests, so long as they stay within the spirit of the project. The more creativity your work demonstrates, the better.
Below are some suggested extensions. Focus your extensions on exercising your ability to automate the process of creating a cityscape by creating additional functions or adding parameters to existing functions.
- Create a function or functions for other building types and mix them up.
- Create a function or functions for other objects that might be found in a cityscape.
- Add more parameters to your building function so that you can vary other aspects of the building besides its scale.
Create a web page for your writeup. Your writeup should include the following.
- A brief description of the task (in your own words).
- A description of how you parameterized the building function. Which aspect of the building controlled its scale, and where did you put the origin (where the turtle started drawing the building)?
- The two required pictures.
- A description of and pictures for any extensions you did.
When your writeup is ready for viewing and linked to your index page, send me an email.
To hand in your python code, place it on the Academics fileserver. Do not post it on the web. You may include snippets from your code in your writeup to demonstrate how certain aspects of the function work, but please do not include the whole thing.
To place the code on the fileserver, go to the server fileserver1 and select the Academics volume. Within that there ought to be a CS151 folder, and within that there should be a folder with your username. You can read and write files to the directory with your username and I can read them, but no one else can access them. Put your python files (at least three) in that directory to hand them in.