CS5nm, Fall 2008

ex10: working through Chapter 4
in the textbook (TurtleWorld)


Special Note—ex09 is postponed

Note: We are going to postpone the deadline for ex09 for two weeks to give you more time to prepare—specifically, you should be able to put off working on it until after the exam on Friday 10/24. The new due date will be 11/05.

I now realize that for many students, ex09 is a bit confusing. This assignment, and the other work we'll do in the next two weeks should help you be better prepared to work on ex09.

ex10 replaces ex09—and you are encouraged to finish it by 10/23.

The due date is Thursday 10/23 at 5pm—the day before the exam—because completing it will help you study for the exam.

I want to encourage you to complete it before the exam, so that you will be well prepared.

However, to reduce the stress level, I will accept this late—through 5pm on Wednesday October 29, with NO PENALTY.

So, the due date on GauchoSpace will show Wednesday October 29 at 5pm.

Goals for this exercise

By the time you have completed this exercise, you should be able to

The TurtleWorld examples do graphics in Python, but in a much simpler way than using PyGame.

TurtleWorld is not as powerful as PyGame, but it is much easier to understand.

This should help prepare you to better understand how to work with the PyGame material (such as ex09).

Step by Step Instructions

Step 0: Getting Started

Step 0a: Make a folder to store the TurtleWorld

You need to make a folder somewhere to store some files we'll need for this lab. If you already have a folder such as one of these, you are in good shape:

  Folder
Mesa Lab
(Windows)
N:\CS5NM or N:\cs5nm
Mac

/Users/yourname/CS5NM
or
/Users/yourname/cs5nm

Windows
(your own
computer)

C:\CS5NM
or
C:\cs5nm


The exact name and location doesn't really matter as long as you know what the name of that folder is, and it isn't really long and cumbersome to type—because you'll have to type it in later.

If you don't have a folder like this (with a nice short name), create one now.
If you are not sure how to do that, ask your TA or your Instructor for help.

Step 0b: Download the software for TurtleWorld (swampy.1.1.zip)

For this exercise, you'll need to download some extra software. The software is in a file called swampy.1.1.zip, which is about 2.2MB (Megabytes) in size, and can be downloaded from either of the links below.

http://www.cs.ucsb.edu/~pconrad/cs5nm/downloads/swampy.1.1.zip

http://www.greenteapress.com/thinkpython/swampy/swampy.1.1.zip

Step 0c: Unzipping the file swampy.1.1.zip

After you download the file swampy.1.1.zip, open up the folder where you stored it. You can double-click on it to "unzip it" into a folder called swampy.1.1. This folder will take up about 4.1MB of space. You should now have a folder such as:

The exact name will depend on where you stored the swampy.1.1.zip file. The important thing is to know where this is, because we'll need to use this in a later step.

Step 0d: Open IDLE and add this new software to sys.path

Open up IDLE in the usual way, and put your mouse pointer at the Python Shell Prompt. We are going to type some commands there to load the swampy.1.1 software into IDLE—but we'll do this a different way then we've done before.

In the past, to load something into IDLE, we just copied and pasted code into the window on the right. Because swampy.1.1 contains a entire collection of files, we can't cut and paste them all—it just would take too long. So, instead, we add the entire collection into IDLE like this:

>>> import sys
>>> sys.path.append("N:/CS5NM/swampy.1.1")

What you will need to type will depend on where you stored the swampy software (refer back to Steps 0a, 0b, and 0c).
For example, you might instead need to type if you are on a Mac.

>>> import sys
>>> sys.path.append("/Users/yourname/CS5NM/swampy.1.1")
Several things that you must be careful about:
  1. You cannot use the name of the swampy.1.1.zip file—you must have unzipped this into a folder
  2. Very important: you must use forward slashes like this / NOT backslashes like this: \
  3. If you stored swampy.1.1 somewhere else, put that instead of "N:/CS5NM/swampy.1.1"
 

