CS 240: Data Structures and Algorithms
James Madison University, Fall 2012

In Class Activity #1: Turtle Graphics

Objectives

The goal of today's activity is to get comfortable working with basic features of the Python programming language including the Python interpreter, variables, user defined functions, loops, and user input.

Introduction

While Python was not designed to be an educational programming language, it has become a popular choice for teaching beginning programmers. One of the earliest programming languages that was designed specifically for educational use was Logo. The Logo language featured a graphical turtle that could be programmatically moved on the screen. The Python standard library includes a Logo-inspired turtle graphics module. The activity today involves getting comfortable with Python by experimenting with turtle graphics.

The following sequence of commands illustrate the process of importing the turtle module and executing turtle commands.

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> turtle.forward(100)
>>> turtle.left(90)
>>> turtle.forward(100)

Complete documentation for the turtle module can be found in the Python Standard Library Documentation. The following functions should be enough to get you started for today's activities:

forward( distance )
backward( distance )
Move the turtle forward/backward the indicated distance (in pixels).
left( angle )
right( angle )
Turn the turtle left/right by the indicated angle (in degrees).
penup()
pendown()
After penup() is called, the turtle will not draw until pendown() is called.

Exercises

  1. Getting Started
    • Open a terminal window (located under Applications->Utilities on OS X. In CS/ISAT 250 it should be a black square in the Dock at the bottom of the screen.). Start the Python interpreter by typing "python2.7" at the prompt.
    • Spend a few minutes experimenting with the turtle commands from the table above.
  2. Drawing a Square
    • Start the IDLE editor by typing "idle2.7" in a terminal window.
    • Select "New Window" under the IDLE "File" menu. This should bring up an empty editor window.
    • Write a short Python program that draws a square. Your program must contain two python functions:
      • square( size ) This function should draw a square at the current turtle position. The size parameter will determine the length of the sides of the square.
      • main() This function must include a single call to the square function described above.
      The last statement in your in your .py file should invoke the main() function as follows:
      if __name__ == "__main__":
          main()
      
    • Save your file with the name shapes.py and test it. You can run your program from the IDLE "Run" menu, or by typing "python shapes.py" at the terminal prompt. Try out both methods. Don't panic if you can't close the turtle window when you run your program from IDLE. The window will close when you exit IDLE, or re-run your program.
  3. Polygons
    • Add a new function to your program with the signature polygon(sides, size). This function should take two integer parameters: one indicating the number of sides for the polygon and one indicating the length of each side. When invoked, it should draw an appropriate shape at the current turtle position.

      Hints:
      • This could be accomplished using either a for loop or a while loop. The best approach is to use a for loop along with the range function to control the number of loop iterations.
      • Notice that the turn angles required for drawing a square were 90° and that 90 = 360.0 / 4.
    • Update your main so that it includes several calls to your newly defined polygon function. For example, you might draw a small triangle inside a large octagon inside an even larger square.
  4. User Input
    • Add one more function to your file named promptAndDraw(). This function takes no parameters. It should prompt the user for the number of sides for a polygon and the length of the polygon sides, and should then draw the appropriate shape.
    • Modify your main function so that it only includes a single call to promptAndDraw. A session with your program should look like the following:
        > python shapes.py
        How many sides should the polygon to have?
        3 
        Please enter the length of each side (in pixels).
        30
      
      With the result that an equilateral triangle is drawn to the screen.
    • Modify your main function so that it calls prompt and draw in an infinite loop. The user should now be able to create any number of shapes.
  5. If you Have Extra Time
    • Create a new function named function polygonXY(sides, size) that takes an x and y position to begin drawing. The new version of the function should have the signature polygonXY(x, y, sides, size). Modify promptAndDraw so that it requests x and y positions from the user and calls the new function.
      Hints:
      • You'll need to use the goto function to accomplish this. You can read about it in the Turtle Documentation.
      • Use the pendown and penup functions to avoid drawing a line when goto is executed.
    • Write a parametrized function named polygonOfPolygons that draws a large polygon composed of smaller polygons. For example, this function might be used to draw a large triangle composed of small octagons. Test your function by calling it from main.

Finishing Up

There is nothing to hand in for this assignment. Make sure that you save a copy of your code, either on a thumb drive, the N: drive, or by e-mailing it to yourself. If you worked with a partner, make sure both of you get a copy. The next homework assignment will build on this work.