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.
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.
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).
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 |
| Windows (your own computer) |
C:\CS5NM |
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.
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
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:
N:\CS5NM\swampy.1.1
C:\CS5NM\swampy.1.1
/Users/yourname/swampy.1.1
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.
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")
sys.path.append("N:/CS5NM/swampy.1.1.zip") won't work.sys.path.append("N:/CS5NM/swampy.1.1") (without the .zip at the end).sys.path.append("N:/CS5NM/swampy.1.1.zip")sys.path.append("N:\CS5NM\swampy.1.1.zip")swampy.1.1 somewhere else, put that instead of "N:/CS5NM/swampy.1.1" sys.path.append("/Users/myname/CS5NM/swampy.1.1") on a Macsys.path.append("C:/CS5NM/swampy1.1") swampy.1.1Once 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 >>>
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.)
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 |
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.
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.
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
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)
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.
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.
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)
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.