By the time you have completed this lab, you should be able to
If your partner is not present at the start of lab, please wait for five minutes and then ask the TA to pair you with someone else for this week.
For the rest of these instructions, we are going to assume you are working in Phelps 3525 or CSIL. If not, you'll need to adapt the instructions to your computing environment.
Decide which one of you will be the first pilot for this lab, and then log in to the pilot's account and create a directory called lab02 inside his/her cs8 folder. You will work in this account for the rest of the lab, but remember to switch roles a couple of times between pilot and navigator during the course of the lab.
By the way, there are two ways to create a new directory (folder):
cd
and mkdir
commands like you did in prior labs, orNext, in IDLE, select "File=>New Window" to open a new "untitled" window for Python code (just like you did in lab00 for zen.txt).
When it comes up, click and drag the window by its title bar over to the right of your Python Shell window (so the shell window is not covered up).
Once you've opened that file, add a comment to the top of your (Untitled) file like this one, substituting your own name(s) and the current date in the proper spots:
# lab02.py # Name: Agnes Nitt and Jason Ogg, 4/18/2017 # some functions to draw pictures using Turtle Graphics
Then, save it under the name lab02.py inside your lab02 directory.
First make sure that you can show turtle graphics. Try this command at the Python shell prompt (>>>):
>>> import turtle
>>>
If what you get back is just another Python prompt (not a bunch of error messages) then you are good to go.
Then type (still in the Python shell window) the following command:
>>> pat = turtle.Turtle("turtle")
That last command will open another new window - probably titled "Python Turtle Graphics" - and eventually this window will display your drawings. But ignore it for now. In fact, you will need to recreate it later.
Back in the new file window (lab02.py file), type the drawRectangle function you were asked to write in Hw2 (if you did not do it then, you will have to do it now - instructions are in Hw2), and add a comment in front of it, as in the example below:
# function to draw a rectangle # parameters: name of a turtle, and the width and height to draw # draws: a rectangle at the position of the turtle
Once you've typed in the function, save the file, and use the Run=>Run Module command to "compile" your Python code. You might get an error message when you do this the first time - read the message, and figure out what to fix in your function definition. Keep fixing, saving and running the module until it works without error messages. Then look in the Python Shell window - you should see something like this:
>>> ================================ RESTART ================================
>>>
Congratulations! You just compiled and loaded into memory a useful function that we can use to make drawings.
Unfortunately though, IDLE just destroyed a portion of the work you did earlier: pat the turtle is gone from memory (the Python shell was restarted). Not such a big loss this time - just two commands to repeat, but annoying nonetheless. Thankfully IDLE does provide a way to repeat prior commands. Try that way now: click (with the mouse) on the "import turtle" line (inside the Python Shell window) and then hit the enter key - notice the command appears at the >>> prompt - hit enter one more time to, well, enter it. Repeat the "pat = ..." command too.
Now, to test your function, try calling it with various values for width and height:
>>> drawRectangle(pat, 20, 50) >>> drawRectangle(pat, 100, 75)
You can also try moving pat between the rectangles, picking up the pen first—for example this will move pat over a bit before drawing the next rectangle.
>>> pat.up() >>> pat.forward(100) >>> pat.down() >>> drawRectangle(pat, 60, 80)
After playing with pat and the drawRectangle function for awhile, move on to Step 5.
IMPORTANT: You must try this step, but you don't have to succeed at it to earn full credit. Please begin it and try to finish it, but if you are running out of time before lab ends, then move on to Step 6 - the TA will check off your work even if it is not exactly correct, as long as you can show evidence that you tried.
First switch roles between pilot and navigator if you did not already do that.
Add a function named drawHouse to the lab02.py file according to the following specifications:
>>> drawHouse(pat, 100) # a really big house >>> drawHouse(pat, 10) # a tiny house (just 10 pixels wide)
Here is an example: | Note: your house doesn't have to look exactly like this—i.e. the proportions of the width/height and the steepness of the roof can be different. As long as it "looks like a house", that's good enough for this lab. |
Get your TA's attention to inspect your work, and to record your lab completion. |
Don't leave early though ... see challenge problems below.
Step 6a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK
In the unlikely event that you must complete this assignment at CSIL, then submit it with the turnin program - but do not turn it in if the TA already checked you off. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Phelps 3525. Use the following command after you cd into your cs8/lab02 directory:
turnin Lab02@cs8c lab02.pyAfter answering the questions, be sure to wait for the message indicating success.
Optional Extra Challenge
- Make a function to draw a block (row) of houses. Precede the function definition with an appropriate comment, like always! Write the function header to take three parameters: a turtle, the width of one house, and the number of houses to draw:
def drawBlock(turtle, width, number):The function should draw as many houses as the parameter named number specifies, and each house should have the specified width. Separate the houses by moving the turtle without drawing any lines - pick up the pen, move, and then put it down again. We separate the houses in our solution by 1/2 house width. Here is an example (12 houses, each width 25 in the original drawing):
- Add a door and window(s) to your house, by modifying your drawHouse function. Of course you should use drawRectangle to help. Maybe add a chimney, eaves on the roof (by adjusting to let it overhang the sides), and/or other interesting features. After you test the new drawHouse function, and if you used it in your drawBlock function, then try drawBlock again to see your new features in a whole row of houses.
- It shouldn't be too difficult for you now to create a picketFence function - each slat of the fence is like a tall, skinny (plain) house, and a section of these slats is like a row of houses.
- Still have time after writing picketFence? Then why not try a drawYard function that draws a house with a picket fence in front of it? You might even try to add a driveway, and a garage, and ...
Template © 2009, 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. Adapted by Diana Franklin and Michael Costanzo.