Lab 5: Python Graphics

Comp 150, Dordal, Feb 24, 2006

Goals:

Graphics

To load the graphics module, first download it and put it in your working directory. There are two versions: I'd stick with the earlier version for now. Start python in the same directory that you saved the graphics*.py into, and load the graphics module as follows:
    from graphics22 import *
If you're using the later version, of course, you import graphics32. We will not be modifying this file, so reloading is not an issue. Create a graphics window with the following:
    w = GraphWin("my window", 500, 400)
To draw a circle in this window:
    center=Point(200,100)
    circle=Circle(center, 100)
    circle.draw(w)
To make the circle solid:
    circle.setFill("cyan")
Try other colors too: "red", "red3", "purple", "green4". Is there a "cyan2"? "magenta"? "cerulean"?

How about a rectangle with some text:
    rect = Rectangle(Point(100, 50), Point(300, 150))
    rect.draw(w)
    hi = Text(Point(200, 100), "hello there")
    hi.draw(w)
Note that there is a hierarchy here: shapes like Circle and Rectangle and Text are defined in terms of Point objects. There are several other shapes; see Zelle, page 151. Others include Oval, Line, and (my favorite for drawing) Polygon. There are also a bunch of things you can do with shapes once you've drawn them: draw, undraw, setFill (fill color), setOutline (outline color), move, and clone. See Zelle for details.

Your assignment

Draw a face. It should have a circle (or oval) head, two eyes, a nose, and a mouth. The mouth (or some other feature) should be a non-Circle (eg a Polygon). Ears are optional. Actually a face is optional: any picture will do but it should have at least six components of at least two (preferably three) different types. Try to use color too. Put your drawing steps into a file, so you can reload and redraw easily (you're likely to need to do that, as it's hard to figure out coordinates without trial and error).

Put all your shape-creating into a file, and put all the draw parts into a function to make it easy to re-do at will. It's easiest if you make the GraphWin thing, w, a parameter:

def drawface(w):
   head.draw(w)
   lefteye.draw(w)
   righteye.draw(w)
   nose.draw(w)
   mouth.draw(w)

One trick that's handy is to pick a center point, c=Point(200, 200), and then work out all the other points relative to your center:
    lefteyep = c.clone()
    lefteyep.move(-50, -20)
    hi = Text(Point(200, 100), "hello there")
    lefteye = Circle(lefteyep, 10)
The book talks about cloning the entire left eye and moving it right; that's also possible. But I find it easier just to clone the center point.

Mail me your face-drawing file, as usual.

Reference

Basic shapes:

Operations on a shape s: