CSC111 Lab 8 2018

From dftwiki3
Revision as of 13:48, 25 March 2018 by Thiebaut (talk | contribs) (getColor() Function)
Jump to: navigation, search

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


This lab continues on with the lab from last week. You will need to add obstacles in the graphic window, and make your circle bounce on obstacles, or get stuck to obstacles.



Adding an Obstacle


  • Take your graphic program from last week, where you made a colored circle move around the graphic window, bouncing off the sides.
  • Add a rectangle in the middle of the graphic window. You should declare it at the beginning of your main() function, as illustrated here:


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

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

    # the remaining part of your code follows below...



Challenge 1

QuestionMark3.jpg


  • Make your ball stop when its center enters the green rectangle. The image below illustrates this situation.


BallStoppedOnGreenRect.png



  • Hints: to make an object stop, you can simply set its displacement to 0, so that the while-loop makes it move by 0 pixels every loop.



Challenge 2

QuestionMark4.jpg


  • Modify your program one more time, and this time the ball will bounce off the obstacle. When you detect that the center of the ball is inside the green rectangle, change dx and dy to their opposite. Note that this will make the ball bounce back on the same path it came from. It is quite challenging to make the ball bounce in a realistic way, and you are welcome to try to make it happen, but it's trickier. Bouncing off in the opposite direction is fine for this lab!


if  ... :
    dx = -dx
    dy = -dy







Adding a Second Obstacle


  • Add a second rectangle in the graphic window. Make it magenta in color. You should declare it at the beginning of your main() function, as illustrated here:


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 )


    # the remaining part of your code follows below...


  • Feel freel to position it at a different location than the one used above.


Challenge 3

QuestionMark5.jpg


  • Make your ball stop when its center is fully inside the first green obstacle, and bounce off the magenta obstacle. For this challenge, simply make the ball bounce off the second obstacle when its center enters the obstacle.
  • If you find that the ball never gets to hit both boxes, you may want to change the initial direction and add decimal digits to the dx and dy to create an angle that yields a path that eventually will hit the obstacles. For example:


    dx = 5.111
    dy = -2.51






BallWithGreenAndMagentaRects.png




Using Boolean Functions


isInside()


  • Add the code of the function isInside(), shown below, to your last program, above the main() function.


# 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


  • Modify the code inside the while loop in your main function, so that it uses the isInside() function to detect contact with Obstacle1 and with Obstacle2.
Here's what your code should look like (you will have to pass the appropriate parameters to the function):


        # if the center of the ball is inside the first obstacle, stop
        # the ball.
        if  isInside(  ...  ) == True:
            dx = 0
            dy = 0

        # if the center of the ball is inside the second obstacle, bounce
        # off
        if  isInside(  ...  ) == True:
            dx = -dx
            dy = -dy



IsLeftSide() Boolean Function


  • Add a new boolean function to your program called isLeftSide(). This function receives 3 parameters, in this order:


  • an x coordinate
  • a y coordinate
  • the width of the window.


and it returns true if the point (x, y) is on the left side of the window, and false if the point (x, y) is on the right side of the window. This function basically compares the x coordinate to the midpoint on the x axis of the window.


  • Use your new function to color the ball rid when it is moving on the left side of the window, and yellow when it is moving on the right side of the window. Example:


if isLeftSide(  x,  y, win.getWidth() ) == True:
      circ.setFill( 'red' )
else:
      circ.setFill( 'yellow' )


isUpperHalf Boolean Function


  • Comment out or remove the previous if statement that changes the color of the ball, so that it won't interfere with the code you are about to write.
  • Create a new boolean function called isUpperHalf(), that takes 3 parameters, the first two being the coordinates of a point, the third one the height of the window, and that returns true if the point is in the top half of the screen, an false otherwise.


  • Use your new function to control the color of the ball as it moves around the window. The ball should be 'blue' when it is in the upper half of the screen, and 'brown' when it is in the lower half.




Demonstrate the correct operations of your programs to your Lab Instructor, and you will be instructed on the process of submitting your lab to Moodle.

getColor() Function


  • Your assignment is to create a new function called getColor(). This function should receive the x and y coordinate of the center of the ball, along with the width and height of the window. It will return a string which is the name of a color. If the x and y coordinates passed to the function are inside the top left quarter of the window, the function will return the string 'yellow'. If the x and y coordinates are inside the top right quarter, the function will return the string 'red'. For the lower-left quarter, the function will return 'blue'. And finally, the function will return 'brown' for the bottom-right quarter.
  • Here is an example of how the function can be used inside the while loop:


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

# set color of circle according to its position
color = getColor( x, y, win.getWidth(), win.getHeight() )
circ.setFill( color )