Smith College Computer Science 111 - Lab7. Fall Semester, 2013

The purpose of today's lab and the follow-on homework is to use graphics more and become more acquainted with object-oriented programming. You will begin to implement the logo programming environment. First you'll work on the graphics part, using the graphics module that accompanies the textbook. Read Chapter 4 of your textbook. Remember Section 4.8 has a nice summary of the graphics objects.

Part 1. Prepping and Getting Files.

  • Remember that you need to use the graphics module. Save it to your H: or I: folder. Right-click here if you need to download it again. graphics.py You can also watch the logo movie: You will need the franklin turtle image to start.
    Right-click and save-the-image:
    franklinTurtleSmall.gif
  • Now start editing a file called hw7.py. This lab actually is a start to the homework.
    Before starting your main() definition type in
    from graphics import *
    
    Recall, when we use the from graphics import * statement, we can use graphics functions and objects without putting graphics. in front of each one.


    Here is Zelle's graphics module on-line reference Follow these steps:
    1. Start a definition of a main function.
    2. Next, set up a GraphWin object and assign it to variavle win; name it Turtle Graphics, make it at least 400 by 400 pixels in size, and set its background to white.
      Also, change its coordinates so 0,0 is down in the left hand corner of the screen. Look up the GraphWin setCoordinates method:
      Go to and click on GraphWin, then scroll down to setCoordinate: Zelle's graphics module on-line reference
    3. Now figure out how to get an image in and draw it in the GraphWin window. The filename is "franklinTurtleSmall.gif".
      If you go to Zelle's on-line reference, and look near the bottom you will see a link for Displaying Images. When you click there one thing you can find out that the anchor point we discussed in class is at the center of the Image object.
    4. You are free to choose your own image of a turtle.
      This should be a small .gif file (not jpg). Try images.google.com.
      You could enter search keywords like "turtle" and "gif".
      My image is 143 x 162 pixels, a pretty good size for a 400x400 graphics window. An added benefit of google is that it tells you the image size in pixels.
    5. Now import hw7 and run main(). Do you see a window with your Turtle drawn in it? If not, fix it before going to the next part.

    Part 2. do some more graphics

    Note: These instructions assume there is a variable called angle that contains the number of degrees that the turtle is turned, counterclock-wise, from the horizontal, positive x axis. Add graphics features, one at a time, and test each before moving on to the next one.

    1. We will work on the hardest part now, that is, moving forward. To get started on this, we will assume a user has typed fd num where num is some integer (in units of pixels), and the user may have previously changed the angle from 0. We are going to let the user think in terms of units of degrees for the angle. But the math module functions that compute sine and cosine use radians as the units for angles.
      We need to do two things:
      1. change from degrees to radians
      2. use the math.cos() and math.sin() functions to compute the change in x and the change in y coordinates, where num is the hypotenuse.
      To convert from degrees to radians, use this code:
         angRad = (2.0*math.pi)*angle/360.0
      
      Do you remember that we went over this in class? To use the cos and sin functions, place
      import math
      
      at the top of your file, where we import modules.
      Then after computing the angle in radians, insert these two lines:
         dx = num * math.cos(angRad)
         dy = num * math.sin(angRad)
      
      Finally, add a statement to move the turtle to the new location:
         turtle.move(dx,dy)
      
      Remember: the move method moves relative to the center point. And that is done by the method, and the point coordinates are reset for you. So dx and dy are how much to move in the x and y directions, not where to go. To test this part, add temporary statements let the user type in a value for num and for angle in degrees (both should be integers). Run this program and see if the turtle moves where it should.
    2. Now we will start to implement the pen up (pu) and pen down (pd) commands. It's easy! Just make a boolean variable at the top of your main() definition, named pd, and assign it the boolean value True. All this means is that you add the statement
      pd = True
      Note for later: When you add in the command structure in Hw7, if the turtle command is pd, you will have one statement:
          pd = True
      
      And if the command is pu, you will have one statement:
          pd = False
      
    3. Now let's go back and change the statements we just made (which will be the fd num statements in hw7) so that if pd is true, a line is drawn when the turtle moves and otherwise the turtle moves and a line is not drawn. After calculating the angle in radians, and finding dx and dy, use this statement to get the current turtle image location:
          location = turtle.getAnchor()
      
      If pd is True, the program should create a line when the turtle moves forward. The Line class expects two Point objects, the beginning and end points. The beginning point is location (where the turtle is now) and the endpoint is calculated in this way:
          newX = location.getX() + dx
          newY = location.getY() + dy
          endlocation = Point(newX,newY)
      
      In other words, we get the x coordinate of the beginning point (where the turtle is) and add dx, and we get the y coordinate of the beginning point and add dy. We make a new Point out of the new coordinates and assign it to endlocation.
      Now we check the value of the variable named pd. If it is True, we make a new Line, draw it, and then move the turtle. Otherwise, we move the turtle without making and drawing line:
      # your code for this goes here 
      # look up the Line class on Zelle's graphics page or in Chapter 4
      
      Type this all in and try it with both values of pd (True and False).
      Get this all to work before moving to the next part.
    4. Now see if your turtle can make a square:
      Just for now, to test the fd command, set up a definite (for) loop around the above statements.
      Before the loop you'll have
      pd = True
      
      The loop should go long enough to make the four sides of a square. And inside the loop you'll have all of the statements needed to make the turtle go forward, after you set num = 150 and add 90 to angle.

      This should cause the turtle to draw a square.


      Do not move on until you get this part working!
    5. Next write code to implement the backward command. This is a little trickier, but is mostly the same as the forward command. Make sure it works for the general case when the angle is not 0.
      One way to test this is to move the turtle forward then sleep for 2 seconds (import time, etc)., then move it backwards and see that it goes back to where it started.
    6. Next write code to move the turtle to the home point. This is easier than the forward or backward commands because you already know the point to which to move the turtle.
    7. Let me see your turtle moves before you leave. Thanks.

      Notice! upload via sftp, your files to beowulf before you leave:

    8. To sftp all your files up to your 111a-XX account.


      Remember to put your
      hw7.py
      *.gif
      
      hw7 builds directly on this lab. If there's more lab time, start on hw7 now.