Lab 2: Turtle graphics Due: 11:59 PM on 2020-02-25

FAQ

Background

For this assignment, we will use the turtle graphics module to draw a picture. For your prelab, you should have designed your drawing and figured out the rough placement of the different components. See the prelab for the details of what should be included in your drawing. For this assignment you will write a single Python script with multiple functions to generate your picture. Here is an example completed picture:

Seascape

Guide

This section describes in detail how to develop the required functions triangle (for fish or spaceships), polygon (for rocks or planets), add_circles (for bubbles or stars), and generate_picture (for drawing the entire picture, using calls to your defined functions). Below the Guide section are the Specification and Creativity sections.

Style

Before you start coding, a few brief comments on style. We’ve talked about coding style in class and now we’re going to put those guidelines into practice. In particular, make sure you keep the following in mind as you’re writing your program:

Keep it DRY (Don’t Repeat Yourself). If you find yourself duplicating code (i.e. copying and pasting code), think “function”. Could you replace that duplicated code with a function? If you find yourself copying a function to make a small change, e.g. background color, use a parameter instead (i.e. make the background color a parameter to your function). For example:

def red_circle(radius):
    fillcolor("red")
    begin_fill()
    circle(radius)
    end_fill()

def blue_circle(radius):
    fillcolor("blue")
    begin_fill()
    circle(radius)
    end_fill()

would be more DRY-ly implemented as:

def colored_circle(radius, color):
    fillcolor(color)
    begin_fill()
    circle(radius)
    end_fill()

Moving from the Prelab to coding

You should use your prelab as a guide to help you as you start to put your picture together. However, feel free to deviate from your original design. The prelab was just a brainstorming session to get you started and to help with the basic layout. As you start to code up your picture, you may also notice that some of your x and y coordinates as well as sizes are not exactly right (either because of your measurements or because of a differences in screen size). You can use the setup function to set the size of the window and the screensize function to set or query the size of the drawing canvas.

Basic Shapes

To get started, we’re going to write some functions to generate basic shapes. Make sure that you have these working before moving on to the next part. We encourage you to refer to the documentation online as you work on this: https://docs.python.org/3.6/library/turtle.html. Don’t forget to include the import statement for the turtle module at the top of your file. For example, to bring all the turtle functions into your namespace:

from turtle import *

Triangle

Write a method named triangle that draws an equilateral triangle (i.e. a triangle with all three sides the same length). To start, you can define your function to take just three parameters: the x and y coordinate to draw the triangle and the length of the sides of the triangle. Once the basics are in place, you can add additional parameters such as color. The triangle should be drawn so that the left edge of the triangle is vertical. Recall that the interior angles of an equilateral triangle are all 60 degrees, which means the angle between a straight line drawn from one side and the adjacent side is 120 degrees. Your x and y coordinates may indicate any part of the triangle (e.g. the top left, the center, etc.).

Your triangle function should always draw a vertical left edge. To maintain this “invariant”, you could require that the caller of your function always ensure the turtle has the same heading. However, it is easy to make mistakes with such an approach (the caller could forget, or not know). A better approach is to make sure that the triangle function always works regardless of the current state of the turtle. You can do so by setting the turtle’s position and heading as part of triangle. In general, we want to avoid relying on the caller, if possible, for correct operation of a function.

Polygon

Write a method named polygon that draws a polygon. An n-sided polygon has n equal length edges and the angle between a straight line drawn from one side and the adjacent side is 360/n. To start you can define your function to take just 4 parameters:

Once the basics polygon is working, you can add additional parameters such as color. Again, the x and y coordinates may indicate any point on the polygon. Unlike the triangle where you know when you’re writing the function exactly how many sides the object will have, for the polygon you can’t hard-code the different line segments. Instead, you’ll have to use a for loop. For example, below are some polygons of differing number of sides and size. All can be drawn with the polygon function.

polygons

The Bubbles in the Water OR the Stars in the Sky

At this point, you should have two functions written that draw triangles and polygons anywhere on the screen. We’ll now work on enhancing the backdrop (water or sky) with randomly spaced circles (bubbles or stars). Write a function named add_circles that takes as a parameter the number of circles to add and randomly places circles throughout the screen. Some hints for how to do this:

To check that everything is working correctly, try adding differing numbers of circles and make sure that they’re distributed throughout the screen and that the right number are actually being drawn. Once you’re sure it’s working, extend your function to make the size of the circles random. You’ll have to experiment with different size ranges to see what looks best. For example, if you look at the picture at the beginning of the lab, you’ll see that the bubbles range in size. Use constants here rather than hard-coding numbers in your code. Again, you are welcome to add additional parameters.

Putting it all together

