For this assignment, we will be using 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 be working on a single Python program that generates your picture. Here is an example (minimal) completed picture:
There are four required functions you must implement according to the specifications given below, whether or not you use all of them in your drawing. This section describes in detail how to develop the required functions: triangle
(could be used for minimal 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.
Before you start coding, a few brief comments on style. 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()
It is generally OK to add additional parameters to the required functions as long as they have the minimum specified set of parameters.
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). In your generate_picture
function, 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 (see the FAQ for more detail).
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.7/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 *
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 at least 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”, set the turtle’s position and heading as part of triangle
. This is safer than assuming the turtle is already facing in the correct direction; in general, avoid relying on the caller, if possible, for correct operation of a function.
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 at least 4 parameters:
x
and y
location to draw the polygon,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.
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 (e.g., for bubbles or stars). Write a function named add_circles
that as a starting point 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:
circle
function draws a circle with a given radiusrandint
function from the random module may be useful. To make this
function available import it at the top of your program, e.g. from random import randint
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 named constants here rather than hard-coding numbers in your code. If you find it useful, you are welcome to add additional parameters.
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. If you want to use the setup
and screensize
functions, call them from your generate_picture
function before drawing any shapes. 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). 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).
Name your file lab2_turtle.py. At a minimum your submission should have:
triangle
that draws an equilateral triangle filled with color. The function must have at least 3 parameters, 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.polygon
that draws a polygon filled with color. The function must have at least 4 parameters, the x
and y
coordinates at which to draw the polygon, the number of sides, and the length of the each side.add_circles
that draws filled circles of random radius randomly throughout the screen. The function must have at least one parameter, the number of circles to draw.generate_picture
that has no parameters 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 the prelab that you need 6+ fish/ships (triangles) and 6+ rocks/planets (polygons) and reasonable coverage of randomly sized and placed circles.
Note: Gradescope requires that your program be named lab2_turtle.py. If you
name it turtle.py, Python will not be able to find any of the functions in the
turtle
(for reasons we will learn about later in the semester).
The amount of creativity points awarded will be based on creativeness and difficulty of implementation. Here are some examples, but I encourage you to include your own:
star
that draws stars (not
asterisks) using a for
loopTo 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! Follow the specifications carefully! Make sure you have all of the required functions (with the minimum required parameters). It is OK (and encouraged) to go beyond the specification but make sure when you do so, you still satisfy the specifications in the lab. In particular, make sure you include correct definitions of the triangle
, polygon
and add_circles
functions, even if you don’t use them in your drawing.
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 at the same time (that is submit both files as once). Your 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.
Features | Points | |
---|---|---|
triangle |
5 | |
polygon |
5 | |
add_circles |
5 | |
generate_picture |
5 | |
Code design and style | 5 | |
Creativity points | 2 | |
Total | 27 |
Make sure that your functions work regardless of context. For example, be sure to set the heading of the turtle explicitly in the triangle
function. Recall we always want the left edge to be vertical. Don’t rely on the state of the turtle always being maintained as vertical outside the function. If the invariant is not maintained then your triangle
function will not do the “right thing”. Similarly, don’t assume that the pen will be up at the beginning of a drawing function. In general we want to make sure our functions work regardless of what has happened before. And so we should set the position and orientation inside the function to be exactly what we need without relying on any assumptions.
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
.
All of our functions should have Docstrings. Recall from class that Docstrings are special comments contained within triple quotes at the very beginning of a function that describe what the function does, its parameters and its return value (if any). We want to use standard format shown in class with a one sentence description of the function and explicit sections for the parameters and return value. For example:
There are several ways to specify colors in turtle. The
documentation
for the pencolor
function shows the different approaches:
"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."#ff0000"
for red. This
online color
picker can
can help you determine the hex string for a color.