CPSC 115L: Introduction to Computing Fall2010

Laboratory 3: Functions and Turtle Graphics

September 22, 23

As usual, you are expected to work with an assigned partner as a pair. Both you and your partner will receive the same grade. Both of you should always save your laboratory work on your own accounts.

Preliminaries

Before coming to this lab, you must read Chapter 4 of Downey, Case Study: Interface Design.

Objectives

The main objectives of this laboratory are
  1. to learn how to design, implement and use functions in your programs,
  2. to experiment with a drawing system known as Turtle Graphics, and
  3. to continue tutorials on GNU/Linux's command-line interface.

Introduction

TurtleGraphics is a computer graphics methodology that was popularized in the 1960s and 1970s by the Logo programming language. It provides a set of functions for drawing lines by steering a turtle around on the screen. The turtle has three attributes:

You can steer a turtle using several functions, fd(n) and bk(n) for moving forward or backward by n pixels, and lt(d) and rt(d), for turning left or right by d degrees. You can also move the pen up and down with the pu() and pd() functions.

Installing and Testing Turtle World

Our text comes with a turtle graphics application called TurtleWorld. Download it from http://www.greenteapress.com/thinkpython/swampy/install.html and follow the directions there for installing it. The basic steps are:

  1. Test that your version of Python has the Tkinter package. To do this step, you will have to start the Terminal application. The Tkinter package should be installed. If not, get help from the TA or instructor.
  2. Download Swampy 1.4 for Python 2.7 and unpack it. Put it in your cpsc115 directory. When you unpack it, it will create a directory named swampy.1.4. Change into that directory: cd swampy.1.4.
  3. Start TurtleWorld by following the directions on the Swampy web site. Work through the examples shown there, including:

Textbook Exercises

Work through exercises in Section 4.3 of the Downey text. The solutions to these exercises are given in sections 4.4 through 4.7. A good way to do these would be to try to do the exercise on your own without looking at the solution. When you are done you will have written and tested the following functions.

Save your solutions to the exercises in a single file, called lab3.py. Use gedit or some other text editor to write your programs. Put your specifications and pseudocode as comments in your file. For each exercise, demonstrate your solution and show your code to the TA or instructor:

  1. square(t) - which uses the turtle t to draw a square.
  2. square(t,length) - which uses the turtle t to draw a square with sides of length length.
  3. polygon(t, n, length) - which draws an n-sided regular polygon with sides of length length.
  4. circle(t, radius) - which draws a circle of radius radius.
  5. arc(t, radius, angle) - which draws a fraction of a circle, given by angle in degrees -- that is, a quarter of a circle would use an angle = 90 degrees.

Free Form Exercise

Implement one of the shapes in Exercise 4.2 and one of the shapes in Exercise 4.3 on page 45 of Downey. As described there, your solution should use an appropriately general set of functions -- that is, it should make proper use of parameters.

Documentation

For each function that you write, write a docstring that describes that function's interface. See Downey section 4.9.

As usual, at the beginning of each of your scripts (files), put a header in the following format:

#
# File: wage.py
# Author: Takunari Miyazaki
# Lab section: Tuesday
#
# Created:  02/09/10
# Modified: 02/09/10
#

Learning GNU/Linux's command-line interface

For the remainder of this laboratory session, walk through the remaining three tutorials of the UNIX Tutorial for Beginners (for this, you must be on this Laboratory 3 webpage and click the link provided). When completed, show your work to the instructor or TA.

What to hand in

Upon completion of your laboratory, submit a hardcopy printout of all your scripts. They should all be properly documented.

* CPSC 115L home page
Valid HTML 4.01!