CS 240: Data Structures and Algorithms
James Madison University, Spring 2013

In Class Activity #1: Making Faces

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.
goto(x, y) Move the turtle to the indicated position. The orientation is not changed.

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 Rectangle
    • 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 rectangle. Your program must contain two python functions:
      • 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).
      • main() This function must include several calls to the rectangle 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 faces.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. Faces
    • Add a new function to your program with the signature face(x, y). This function should draw a face composed of four rectangles. The lower-left corner of the face should be located at position (x, y), and the overall size of the face should be 50x50 pixels. The result should look something like the following:

      Don't write this function by copying and pasting the code from your rectangle function! This function should include four calls to rectangle.

      Update your main so that it includes at least one call to your newly defined face function.

  4. User Input
    • Add one more function to your file named promptAndDraw(). This function takes no parameters. It should prompt the user for a location and should draw a face at the indicated position.
    • 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 faces.py
        X Position?
        20 
        Y Position?
        30
      
      With the result that a face is drawn at position (20, 30).
    • Modify your main function so that it calls promptAndDraw in an infinite loop. The user should now be able to create any number of faces.
  5. If you Have Extra Time
    • Create and test a new function named faceGrid(x, y, size). This function should create a size x size grid of faces beginning at position (x,y). In other words, calling faceGrid(0, 0, 2) would create a grid of four faces with the bottom-left corner of the bottom-left face at position (0,0). Adjacent faces should touch at their edges.
    • Create and test a new function named function scaledFace(x, y, size). This function should work exactly like face(x, y), except it will allow the user to select a size for the face in pixels. In other words, the call scaledFace(0, 0, 100) would create a 100x100 pixel face at position (0, 0). All of the facial features should scale appropriately.

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