Lab 5: Python Graphics
Comp 150, Dordal, Feb 24, 2006
Goals:
- Getting the graphics module to work
- creating and drawing some shapes
- More loops
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:
- Point(x,y): takes integer coordinates
- Line(point1, point2)
- Circle(centerpoint, radius)
- Rectangle(upperleftpoint, lowerrightpoint)
- Oval(point1, point2) See Zelle for how point1 and point2 are used
- Polygon(point1, point2, ..., pointN)
- Text(centerpoint, string)
Operations on a shape s:
- s.draw(win) draw it in GraphWin win
- s.undraw() erase it; win is not a parameter
- s.setOutline(string) set a shape's outline color; eg string = "cyan"
- s.setFill(string) set a shape's fill-in color; shapes are hollow by default
- s.setWidth(pixels) set width of outline. Try pixels = 3
- s.move(dx, dy) move dx units left and dy units down. These can be negative to move right or up.
- s.clone() creates an independent copy. Move the clone and the original is unaffected.