Skip to content

Assignment 3: Drawing with Turtles

PrairieLearn Quiz Due: February 17 at 11:59pm (no grace day/ no late)
Assignment Due: February 19 at 11:59pm

Turtle Background (not required reading)

Programming has long been used to create or supplement artistic works, such as in exhibits in museums. Daily programming and software are used by visual designers, artists, and architects to create their works. Software has engaged a new generation of visual artists to consider programming as an essential part of their creative practice.

Logo is a computer programming language designed to teach programming to children. The designers of Logo intended for the language to allow novice programmers to get started quickly writing programs but also wanted the language to be powerful and extensive for more advanced users. In the last 40 years, Logo has also caused people to think differently about how to teach art, geometry, social science, and complex systems.

One of Logo’s key features is turtle graphics, which is an on-screen cursor that moves and draws on the screen to create graphics based on commands given by the user. (It is called “turtle” because the original implementation was an actual robot that looked like a turtle and carried a pen. The user could send commands to the robot to maneuver it across a piece of paper so that it would draw a specified design.) The basic commands to move and turn a "turtle" are so much a part of programming history that they have been incorporated into almost every modern programming language, Python included.

Assignment Specifications

Turtle Resources

Examples and documentation for using the Python Turtle graphics module are given in Chapter 4 of the course textbook. There are many more in the documentation for Python's Turtle graphics module. The tutorial linked here is also very helpful.

Concepts Needed for this Assignment

  1. Randomness
  2. Selection (if statements)
  3. For loops
  4. Turtle library

Learning Goals

  1. Understand how to use functions from modules provided by Python, like Turtle. (You already did this once when you used the random module in the Faces assignment.)
  2. Reading library function documentation.
  3. Designing and Testing programs by reading and studying other programs.
  4. Gain experience and understanding with loops, including loops with the Python range function.
  5. Gain more experience with writing and calling functions with and without parameters.
  6. Learning how to import a module you've written into another module you've written.

Tips Before Starting

  • Be attentive to the functions you must write as specified below. This includes the names of the functions and the requirements for what your program must do.

  • Please look carefully at the example code that is provided for you. Reading and understanding this code will help you complete the tasks in this assignment.

  • When debugging your code you'll likely want to set the turtle speed to zero, e.g. t.speed(0) so that your program runs faster. See the code you download for examples.

  • Document your code! For each function you write, you must have a docstring comment describing what that function does. This will be easier if you write comments as you write your functions rather than having to go back and remember the purpose of each function after finishing the assignment.

  • There is a PrairieLearn quiz for assignment 3 you must complete. Note it is due earlier and has no grace day!

  • Be creative and have fun!

  • Remember to fill out the reflect form after you have completed the assignment!

Assignment: Reading Quiz

On the assignment page, there is a reading quiz for this assignment. It is to help ensure you understand what this assignment is asking you to do and some of the key concepts in this assignment. You have unlimited tries and it will count towards your reading quiz grade. Note this quiz is due earlier than the assignment is due, and does not have a grace day and cannot be turned in late. The intent of these assignment reading quizzes are that you do them early to check your understanding of the assignment. You can take the quiz right after reading through this assignment and for assignment quizzes you can take them as many times as you want until they are due! You cannot extend the due date for assignment reading quizzes. So be sure to take them early!

Instructions for Completing the Assignment

Using the Provided Starter Modules

You'll write three modules as specified below.

  1. You are provided starter code for each module. The starter code also includes BoundingBox.py, which you will not edit. Here is the starter code.
  2. We provide additional Python modules to study and to use as models/examples. Here are the extra turtle programs.

Download the starter code zip file, unzip the file, then go into Pycharm and select File > Open… then select the unzipped folder. Choose either New Window or This Window.

Requirements

You are asked to create three Python modules as described below. You should implement them in the order shown, since the second two use the first module as part of their implementation. Remember: Each function you write should have a docstring comment describing what it does.

If you run any of the starter code modules that you imported into Pycharm, a black “bounding box” should be drawn inside the pop-up turtle graphics window. All images drawn in the modules described below must fit inside this bounding box, or they may not be graded. Generally, try to keep x-coordinates between -500 and 500, and y-coordinates between -250 and 250. Do not use win.window_width(), since it causes problems with the autograder.