You now have all the components required to put together your final picture. Create a function named generate_picture that draws the entire picture. The generate_picture function should include all operations needed to go from a blank canvas to a completed drawing with no other manual intervention. Your generate_picture should call your triangle, polygon and add_circles functions. To complete your drawing you will likely need to change the background color as part of generate_picture and enhance your triangle, polygon and add_circles functions to draw shapes filled with appropriate colors (e.g. orange fish, brown rocks and white bubbles). Make sure to do the begin_fill and end_fill calls from within triangle, polygon, and add_circles, not from generate_picture.

Recall that you need 6 or more fish/ships and 6 or more rocks/planets and reasonable coverage of randomly sized and placed circles (e.g. 10 or more).

Specification

Name your file lab2_turtle.py. At a minimum your submission should have:

  1. A function named triangle that draws an equilateral triangle. The function must take at least 3 parameters, including the x and y coordinates at which to draw the triangle, and the length of the sides. The left edge of the triangle must be vertical.
  2. A function named polygon that draws a polygon. The function must take at least 4 parameters, including the x and y coordinates at which to draw the polygon, the number of sides, and the length of the each side.
  3. A function named add_circles that draws filled circles of random radius randomly throughout the screen. The function must take at least one parameter, the number of circles to draw.
  4. A function named generate_picture that has no arguments and draws your entire picture starting with a blank canvas.

Note: These above describes the minimum specifications for the triangle, polygon and add_circles functions. If you want to add additional parameters to those functions (or additional functions), that is OK and encouraged. generate_picture, however, must not take any parameters (it is used by the graders to test your program).

Recall from the prelab that you need 6+ fish/ships (triangles) and 6+ rocks/planets (polygons) and reasonable coverage of randomly sized and placed circles.

Note: Make sure you don’t have any files called turtle.py, or else Python will not be able to find any of the functions in the turtle module.

Creativity Suggestions

You can earn up to 3 creativity points on this assignment by adding additional elements to your picture. The amount of points awarded will be based on creativeness and difficulty of implementation. Here are some examples, but I encourage you to include your own:

To receive the creativity points you MUST include in your comments at the top of the program a listing of your additions (otherwise, it can be hard to figure out).

Note: If your picture gets very fancy, it is possible that you no longer employ the triangle, polygon or add_circles functions. Even if you don’t use them, leave them in your code since they will be graded!

When you’re done

Make sure that your program is properly commented:

In addition, make sure that you’ve used good coding style (including meaningful variable names, constants where relevant, vertical white space, etc.).

Submit both your program and a screenshot of your picture via gradescope. Your program program file must be named lab2_turtle.py and your screenshot must be named lab2_screenshot.png.

To take a screenshot of the drawing window:

Mac: Press command+shift+4. Then hit the spacebar (on a mac), doing so will change the crosshairs into a camera to screenshot a specific window. If you then click on the window where your picture is drawn, an image file will be saved on your desktop entitled “Screen shot…”. You can double-click on this file to make sure it worked.

PC: You can use the “Snipping Tool”. Save your image in png format on the desktop.

Make sure you submit both your code and your image at the same time. You can submit multiple times, with only the most recent submission (before the due date) graded. Note that the tests performed by Gradescope are limited, especially for this lab which is difficult to test automatically. Passing all of the visible tests does not guarantee that your submission correctly satisfies all of the requirements of the assignment.

Grading

Feature Points
triangle  
correct parameters, at least 3 2
equilateral 1
varying sizes 1
varying x, y 1
correct direction 1
filled with color 1
polygon  
correct parameters, at least 4 2
correct shape 1
varying sizes 1
varying x, y 1
filled with color 1
add_circles  
correct parameters, at least 1 2
distributed over entire area 1
random locations 1
random sizes 1
filled with color 1
generate_picture  
correctly defined with no parameters 1
background color 1
6+ triangles/fish/ships 1
6+ polygons/rocks/planets 1
calls add_circles 1
   
Comments, style 3
Creativity points 3
   
Total 30

FAQ Excerpts

Available colors in turtle

There are several ways to specify colors in turtle. The documentation for the pencolor function shows the different approaches:

  • You can use string names like "red". This link shows the available colors by name. The names are not case sensitive in Python, i.e., there is no need to capitalize the names as shown in the link.
  • You can use hex strings with the RGB color, e.g. "#ff0000" for red. This online color picker can can help you determine the hex string for a color.
  • You can specify the color as three numerical arguments with the RGB values. By default those numbers need to be floats in the range 0-1.

Working with setup and screensize

You don’t need to use the setup and screensize functions, and can instead design your drawing using the rough size of 700×700 pixels (i.e. coordinates from -350 to 350). However, if you want to explicitly control the size of the window and canvas you will likely want to use both setup and screensize.