Lab 2: Turtle graphics Due: 09:00:00 AM on 2019-07-20

Background

For this assignment, we will be using the turtle graphics module to draw a picture like shown below:

Seascape

You will have two options, a seascape with fish and rocks, or a space scene with spaceships and planets. For fish or spaceships, we will use a triangle function and for rocks or planets, we will use arbitrarily sized polygons.

Before you get started code, on a piece of paper (ideally graph paper) plan out the design for your drawing. The screen size will be about 700 x 700 (each dimension ranging from -350 to 350, with the origin (0,0) in the center of the window).

With these dimensions in mind, plan out on the paper where the different shapes will go, their sizes, etc. In particular:

As part of your planning, start thinking through how you will draw your shapes and how that approach influences your choice of coordinates, etc. For example, when you specify the coordinates of a triangle, which corner of the triangle do those coordinates specify?

You should plan on having at least six fish/ships and six rocks/planets (though if you’d like to have more, that’s fine too).

Now that you have a plan, you are ready to write a single Python script named lab2_turtle.py that generates your picture.

Specification

At a minimum your submission should have:

  1. A function named triangle that has three parameters, the x and y coordinates at which to draw the triangle and the length of the sides, and draws an equilateral triangle. The left edge of the triangle is vertical.
  2. A function named polygon that has 4 parameters, the x and y coordinates at which to draw the polygon, the number of sides, and the length of the each side, and draws a polygon.
  3. A function named add_circles that takes as a parameter the number of circles to draw and randomly draws filled circles of random radius throughout the screen.
  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 arguments (it is used by the graders to test your program).

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

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 will no longer need the triangle, polygon or add_circles functions. Even if you don’t use them, leave them in your code since they will be graded!

Guide

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 circle1(radius):
    fillcolor("red")
    begin_fill()
    circle(radius)
    end_fill()

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

would be more DRY-ly implemented as:

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

Design

You should use your desgin as a guide to help you as you start to put your picture together. However, feel free to deviate from your original design. The original design was intended 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. I encourage you to refer to the documentation online as you work on this: https://docs.python.org/3/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 *

Some functions that you might find useful are:

Triangle

Write a method named triangle that draws an equilateral triangle (i.e. a triangle with all three sides the same length). Your function should take three parameters: the x and y coordinate to draw the triangle and the length of the sides of the triangle. The triangle should be drawn so that the left edge of the triangle is vertical. For those rusty on geometry, 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. Your function should take 4 parameters:

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 Background

At this point, you should have two functions written that draw triangles and polygons anywhere on the screen. We’ll now work on filling in the background. The key component of the background is randomly spaced circles. Write a function named add_circles that takes as a parameter the number of circles to add and randomly places circles of radius 4 throughout the screen. Some hints for how to do this:

To check that everything is working right, 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, add a bit more code to make the size of the circles random. You’ll have to play with different size ranges to see what looks best. For example, if you look at the picture on the first page of this handout, you’ll see that the bubbles range in size. Use constants here rather than hard-coding numbers in your code.

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 fill with appropriate colors (e.g. orange fish, brown rocks and white bubbles).

Actually create your picture using your three functions. Change the fill color of the objects to make it look more realistic (e.g. orange fish and brown rocks).

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

If your drawing gets complicated, it may start to take a long time to render. To speed up the drawing process, add the following at the top of your program.

speed(0)

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 name 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 by the due date. 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  
equilateral 0.5
varying sizes 0.5
varying x, y 1
correct direction 1
polygon  
correct shape 3
varying sizes 1
varying x, y 1
add_circles  
number of circles by argument 1
covers entire area 1
random locations 2
random sizes 1
generate_picture  
background color 1
proper fills 1
6+ fish/ships 2
6+ rocks/planets 2
   
Comments, style 5
Creativity points 3
   
Total 27