Project 2: Turtle Drawing

In the beginning of the semester we drew interesting shapes on code.org. In this project, we will use python’s turtle module to draw shapes both iteratively and recursively. In this project, we will use loops, functions, conditions, and variables to draw pictures with the python turtle package.

You must work with a partner (unless granted permission in advance); make sure both students are clearly identified in the comments at the top of each program.

An important skill as a computer scientist is the ability to learn how to use an existing API library by reading documentation, finding good API usage examples on the internet, and adapting those examples to meet your needs. In this project, you will begin building these skills to help you make the transition from classroom to practice.

To complete the assignment, you will need:

If you get stuck, please e-mail me a zip file of your project, a screenshot, and a description of:

  1. What you expect to happen
  2. What is actually happening

The assignment is broken down into checkpoints. The final submission must include the zip file of all the code, but the prior check points can simply be screenshots.

Instructions

Part 1: Learn how to work with turtles

Note: This part should be submitted individually!

1. Draw a line

Create a python program where you can play with turtles. Draw a line on the screen using the python turtle package. I learned how to make turtle programs by using these examples.

A basic turtle program looks like the following:

import turtle
turtle.shape("turtle")  # optional
turtle.speed(0)         # optional

turtle.forward(90)
turtle.left(90)
turtle.forward(90)

turtle.Screen().exitonclick()

The first & last lines should be in every turtle program you write. DO NOT NAME YOUR PROGRAM turtle.py!

2. Draw a square

  1. Draw a square on the screen.

  2. Change its color.

  3. Make the line thicker by changing the pensize or width.

  4. Change the speed the turtle draws.

  5. Make a square function that takes the length of each side as a parameter & draws a square using the turtle. Call this function instead of the lines you used to draw the square in step 1 above.

3. Draw two squares

It’s possible to move the turtle without drawing. Draw two squares on the screen that don’t touch. For example:

Now you’re ready to tackle the rest of the assignment. To speed up testing, set the turtle speed to be 0.

Submitting Part 1

Once you’re satisfied that your program is working correctly, take a screen shot of your two squares code and the two squares display and submit it to Canvas.

Part 2: Functions

Note: This part should be submitted as a team!

None of the following turtle programs should take user input!

shapes.py

Create a program shapes.py that implements the following functions. You may find this webpage to be helpful.

  1. rectangle(width, height): takes the length of the width and height as parameters.
  2. square(side): takes the length of a side as a parameter. Implement this by calling the rectangle function.
  3. triangle(side): takes the length of a side as a parameter and draws an equilateral triangle.
  4. hexagon(side): takes the length of a side as a parameter and draws an hexagon.

Test that your functions work by clearly drawing a square, rectangle, triangle, and decagon on the screen. They shouldn’t overlap too much. For example:

Every submitted picture should look unique.

Remember to declare all your functions at the top of the file, before you call them.

polygon.py

Next we’ll generalize our polygon drawing by writing a program polygon.py with the following function:

polygon(num_sides, length): takes the number of sides (num_sides) and the length of each side (length) as parameters. If the number of sides is less than 3, draw nothing.

Hint: To determine how far to turn your turtle, you will need to divide 360 (the number of degrees in a circle) by the number of sides.

Test that your function works by using a loop to draw a number of shapes in an interesting pattern. Here are some examples:

Every submitted picture should look unique.

star_polygon.py

We can use a similar technique to draw stars with an arbitrary number of points, called regular star polygons. Write a program star_polygon.py with the following function:

star_polygon(num_pts, num_skip, side_length): takes the number of points (num_pts), the number of points to skip to calculate the next angle (num_skip) and the length of each line in the star (side_length) as parameters.

Hint: You can calculate the angle to turn your turtle by multiplying 360 by the number of points to skip, divided by the total number of points.

Here are some examples:

Every submitted picture should look unique.

spiral.py

Write a program spiral.py with the following function:

spiral(length, angle): takes the length of the spiral (length) and the angle of the spiral (angle) as parameters.

You must use either iteration (loops) or recursion (function that calls itself). Make sure the number of times your loop executes depends on the length parameter, and not a fixed number of times.

Every submitted picture should look unique.

Test your functions by drawing two different spirals.

Grading

You will be graded on the following:

Submitting

When complete, your project should include the following four python programs:

Zip your project using the same steps as for the earlier project, giving the zip file the same name as your project2_uLogin folder name, and submit to canvas.