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

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 are ready to proceed with 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 * 


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 do is to write a function definition for the item that you were planning to draw for ex09, but using the Turtle instead of the more complicated PyGame routines.

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.

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.

Step 4: Submitting on GauchoSpace

(Coming soon...)

 


Evaluation and Grading (100 pts)

Grading Rubric:

(coming soon)

 

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.