CSC111 First Graphics Demo Program

From dftwiki3
Revision as of 08:09, 4 October 2011 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- <source lang="python"> # graph1.py # D. Thiebaut # some demo programs to start # playing with the graphics module # from graphics import * def test1(): # defin...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 09:09, 4 October 2011 (EDT)


# graph1.py
# D. Thiebaut
# some demo programs to start
# playing with the graphics module
#

from graphics import *

def test1():
    # define a window with its geometry 
    win = GraphWin( "Shapes", 800, 600 )
    
    center = Point( 100, 100 )
    circ   = Circle( center, 50 )
    circ.setFill( "green" )
    circ.setWidth( 5 )
    circ.draw( win )
    label = Text( center, "Red" )
    label.draw( win )
    rect = Rectangle( Point( 10, 10 ), Point( 200, 200 ) )
    rect.draw( win )
    
    # wait for user to click graphics window...
    win.getMouse()                      
    win.close()

def test2():
    # run the test function in the graphics module
    test()

def car():
    # draw a rudimentary car 
    win = GraphWin( "Yellow Cab", 600, 400 )
    body = Rectangle( Point( 10, 60 ), Point( 300, 160 ) )
    body.setFill( "yellow" )
    body.draw( win )
    top  = Rectangle( Point( 30, 10 ), Point( 200, 60 ) )
    top.setFill( "yellow" )
    top.draw( win )
    wheel1 = Circle( Point( 30, 160 ), 20 )
    wheel1.setFill( "black" )
    wheel1.draw( win )
    wheel2 = Circle( Point( 300-50, 160), 20 )
    wheel2.setFill( "black" )
    wheel2.draw( win )
    fWindow = Rectangle( Point( 15, 70 ), Point( 200-5, 60-5 ) )
    fWindow.setFill( "white" )
    fWindow.draw( win )
    # wait for user to click mouse
    win.getMouse()

    # then move car out of window...
    for step in range( 200 ):
        body.move( 10, 0 )
        top.move( 10, 0 )
        fWindow.move( 10, 0 )
        wheel1.move( 10, 0 )
        wheel2.move( 10, 0 )
        
    # wait for user to click mouse to quit...
    win.getMouse()
    win.close()
    
def main():
    #test1()
    #test2()
    car()
    
main()