CSC111 Programming Examples Week 7 2015

From dftwiki3
Revision as of 09:21, 9 March 2015 by Thiebaut (talk | contribs) (classDemo3.py)
Jump to: navigation, search

--D. Thiebaut (talk) 10:21, 9 March 2015 (EDT)


classDemo1.py


# classDemo1.py
# D. Thiebaut
# Uses graphics.py from Zelle
#
# This program displays the basic elements of a program
#
from graphics import *

def main():
    win = GraphWin("Class Demo 1", 600, 400)
    
    c1 = Circle( Point(50,150), 20 )
    c1.draw( win )
    c2 = Circle( Point( 100, 150 ), 20 )
    c2.draw( win )

    r1 = Rectangle( Point( 10, 100 ), Point( 150, 150 ) )
    r1.draw( win )
    
    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()


classDemo2.py


# classDemo2.py
# Uses graphics.py from Zelle
#
# This program displays the basic elements of a program
#
from graphics import *

def main():
    win = GraphWin( "Class Demo 2", 600, 400 )

    # create and draw a red circle
    center = Point( 100, 100 )
    circ = Circle( center, 30 )
    circ.setFill( 'red' )
    circ.draw( win )

    # add a label inside the circle
    label = Text( center, "red circle" )
    label.draw( win )

    # create and draw a rectangle
    rect = Rectangle( Point( 30, 30 ), Point( 70, 70 ) )
    rect.draw( win )

    
    win.getMouse() # Pause to view result
    win.close()    # Close window when done

main()


classDemo3.py


# classDemo3.py
# Uses graphics.py from Zelle
#
# This program moves a ball on the screen
#
from graphics import *

def main():
    win = GraphWin( "Class Demo 3", 600, 400 )

    # create and draw a red circle
    center = Point( 100, 100 )
    circ = Circle( center, 30 )
    circ.setFill( 'red' )
    circ.draw( win )

    # set initial direction of ball
    dx = 1.5
    dy = 0.25

    # move ball on screen
    while win.checkMouse() == None:
        circ.move( dx, dy )
        
        #x = circ.getCenter().getX()
        #y = circ.getCenter().getY()
    
    win.close()    # Close window when done

main()