Lab 4: Python Graphics

Goals:

To load the graphics module, first download it and put it in your working directory. Here is the newest version: Either start python from the command line after first switching to the same directory that you saved the graphics.py into, or else start Idle and switch directories using, for example,
    import os
    os.chdir('d:/userdata/pld')
       # or whatever you named your directory

Note added later: the above does not work! You must save (or copy) graphics.py into the system folder C:\Python26. Then it will be available for import into an Idle session.

Then load the graphics module as follows. We will not be modifying this file, so reloading is not an issue.
    from graphics import *
Create a graphics window with the following (500 and 400 represent the size, in pixels; you can change this):
    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, including 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.

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 paste in (or otherwise 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)
I find it easiest to clone the center point like this, but you can also clone the left eye and move it right by twice as much horizontally (100) and nothing vertically (0). Or skip cloning, and just do everything "from scratch".

Mail me your face-drawing function, as usual.

Reference

Basic shapes:

Operations on a shape s: