Lab 4: Python Graphics
Goals:
- Getting the graphics module to work
- creating and drawing some shapes
- More loops
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:
- Point(x,y): takes integer coordinates
- Line(point1, point2)
- Circle(centerpoint, radius)
- Rectangle(upperleftpoint, lowerrightpoint)
- Oval(point1, point2), where point1 and point2 are the upperleft and lowerright corners of the bounding rectangle)
- 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.