CSC111 Lab 8 2018

From dftwiki3
Jump to: navigation, search

D. Thiebaut (talk) 14:42, 25 March 2018 (EDT)



...

<showafterdate after="20180402 00:00" before="20180601 00:00">

Solution Programs


demo5.py


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

def main():
    win = GraphWin( "Lab 7 Moving ball", 600, 400 )

    # create a green obstacle in the middle of the window
    x1 = 200
    y1 = 200
    x2 = 250
    y2 = 250 
    obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    obstacle1.setFill( "green" )
    obstacle1.draw( win )

    # create another green rectangle on the right of the first one
    x3 = 350
    y3 = 200
    x4 = 400
    y4 = 250 
    obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
    obstacle2.setFill( "magenta" )
    obstacle2.draw( win )
    
    # create and draw a red circle
    radius = 30
    center = Point( 100, 100 )
    circ = Circle( center, radius )
    circ.setFill( 'red' )
    circ.draw( win )

    # define the direction the circle will start moving in.
    # 5 pixels to the right every move, and 0.25 pixels down
    # every move.
    dx = 5.1
    dy = 2.51

    # as long as the mouse hasn't been clicked on the window
    # keep on looping.
    while win.checkMouse() == None:

        # move the circle in the current direction.
        circ.move( dx, dy )

        # get the x and y of the center of the circle.
        x = circ.getCenter().getX()
        y = circ.getCenter().getY()

        # if the center of the ball is outside the right or left boundary,
        # reverse the x direction of movement.
        if  x > 600 - radius or  x < radius: 
            dx = -dx
        if  y > 400 - radius or y < radius:
            dy = -dy

        # if the center of the ball is inside the first obstacle, stop
        # the ball.
        if  x1 < x < x2 and y1 < y < y2 :
            dx = 0
            dy = 0

        # if the center of the ball is inside the second obstacle, bounce
        # off
        if  x3 < x < x4 and y3 < y < y4 :
            dx = -dx
            dy = -dy

    
    # if we're here, it's because the the user clicked on the graphic window.
    # we can close everything and quit.
    win.close()    

main()


demo6.py


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

# ifInside: returns true of the coordinates of a point defined by 
# its coordinates ballX and ballY, are inside the rectangle defined by a top
# left point of coordinates obsX1, obsY1, and a bottom-right point
# with coordinates obsX2, obsY2.   Returns false if the point is outside
# the rectangle.
def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ):
    if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2:
        return True
    else:
        return False

    
def main():
    win = GraphWin( "Lab 7 Moving ball", 600, 400 )

    # create a green obstacle in the middle of the window
    x1 = 200
    y1 = 200
    x2 = 250
    y2 = 250 
    obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    obstacle1.setFill( "green" )
    obstacle1.draw( win )

    # create another green rectangle on the right of the first one
    x3 = 350
    y3 = 200
    x4 = 400
    y4 = 250 
    obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
    obstacle2.setFill( "magenta" )
    obstacle2.draw( win )
    
    # create and draw a red circle
    radius = 30
    center = Point( 100, 100 )
    circ = Circle( center, radius )
    circ.setFill( 'red' )
    circ.draw( win )

    # define the direction the circle will start moving in.
    # 5 pixels to the right every move, and 0.25 pixels down
    # every move.
    dx = 5.111
    dy = -2.51

    # as long as the mouse hasn't been clicked on the window
    # keep on looping.
    while win.checkMouse() == None:

        # move the circle in the current direction.
        circ.move( dx, dy )

        # get the x and y of the center of the circle.
        x = circ.getCenter().getX()
        y = circ.getCenter().getY()

        # if the center of the ball is outside the right or left boundary,
        # reverse the x direction of movement.
        if  x > 600 - radius or  x < radius: 
            dx = -dx
        if  y > 400 - radius or y < radius:
            dy = -dy

        # if the center of the ball is inside the first obstacle, stop
        # the ball.
        if  isInside( x, y, x1, y1, x2, y2 ):
            dx = 0
            dy = 0

        # if the center of the ball is inside the second obstacle, bounce
        # off
        if  isInside( x, y, x3, y3, x4, y4 ):
            dx = -dx
            dy = -dy

    
    # if we're here, it's because the the user clicked on the graphic window.
    # we can close everything and quit.
    win.close()    