Here is an overview of the three modules you will be writing:

  1. TurtleShapes.py

    1. Write an image-drawing function drawOneShape that draws a geometric shape and takes two parameters: a turtle and a size.
    2. Write another image-drawing function called drawOneDESCRIPTION (where DESCRIPTION should be replaced with words explaining what the function draws) that take two parameters: a turtle and a size.
      1. The image drawn by drawOneDESCRIPTION should use at least three different colors and two different geometric shapes.
    3. The main program block must contain one call to drawOneShape and one call to drawOneDESCRIPTION, in total drawing two images in different positions on the screen with different sizes.
  2. DrawRandom.py

    1. Write a function called drawEverywhere. This function takes two parameters: a turtle and the name of an image-drawing function (like drawOneShape or drawOneDESCRIPTION) .
      1. When drawEverywhere is called, the user should be prompted for how many shapes to draw. The image-drawing function should then be used to draw the specified number of images at random locations and with random sizes.
    2. The main program block must contain one call to input() to prompt the user for which image-drawing function to use and two possible calls to drawEverywhere where the second parameter is the name of the function specified by the user.
    3. The image-drawing functions should be imported and passed to drawEverywhere in the main block.
  3. DrawRow.py

    1. Write a function called drawRowsOfRows. This function takes two parameters: a turtle and the name of an image-drawing function (like drawOneShape or drawOneDESCRIPTION).
    2. The main program block must contain one call to drawRowsOfRows, resulting in ten rows of shapes being drawn with different y-coordinates. Shapes in the same row should be the same size, and different rows should have different sizes.
    3. The function drawOneDESCRIPTION should be imported and passed to drawRowsOfRows in the main block.

First Module: TurtleShapes.py

First, in the Python module named TurtleShapes.py, complete the function drawOneShape which, when called, draws a single geometric shape (circle, square, triangle, etc.). This function must have exactly two parameters: a turtle and a size. The function draws the shape using the specified turtle and with the specified size.

Then write (from scratch) another function whose name begins with drawOne and is descriptive of the image that the function draws, e.g., drawOneHouse, drawOneFlower, or drawOneDesign. This function must have exactly two parameters: a turtle and a size. The function draws a shape using the specified turtle and with the specified size. The drawing must include at least 3 different colors and at least 2 different geometric shapes. Do not call drawOneShape inside drawOneDESCRIPTION; these two functions should be independent from one another. Note that drawOneShape and drawOneDESCRIPTION must use the size parameter in the body of the function so that the size can be changed for various function calls in the next two modules.

The TurtleShapes.py module must have a main program block which contains one call to drawOneShape and one call to your drawOneDESCRIPTION function, so that the two images are drawn with different sizes and in different positions on the screen. They should be called with different size arguments. The creation of the turtle used and the two function calls should be done directly from the main block. For example, the screenshot above shows two snow-people of different sizes. Instead of drawing two of the same image, your code should draw one geometric shape and one more complicated image.

Second Module: DrawRandom.py

The module DrawRandom.py, when run, should prompt the user to choose an image-drawing function via an input command. (You can read about receiving input from the user in Section 2.8 of the course textbook.) It should then prompt the user again, this time to specify a number of images to be drawn, and then use the specified image-drawing function to draw that number of images. Your module must use random coordinates and random size when drawing the images.

Start by completing the function drawEverywhere, which takes two parameters: a turtle and the name of a function. The function passed to drawEverywhere is an image-drawing function that takes two parameters, like drawOneShape and drawOneDESCRIPTION from TurtleShapes.py. When called, drawEverywhere will prompt the user for the number of shapes to draw, and then use the function parameter to draw that number of images at random locations and with random sizes on the screen. (The input command to choose which function to use will be done in the main block; see the following section for details on the main block.)

The main program block of DrawRandom.py should contain the creation of the turtle used, a call to the input() function to prompt the user to specify a function, and two possible calls to drawEverywhere. You can prompt the user to choose a function with something like this:

funcNumber = input("Input 0 for drawOneShape, 1 for drawOneSnowman")

When you call drawEverywhere in the main block, its arguments should be a turtle and the name of the function chosen by the user: either drawOneDESCRIPTION or drawOneShape. You should use if statements inside the main block to specify two possible calls to drawEverywhere, one of which will be made based on the user’s choice. DrawRandom.py can access drawOneDESCRIPTION and drawOneShape by using import TurtleShapes at the top of the DrawRandom module.

See the code in ColorMyWorld.py (in the extras zip file) for ideas on using randomness when drawing with the turtle. Part of the output of module ColorMyWorld is shown above. The example also uses random color, but that is not a requirement for DrawRandom.py.

Third Module: DrawRow.py

The module DrawRow.py, when run, should draw ten rows of images across the screen, with each row at a different y-coordinate. The rows must have at least ten shapes in each row (they can overlap), and the shapes in a given row must all have the same size and y-coordinate. The size used in a given row should be different from the sizes used in other rows, and sizes should be guaranteed to be different (not random). The screenshot above shows two rows of snow-people that meet these size requirements.

Complete the function drawRowsOfRows, which takes two parameters: a turtle and the name of a function. The function passed to drawRowsOfRows is an image-drawing function with two parameters, like drawOneShape and drawOneDESCRIPTION from TurtleShapes.py. When called, drawRowsOfRows will use the specified function to draw at least ten rows of images with at least ten images in each row, meeting the size requirements described above. Do not copy-paste 10 for loops to make the rows! (Hint: Use nested loops or write a helper function.)