Step 0e: Seeing if swampy and the TurtleWorld loaded correctly

Once you've completed Step 0d (the sys.path.append step), try this to see if it worked correctly:

>>> from TurtleWorld import *
>>>

If you don't get an error message, then all is well, and you can continue with Step 1!

If instead, you get this error, then something is wrong. In that case, review the instructions for Step 0d, or ask your instructor or TA for help.

>>> from TurtleWorld import *
Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> from TurtleWorld import * ImportError: No module named TurtleWorld >>>

 

Step 1: Try a Turtle World program

The following code is a simple Turtle World program from Chapter 4 of your textbook.

It creates a Turtle named bob, and then asks the turtle to move forward 100 pixels, turn right, and then move forward 100 pixels more.

To run this, copy and paste the code into your window, save it, and hit F5.

If it doesn't work: you might need to change the line that contains sys.path.append to be whatever you got to work in Step 0 (e.g. sys.path.append("N:/CS5NM/swampy.1.1")

# Turtle1.py     P. Conrad for CS5NM, 10/17/2008, UCSB
# Code from Think Python, by Allen B. Downey, Chapter 4
# Basics of TurtleWorld graphics

import sys sys.path.append("/Users/pconrad/CS5NM/swampy.1.1") # change this if needed
from TurtleWorld import *

world = TurtleWorld() # create a world (a screen) bob = Turtle() # create a turtle, and call him bob print bob # print some stuff about bob in the Python Shell window
fd(bob, 100) # move forward 100 pixels rt(bob) # turn right fd(bob, 100) # more forward 100 pixels

wait_for_user() # keep the screen open til we click the red X or circle to quit

If this works, you should find a window on the screen that looks like this.

If it does work, you are ready to proceed with Step 2. (If not, return to Step 1, and/or ask your instructor or TA for help.)

Things to keep in mind as you proceed to step 2

Just remember that you'll need to have these three lines (with the correct path to swampy.1.1) at the top of any file that you use with Turtle graphics.

import sys   
sys.path.append("/Users/pconrad/CS5NM/swampy.1.1")  # change this if needed
from TurtleWorld import * 

And, you need to include the following at the bottom, to keep the window open:

wait_for_user()  # keep the screen open til we click the red X or circle to quit

Finally, the following functions are defined for working with the turtle. It may be helpful to have this to refer to as you work through Chapter 4, and experiment with your own drawings.

function explanation
fd(turtle,distance)
move turtle forward by some distance
bk(turtle, distance)
move turtle backwards by some distance
rt(turtle)
turn turtle right 90 degrees
rt(turtle,angle)
turn turtle right by some other angle
lt(turtle)
turn turtle left 90 degrees
lt(turtle,angle)
turn turtle left by some other angle
pu(turtle)
pick up the pen
pd(turtle)
put down the pen


Step 2: Open up the online textbook to Chapter 4

The links below take you to Chapter 4 of the textbook (either link should work):

Once you've opened up this Chapter, just follow the instructions in the Chapter.

Be sure to read the paragraphs of text as well as the code carefully.

Type in the code, and run the code, but don't just do it in an "automatic" fashion—really think about what you are doing.

Working with TurtleWorld can be fun—and a lot easier than drawing with PyGame.

Step 1 consists of working through the entire Chapter 4. There is nothing to turn in.

There are several exercises there that you can try, but since solutions to all of these are online and available for download, I'm not going to ask you to turn these in. You are on the "honor system" to try these on your own, and check your answers against the available solutions.

What I will ask you to submit (for a grade) is a function definition for the item that you were planning to draw for ex09, but using the simple Turtle method instead of the more complicated PyGame method. We'll still do PyGame way, but we'll warm up first with the Turtle graphics approach. That is explained in Step 3 below.

Doing this both with TurtleWorld and with PyGame will help us see that there are different ways to accomplish the same task—and different ways of thinking about the same computation.