main()


demo7.py


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

# ifInside: returns true of the coordinates of a point defined by 
# its coordinates ballX and ballY, are inside the rectangle defined by a top
# left point of coordinates obsX1, obsY1, and a bottom-right point
# with coordinates obsX2, obsY2.   Returns false if the point is outside
# the rectangle.
def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ):
    if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2:
        return True
    else:
        return False

def isLeftSide( x, y, width ):
    if x < width//2:
        return True
    else:
        return False
    
def main():
    win = GraphWin( "Lab 7 Moving ball", 600, 400 )

    # create a green obstacle in the middle of the window
    x1 = 200
    y1 = 200
    x2 = 250
    y2 = 250 
    obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    obstacle1.setFill( "green" )
    obstacle1.draw( win )

    # create another green rectangle on the right of the first one
    x3 = 350
    y3 = 200
    x4 = 400
    y4 = 250 
    obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
    obstacle2.setFill( "magenta" )
    obstacle2.draw( win )
    
    # create and draw a red circle
    radius = 30
    center = Point( 100, 100 )
    circ = Circle( center, radius )
    circ.setFill( 'red' )
    circ.draw( win )

    # define the direction the circle will start moving in.
    # 5 pixels to the right every move, and 0.25 pixels down
    # every move.
    dx = 5.111
    dy = -2.51

    # as long as the mouse hasn't been clicked on the window
    # keep on looping.
    while win.checkMouse() == None:

        # move the circle in the current direction.
        circ.move( dx, dy )

        # get the x and y of the center of the circle.
        x = circ.getCenter().getX()
        y = circ.getCenter().getY()

        # if the center of the ball is outside the right or left boundary,
        # reverse the x direction of movement.
        if  x > 600 - radius or  x < radius: 
            dx = -dx
        if  y > 400 - radius or y < radius:
            dy = -dy

        # if the center of the ball is inside the first obstacle, stop
        # the ball.
        if  isInside( x, y, x1, y1, x2, y2 ):
            dx = 0
            dy = 0

        # if the center of the ball is inside the second obstacle, bounce
        # off
        if  isInside( x, y, x3, y3, x4, y4 ):
            dx = -dx
            dy = -dy

        # if the ball is on the left side of the window, its color is
        # red, else its color is yellow.
        if isLeftSide( x, y, win.getWidth() )==True:
            circ.setFill( 'red' )
        else:
            circ.setFill( 'yellow' )
            
    
    # if we're here, it's because the the user clicked on the graphic window.
    # we can close everything and quit.
    win.close()    

main()



demo8.py


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

# ifInside: returns true of the coordinates of a point defined by 
# its coordinates ballX and ballY, are inside the rectangle defined by a top
# left point of coordinates obsX1, obsY1, and a bottom-right point
# with coordinates obsX2, obsY2.   Returns false if the point is outside
# the rectangle.
def isInside( ballX, ballY, obsX1, obsY1, obsX2, obsY2 ):
    if obsX1 < ballX < obsX2 and obsY1 < ballY < obsY2:
        return True
    else:
        return False

def isLeftSide( x, y, width ):
    if x < width//2:
        return True
    else:
        return False

def getColor( x, y, w, h ):
    if x < w/2 and y < h/2:
        return 'yellow'
    elif x < w/2 and y > h/2:
        return 'blue'
    elif x > w/2 and y < h/2:
        return 'red'
    else:
        return 'brown'
        
