|
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
- to learn how to design, implement and use functions in your programs,
- to experiment with a drawing system known as Turtle Graphics, and
- 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:
- a position -- e.g., its current (x, y) coordinates on the Cartesian plane, initally (0,0)
- an orientation -- e.g., the direction it is facing in degrees from 0 to 360, initally 0 (due east)
- a pen, which can be of different colors and can be either up or down, initially down (i.e. drawing mode).
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:
- 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.
- 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.
- Start TurtleWorld by following the directions on
the Swampy
web site. Work through the examples shown there, including:
- Using the buttons to control.
- Running the example script in the Run Code window, and
- Running a script stored in a file, using the Run File button.
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:
- square(t) - which uses the turtle t to draw a square.
- square(t,length) - which uses the turtle t to draw a square with sides of length length.
- polygon(t, n, length) - which draws an n-sided regular polygon with sides of length length.
- circle(t, radius) - which draws a circle of radius radius.
- 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.