Step 3: Submitting on GauchoSpace

For this exercise, I'd like you to write a function definition that draws your drawing (the one you registered on GauchoSpace for ex09—a sailboat, or an iPod, or whatever you decided to draw). Store it in a file called ex10YourName.py (e.g. ex10TerryKrautner.py, ex10RezaShirazi.py).

The first parameter to your function should be a turtle, and the other parameters should make the function general, just like this examples from the chapter (see section 4.5):

def square(t, length):
   for i in range(4):
     fd(t, length)
     lt(t)

Your file should contain the necessary header information and the commands to get TurtleWorld imported, (just like the example in Step 1), your function definition, and then a call to your function to show that it draws your drawing properly.

Just like the example functions in Chapter 4, the function should draw the drawing whereever the turtle happens to be at the moment.

Depending on which way the turtle is facing, that might determine which way the drawing is drawn (right side up, upside down, sideways, etc.), so if you make some assumptions about that, be sure to document what they are in a comment, e.g.

# This function draws a house, right side up, 
# provided the turtle starts out facing right.
# The turtle should start at the lower left hand corner of the house

def drawHouse(t, width, height)
...

You might find it helpful to define some other functions to help you, such as square, circle, rectangle, etc., and use those functions to draw your drawing. If so, that is fine—just include those function definitions in your ex10YourName.py file.

Be sure to include a few function calls to your function

When using TurtleWorld, if you want to draw more than one house, you'll need to have the turtle lift up the pen, and move to a particular starting point before drawing the house.

drawHouse(bob,40,50)

pu(bob) # pick up the pen bk(bob,100) pd(bob) # put down the pen

drawHouse(bob,30,20)

pu(bob) # pick up the pen rt(bob) fd(bob,50) lt(bob) pd(bob) # put down the pen

drawHouse(bob,20,30)

Here is link to a full example—this may be helpful as a model for your response to ex10:

http://www.cs.ucsb.edu/~pconrad/cs5nm/08F/ex/ex10/ex10PhillConrad.py

Helpful Hints

Making the Turtle move slower

While you are debugging—that is, working out the "bugs" (errors) in your program—you might find it useful to make the turtle move slower with a line like this:

 bob.delay = 1.0 # Make bob slow by making him delay 1 second between moves

Put this right after the line that creates the turtle, e.g.:

 bob = Turtle()  # Create a Turtle, and call him bob
 bob.delay = 1.0 # Make bob slow by making him delay 1 second between moves

Be sure to make the turtle fast again before you submit:

bob = Turtle()  # Create a Turtle, and call him bob   
bob.delay = 0.01 # Make bob fast (delay only 0.01 seconds between moves)

 

print statements for debugging

If you are not sure whether you are calculating a quantity correctly, you can insert a print statement like this:

print "roofHouseAngleDegrees = ",
print roofHouseAngleDegrees

The comma at the end of the first print statement means "don't start a new line". You'll get some output on in the window that has the Python Shell prompt, like this:

roofHouseAngleDegrees =  36.8698976458

This can be helpful in determining whether some variable is being assigned the value that you expect.


Step 4: Submitting on GauchoSpace

The only file you need to submit for this exercise is ex10YourName.py.

Be sure that your file contains at least one function defintion that draws your figure, and that it also calls that function at least three times, with different arguments.


Evaluation and Grading (100 pts)

Grading Rubric:

20 pts: naming and submitting files correctly
20 pts: correctly calling function three times, picking up pen between calls
10 pts: calling function with different parameters
30 pts: function definition for a function that draws your drawing
20 pts: good style (commenting and choosing meaningful variable names)

 

Due Date: Thu 10/23/2008 (5pm)

Ex09 is due Thursday 10/23 at 5pm, and you are encouraged to complete it by then (to help you prepare for the exam.)

However, it will be accepted through Wed 10/29 at 5pm without penalty.


Copyright 2008, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.