Difference between revisions of "CSC111 Lab 12 2011"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <bluebox>This lab will have you play with classes and objects: the essence of ''Object Oriented Programming''!</bluebox> <br /> =Part 1: A Bus= <center>[[Image:CS...")
 
(Part 1: A Bus)
Line 11: Line 11:
 
<br />
 
<br />
 
* Using the example we did in class on Tuesday (see code below), create a new program that draws the bus shown above, and makes it disappear from the window by moving to the left.
 
* Using the example we did in class on Tuesday (see code below), create a new program that draws the bus shown above, and makes it disappear from the window by moving to the left.
 +
* Your program should have two classes:
 +
** '''class Wheel'''
 +
** '''class Bus'''
 +
<br />
 +
<br />
 +
<source lang="python">
 +
# A program with a car and a wheel class.
 +
# This is slightly different from what we did in class, but works
 +
# the same.  Figure out what the differences are.
 +
#
 +
from graphics import *
 +
W = 800
 +
H = 600
 +
 +
#----------------------------------------------------------------
 +
class Wheel:
 +
    """A class with two concentric circles"""
 +
 +
    def __init__( self, center, r1, r2 ):
 +
        self.circ1 = Circle( center, r1 )
 +
        self.circ2 = Circle( center, r2 )
 +
        r1, r2 = min( r1, r2 ), max( r1, r2 )
 +
        self.radius1 = r1
 +
        self.radius2 = r2
 +
 +
    def draw( self, win ):
 +
        """draws the wheel on the window"""
 +
        self.circ2.draw( win )
 +
        self.circ1.draw( win )
 +
 +
    def setFill( self, color1, color2 ):
 +
        """colors the outside and inside circles.  First color for small circle"""
 +
        self.circ1.setFill( color1 )
 +
        self.circ2.setFill( color2 )
 +
 +
    def getRadius1( self ):
 +
        """returns radius of small circle"""
 +
        return self.radius1
 +
 +
    def getRadius2( self ):
 +
        """returns radius of large circle"""
 +
        return self.radius2
 +
 +
#----------------------------------------------------------------
 +
class Car:
 +
    """A class for a car, which is a rectangle and two wheels"""
  
<source lang="python">
+
    def __init__( self, P1, P2 ):
 +
        """Get 2 points and create the box and two wheels from the dimensions"""
 +
        self.P1 = P1
 +
        self.P2 = P2
 +
        self.width = abs( P1.getX()-P2.getX() )
 +
        self.height= abs( P1.getY()-P2.getY() )
 +
 +
        self.rect    = Rectangle( P1, P2 )
 +
        center1      = Point( P1.getX()+self.width/8, P2.getY() )
 +
        center2      = Point( P1.getX()+self.width*7/8, P2.getY() )
 +
        radius2      = self.height/3
 +
        radius1      = radius2/2
 +
        self.wheel1  = Wheel( center1, radius1, radius2 )
 +
        self.wheel2  = Wheel( center2, radius1, radius2 )
 +
 +
    def draw( self, win ):
 +
        """draws the car on the screen.  One of the wheels is covered"""
 +
        self.wheel2.draw( win )
 +
        self.rect.draw( win )
 +
        self.wheel1.draw( win )
  
 +
    def move( self, dx, dy ):
 +
        """moves the car by dx and dy"""
 +
        self.wheel2.move( dx, dy )
 +
        self.rect.move( dx, dy )
 +
        self.wheel1.move( dx, dy )
 +
 +
#----------------------------------------------------------------
 +
def waitForClick( win, message ):
 +
    """ waitForClick: stops the GUI and displays a message. 
 +
    Returns when the user clicks the window. The message is erased."""
 +
 +
    # wait for user to click mouse to start
 +
    startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message )
 +
    startMsg.draw( win )    # display message
 +
    win.getMouse()          # wait
 +
    startMsg.undraw()      # erase
 +
 +
 +
def main():
 +
    global W, H
 +
    win = GraphWin( "wheel demo", W, H )
 +
 +
    waitForClick( win, "click to start" )
 +
    car = Car( Point( 100,100 ), Point( 250, 170 ) )
 +
 +
    car.draw( win )
 +
    waitForClick( win, "click to end" )
 +
 +
main()
 
</source>
 
</source>
  

Revision as of 10:18, 30 November 2011

--D. Thiebaut 09:42, 30 November 2011 (EST)


This lab will have you play with classes and objects: the essence of Object Oriented Programming!


Part 1: A Bus

CSC111 bus.png


  • Using the example we did in class on Tuesday (see code below), create a new program that draws the bus shown above, and makes it disappear from the window by moving to the left.
  • Your program should have two classes:
    • class Wheel
    • class Bus



# A program with a car and a wheel class.
# This is slightly different from what we did in class, but works
# the same.  Figure out what the differences are.
#
from graphics import *
W = 800
H = 600
 
#----------------------------------------------------------------
class Wheel:
    """A class with two concentric circles"""
 
    def __init__( self, center, r1, r2 ):
        self.circ1 = Circle( center, r1 )
        self.circ2 = Circle( center, r2 )
        r1, r2 = min( r1, r2 ), max( r1, r2 )
        self.radius1 = r1
        self.radius2 = r2
 
    def draw( self, win ):
        """draws the wheel on the window"""
        self.circ2.draw( win )
        self.circ1.draw( win )
 
    def setFill( self, color1, color2 ):
        """colors the outside and inside circles.  First color for small circle"""
        self.circ1.setFill( color1 )
        self.circ2.setFill( color2 )
 
    def getRadius1( self ):
        """returns radius of small circle"""
        return self.radius1
 
    def getRadius2( self ):
        """returns radius of large circle"""
        return self.radius2
 
#----------------------------------------------------------------
class Car:
    """A class for a car, which is a rectangle and two wheels"""

    def __init__( self, P1, P2 ):
        """Get 2 points and create the box and two wheels from the dimensions"""
        self.P1 = P1
        self.P2 = P2
        self.width = abs( P1.getX()-P2.getX() )
        self.height= abs( P1.getY()-P2.getY() )
 
        self.rect    = Rectangle( P1, P2 )
        center1      = Point( P1.getX()+self.width/8, P2.getY() )
        center2      = Point( P1.getX()+self.width*7/8, P2.getY() )
        radius2      = self.height/3
        radius1      = radius2/2
        self.wheel1  = Wheel( center1, radius1, radius2 )
        self.wheel2  = Wheel( center2, radius1, radius2 )
 
    def draw( self, win ):
        """draws the car on the screen.  One of the wheels is covered"""
        self.wheel2.draw( win )
        self.rect.draw( win )
        self.wheel1.draw( win )

    def move( self, dx, dy ):
        """moves the car by dx and dy"""
        self.wheel2.move( dx, dy )
        self.rect.move( dx, dy )
        self.wheel1.move( dx, dy ) 
 
#----------------------------------------------------------------
def waitForClick( win, message ):
    """ waitForClick: stops the GUI and displays a message.  
    Returns when the user clicks the window. The message is erased."""
 
    # wait for user to click mouse to start
    startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message )
    startMsg.draw( win )    # display message
    win.getMouse()          # wait
    startMsg.undraw()       # erase
 
 
def main():
    global W, H
    win = GraphWin( "wheel demo", W, H )
 
    waitForClick( win, "click to start" )
    car = Car( Point( 100,100 ), Point( 250, 170 ) )
 
    car.draw( win )
    waitForClick( win, "click to end" )
 
main()