In-Class Activity #1: Drawing Pictures

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 and loops.

Introduction

The following example illustrates the process of importing 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.
>>> from turtle import *
>>> forward(100)
>>> left(90)
>>> 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.
goto(x, y) Move the turtle to the indicated position. The orientation is not changed.
setheading(to_angle) Set the orientation of the turtle to the indicated angle. Zero degrees points to the right, and ninety degrees points up.
exitonclick() This command prevents the turtle screen from closing until it is clicked by the user. This should be the last command in your main.

Exercises

  1. Getting Started
    • If you haven't already, open a terminal window and start the Python interpreter by typing "python" at the prompt.
    • Spend a few minutes experimenting with the turtle commands from the table above.
  2. Rectangles
    • Save the file shapes.py to your Desktop.
    • Start IDLE from the start menu and open shapes.py.
    • Add a new function with the following signature:

      rectangle(x, y, width, height)

      This function should draw a rectangle with the indicated width and height. The lower-left corner of the rectangle should be at position (x, y).
    • Modify your main so that it calls your newly created function. Test your program by pressing F5 or clicking on "Run Module" under the Run menu.
  3. Polygons
    • Add a new function to your program with the signature

      polygon(x, y, sides, size)

      This function should draw a polygon with the indicated number of sides at position (x, y). The length of each side should be determined by the size parameter.

      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.
  4. If you Have Extra Time
    • Create and test a new function with the following signature:

      grid(x, y, rows, columns, size).

      This function should create a rows x columns grid of squares. The lower-left corner of the grid should be at position (x,y). The size parameter indicates the width of the individual squares.

      Don't copy/paste code from the existing square function! Your grid function should invoke square with appropriate parameter values.

    • OR
    • Create and test a new function with the following signature

      picture()

      When this function is invoked it should draw a beautiful picture using the shape primitives implemented above.