Okay, I think I've got it. I will no doubt make changes as the semester wears on, but this should be enough to get me started.

#!/usr/local/bin/python

# Simple graphics module for Perspectives in Computer Science
# A procedural front-end for John Zelle's graphics.py
# Peter Drake

import graphics as z

def background(color):
    '''Sets the background color of the graphics window'''
    root.setBackground(color)

def _point(point):
    '''Converts a list of two ints into a Zelle Point object'''
    return z.Point(point[0], point[1])

def line(point1, point2):
    '''Draws (and returns) a line from point1 to point2'''
    result = z.Line(_point(point1), _point(point2))
    result.draw(root)
    return result

def oval(upleft, downright):
    '''Draws (and returns) an oval in the rectangle
    bounded by upleft and downright'''
    result = z.Oval(_point(upleft), _point(downright))
    result.draw(root)
    return result

def polygon(*corners):
    '''Draws (and returns) a polygon through points'''
    result = z.Polygon(*[_point(p) for p in corners])
    result.draw(root)
    return result

# This is currently broken for some reason having to do with
# the OS X Python/IDLE implementation
def text(center, string):
    '''Draws (and returns) text centered at location'''
    result = z.Text(_point(center), string)
    result.draw(root)
    return result
    
def fill(shape, color):
    '''Sets the fill color of shape'''
    if not (isinstance(shape, z.Line) or isinstance(shape, z.Text)):
        shape.setFill(color)
        root.update()

def outline(shape, color):
    '''Sets the outline color of shape'''
    if (isinstance(shape, z.Line) or isinstance(shape, z.Text)):
        shape.setFill(color)
    else:        
        shape.setOutline(color)
    root.update()

def delete(shape):
    '''Deletes shape from the graphics window'''
    shape.undraw()
    root.update()

def color(red, green, blue):
    '''Returns a color with the specified values, each of
    which should be at least 0 and less than 256'''
    return '#%02x%02x%02x' % (red, green, blue)

def mouse():
    '''Waits for a mouse click and returns its coordinates'''
    p = root.getMouse()
    return [p.getX(), p.getY()]

root = z.GraphWin()

print "If you can't see the graphics window,"
print "it's probably at the upper left"
print "of your screen, behind this one."

Is anyone else using Python procedurally in an intro course, or is what I'm doing here perverse?

Peter Drake
Assistant Professor of Computer Science
Lewis & Clark College
http://www.lclark.edu/~drake/




_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to