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.
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.
Part 2. do some more graphics
angRad = (2.0*math.pi)*angle/360.0Do you remember that we went over this in class? To use the cos and sin functions, place
import mathat the top of your file, where we import modules.
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.
Note for later: When you add in the command structure in Hw7, if
the turtle command is pd, you will have one statement:
pd = TrueAnd if the command is pu, you will have one statement: pd = False |
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.
# your code for this goes here # look up the Line class on Zelle's graphics page or in Chapter 4Type this all in and try it with both values of pd (True and False).
pd = TrueThe 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.
hw7.py *.gifhw7 builds directly on this lab. If there's more lab time, start on hw7 now.