def main():
    win = GraphWin( "Lab 7 Moving ball", 600, 400 )

    # create a green obstacle in the middle of the window
    x1 = 200
    y1 = 200
    x2 = 250
    y2 = 250 
    obstacle1 = Rectangle( Point( x1, y1 ), Point( x2, y2 ) )
    obstacle1.setFill( "green" )
    obstacle1.draw( win )

    # create another green rectangle on the right of the first one
    x3 = 350
    y3 = 200
    x4 = 400
    y4 = 250 
    obstacle2 = Rectangle( Point( x3, y3 ), Point( x4, y4 ) )
    obstacle2.setFill( "magenta" )
    obstacle2.draw( win )
    
    # create and draw a red circle
    radius = 30
    center = Point( 100, 100 )
    circ = Circle( center, radius )
    circ.setFill( 'red' )
    circ.draw( win )

    # define the direction the circle will start moving in.
    # 5 pixels to the right every move, and 0.25 pixels down
    # every move.
    dx = 5.111
    dy = -2.51

    # as long as the mouse hasn't been clicked on the window
    # keep on looping.
    while win.checkMouse() == None:

        # move the circle in the current direction.
        circ.move( dx, dy )

        # get the x and y of the center of the circle.
        x = circ.getCenter().getX()
        y = circ.getCenter().getY()

        circ.setFill( getColor( x, y, win.getWidth(), win.getHeight() ) )
                   
        # if the center of the ball is outside the right or left boundary,
        # reverse the x direction of movement.
        if  x > 600 - radius or  x < radius: 
            dx = -dx
        if  y > 400 - radius or y < radius:
            dy = -dy

        # if the center of the ball is inside the first obstacle, stop
        # the ball.
        if  isInside( x, y, x1, y1, x2, y2 ):
            dx = 0
            dy = 0

        # if the center of the ball is inside the second obstacle, bounce
        # off
        if  isInside( x, y, x3, y3, x4, y4 ):
            dx = -dx
            dy = -dy

        # if the ball is on the left side of the window, its color is
        # red, else its color is yellow.
        """
        if isLeftSide( x, y, win.getWidth() )==True:
            circ.setFill( 'red' )
        else:
            circ.setFill( 'yellow' )
        """ 
    
    # if we're here, it's because the the user clicked on the graphic window.
    # we can close everything and quit.
    win.close()    

main()


Eliza


# eliza.py
#
# This program was developed in class. It is poorly documented,
# and is provided here only to help with the note-taking.

import random



def cannedAnswer():
    """display a random canned answer.
    """
    canned = [ "\nPlease tell me more",
           "\nI see...",
           "\nI am listening..." ]
    print( random.choice( canned ) )

def cannedNegative():
    """display statement picking up on negativity
    """
    canned = [ "My, my, so negative today!",
               "Really?", "You should be more positive",
               "Maybe you should take a break!" ]
    print( random.choice( canned ) )

def cannedFamily( member ):
    """display a random statement about the user's family,
        or the particular member the user listed in her answer"""

    canned = [ "How do you feel about your " + member,
               "is that always the case in your family",
               "tell me more about your "+member ]

    print( random.choice( canned ) )
           
def main():
 
     # greet user
     print(  "Welcome.  Please tell me of your problems: " )
     print( '(You may quit at any time by answering "bye")' )
 
     # repeat a huge number of times
     for i in range( 100000 ):
 
         # get user's statement about her life
         answer = input( "\n> " )
         if ( answer.lower() == "bye" ):
             break

         # negative answer
         neg = [ "no", "never" ]
         if answer.lower() in neg:
             cannedNegative()
             continue

         # family member
         family = ["mother", "father", "sister", "brother", "dog" ]
         found = 0
         for member in family:
             if answer.find( member ) != -1:
                 found = 1
                 break
         if found != 0:
             cannedFamily( member )
             continue

         # reflection
         # split the answer into a list of words
         words = answer.split()

         # create a new list with the same words
         newWords = []
         for word in words:
             newWords.append( word )

         # go through all the words in the original list
         # and reflect them in the new list.
         for i in range( len( words ) ):
             if words[i] == "I":
                 newWords[i] = "you"
             if words[i] == "you":
                 newWords[i] = "me"

         # if the two lists of words are different, we must have
         # reflected some words...
         if words != newWords:
             print( ' ' . join( newWords ) + '?')
             continue
            
         # respond semi-intelligently if we couldn't detect anything 
         # relating to negativity, family, or reflection
         cannedAnswer()

     # we get here when the user quits.
     print( "it was nice chatting with you" )
     
main()

</showafterdate>