The main program block of your DrawRow.py module should contain the creation of the turtle used and a call to drawRowsOfRows. When you call drawRowsOfRows, its arguments should be a turtle and drawOneDESCRIPTION from TurtleShapes.py. The rows drawn should consist of the shape drawn by drawOneDESCRIPTION. Don’t forget to import TurtleShapes at the top of the DrawRow module.

Bonus Module: DrawDesign.py

This isn't required and you do not receive extra credit. Here is the result of code we wrote that calls our own shape function and specifies coordinates so that the letter K was drawn. Using your TurtleShapes module, create a design of your own that consists of at least 15 calls to your drawOneDESCRIPTION function.

Grading

This assignment is worth 35 points. (Available on Gradescope)

Autograded portion [14 points]

  • [4] TurtleShapes.py
    • [2] drawOneShape has two parameters and uses the turtle parameter
    • [2] drawOneDESCRIPTION has two parameters and uses the turtle parameter
  • [5] DrawRandom.py
    • [1] Function drawEverywhere takes in two parameters as specified
    • [1] Prompts the user for number of shapes with input()
    • [1] Calls the drawOneDESCRIPTION function passed to drawEverywhere
    • [1] Calls the drawOneDESCRIPTION function passed to drawEverywhere the number of times prompted, causing the shape to be drawn that many times
    • [1] drawEverywhere uses the turtle parameter
  • [5] DrawRow.py
    • [1] Function drawRowsOfRows takes in two parameters as specified
    • [1] Calls the function passed to drawRowsOfRows
    • [1] Function drawRowsOfRows calls drawOneDESCRIPTION function at least 5x5 times
    • [1] Function drawRowsOfRows calls drawOneDESCRIPTION function at least 10x10 times
    • [1] drawRowsOfRows uses the turtle parameter

Hand-graded portion [21 points]

  • [18] Running the code
    • [6] TurtleShapes.py
      • [1] Calls drawOneShape once
      • [1] Calls drawOneDESCRIPTION once
      • [1] drawOneDESCRIPTION image uses at least three different colors
      • [1] drawOneDESCRIPTION image uses at least two different geometric shapes
      • [2] Calls functions with different arguments for the parameter size and the locations of images drawn are different
    • [6] DrawRandom.py
      • [1] Prompts user for image-drawing function to use in main block
      • [1] Imports TurtleShapes and uses the image-drawing function specified by the user
      • [2] drawEverywhere draws the number of shapes prompted
      • [2] drawEverywhere draws shapes with random sizes and coordinates
    • [6] DrawRow.py
      • [1] Imports TurtleShapes and uses drawOneDESCRIPTION function
      • [1] Draws at least 5 rows and at least 5 shapes per row
      • [1] Draws 10 rows and at least 10 shapes per row
      • [1] Uses nested loops or a helper function to draw rows (does not copy and paste multiple for loops)
      • [2] Size used in each row is different, and shapes in the same row are the same size as each other
  • [3] Rest of submission
    • [2] points for docstring comments/name
    • [1] point for submitting the reflect form

Submitting

Checklist Before Submitting

You will submit the three modules you wrote that were provided in the starter code: TurtleShapes.py, DrawRandom.py, and DrawRow.py. Each of these should run on its own as specified above. All images drawn in these modules must fit in the bounding box, or they may not be graded.

  • TurtleShapes.py
    • Has the function drawOneShape that has exactly two parameters: a turtle and a size and uses the turtle to draw a shape of the specified size.
    • Has the function drawOneDESCRIPTION that has exactly two parameters: a turtle and a size and uses the turtle to draw an image of the specified size.
    • When run, it should call both of the above functions once, drawing two images with different sizes and at different locations.
  • DrawRandom.py
    • Has the function drawEverywhere, which meets the specifications outlined above and is called once in the main block.
    • When run, this module should prompt the user for a number of shapes, and then draw the shape at random locations and sizes on the screen.
  • DrawRow.py
    • Has the function drawRowsOfRows, which meets the specifications outlined above and is called once in the main block.
    • When run, it draws ten rows, each at a different y-coordinate. Each row should have at least ten shapes. The shapes in each row should be the same size, but the sizes used in each row should be different from the size used in other rows.
      • (Please review the specifications from the instructions carefully.)
  • The DrawRow and DrawRandom modules should import your TurtleShapes module and use its drawOneDESCRIPTION and drawOneShape functions.
    • In the main block of DrawRow, the function drawRowsOfRows should be passed drawOneDESCRIPTION as a parameter.
    • In the main block of DrawRandom, the function drawEverywhere should be passed either drawOneShape or drawOneDESCRIPTION as a parameter (based on user input).
  • Each function you write must have a docstring comment describing what it does.
  • You should have a comment with your name at the top of each module you submit.

Submission Instructions

  1. Log in to Gradescope.
  2. Under the CompSci 101 dashboard, click Assignment 3.
  3. Click Submit Programming Assignment, click “Click to browse”, and select one of the three required files. Then click “Browse Files” again and select the remaining.
  4. Once all files are selected click Upload.

You can submit more than once. Your last submission will be graded.

After Submitting, Reflect Form