CSC111 Programming Examples Week 7 2015

From dftwiki3
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 red circle around the window.
# It disappears after a while...

from graphics import *

def main():
    win = GraphWin( "Class Demo 3b", 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()


classDemo3.py with 2 moving circles


# Uses graphics.py from Zelle
#
# This program moves two balls on the screen, having them
# bounce off the walls, but not bounce off each other.#
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 )

    # create and draw a red circle
    center2 = Point( 100, 100 )
    circ2 = Circle( center2, 30 )
    circ2.setFill( 'yellow' )
    circ2.draw( win )

    # set initial direction of ball
    dx = 7.5
    dy = 2.25

    dx2 = 1
    dy2 = -5
    
    # move ball on screen
    while win.checkMouse() == None:
        # move circ following its direction
        circ.move( dx, dy )
          
        # check to see if it bounces off the wall
        x = circ.getCenter().getX()
        y = circ.getCenter().getY()
        if x > 600 or x < 0:
            dx = -dx
        if y > 400 or y < 0:
            dy = -dy

        # move circ2 following its own direction
        circ2.move( dx2, dy2 )
          
        # get its x, y coordinates and check to see
        # if the center is moving outside the window.
        x2 = circ2.getCenter().getX()
        y2 = circ2.getCenter().getY()
        if x2 > 600 or x2 < 0:
            dx2 = -dx2
        if y2 > 400 or y2 < 0:
            dy2 = -dy2
        
    win.close()    # Close window when done

main()