CSC111 Car and Trees program

From dftwiki3
Revision as of 14:24, 1 December 2011 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 11:49, 1 December 2011 (EST)


from graphics import *
import random
W = 800
H = 600
 
#----------------------------------------------------------------
class Wheel:
    """A class with two concentric circles"""
 
    def __init__( self, center, r1, r2 ):
        """create the two wheels.  Make sure r1 is the smallest radius of the two"""
        r1, r2 = min( r1, r2 ), max( r1, r2 )
        self.circ1 = Circle( center, r1 )
        self.circ2 = Circle( center, r2 )
        self.radius1 = r1
        self.radius2 = r2

    def setFill( self, col1, col2 ):
        """set the colors. col1 is the color of the smaller of the two radii"""
        self.circ1.setFill( col1 )
        self.circ2.setFill( col2 )
        
    def draw( self, win ):
        """draw the two circles on the window"""
        self.circ2.draw( win )
        self.circ1.draw( win )
 
    def getRadius1( self ):
        return self.radius1
 
    def getRadius2( self ):
        return self.radius2

    def DTmove( self, dx, dy ):
        """moves the 2 circles by dx and dy"""
        self.circ1.move( dx, dy )
        self.circ2.move( dx, dy )

class Car:
    """A class for a car, made of 1 rectangle and 2 wheels"""

    def __init__( self, p1, p2, width ):
        """create the car, with 1 rectangle, 2 wheels.  Store width for
        future operations with the move method"""
        self.width = width
        self.rect = Rectangle( p1, p2 )
        length  = abs( p1.getX()-p2.getX() )
        xWheel1 = p1.getX() + length/4
        yWheel1 = p2.getY()
        xWheel2 = p2.getX() - length/4
        yWheel2 = yWheel1
        
        self.w1   = Wheel( Point( xWheel1, yWheel1 ), length/16, length/8 )
        self.w2   = Wheel( Point( xWheel2, yWheel2 ), length/16, length/8 )

    def draw( self, win ):
        """draws the wheels and the rectangle"""
        self.w1.draw( win )
        self.rect.draw( win )
        self.w2.draw( win )

    def setFill( self, col1, col2, col3 ):
        """colors the car. col1 is the body color. col2 the inner color, 
        col3 with tire color"""
        self.rect.setFill( col1 )
        self.w1.setFill( col2, col3 )
        self.w2.setFill( col2, col3 )

    def DTmove( self, dx, dy ):
        """moves the car.  If it goes off the window, make it
        reappear on the other side"""

        self.rect.move( dx, dy )
        self.w1.DTmove( dx, dy )
        self.w2.DTmove( dx, dy )
        x = self.rect.getP1().getX()

        if x > self.width:
            self.rect.move( -self.width, 0 )
            self.w1.DTmove( -self.width, 0 )
            self.w2.DTmove( -self.width, 0 )
            
class Tree:
    """class for a tree, made of a trunk and a head of leaves"""

    def __init__( self, point, height, width ):
        """The tree is defined by the point at the base of its trunk,
        and its height.  The width of the window is passed for later
        use by the move method"""

        self.width = width
        x = point.getX()
        y = point.getY()
        p1 = Point( x-20, y )
        p2 = Point( x+20, y - height * 2 /3 )
        self.trunk = Rectangle( p1, p2 )
        self.trunk.setFill( "brown" )
        self.head = Circle( Point( x, y - height * 5/ 6 ), height/6 )
        self.head.setFill( "green" )

    def draw( self, win ):
        """draws the trunk and head of the tree"""
        self.trunk.draw( win )
        self.head.draw( win )

    def DTmove( self, dx, dy ):
        """moves the tree.  If the tree moves off to the left of the
        window, it reappears on the right-hand side"""
        self.trunk.move( dx, dy )
        self.head.move( dx, dy )
        x = self.trunk.getP1().getX()
        if x < 0:
            self.trunk.move( self.width, 0 )
            self.head.move( self.width, 0 )
        
#----------------------------------------------------------------
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 )

    # create a list of trees
    trees = []
    for i in range( 4 ):
        tree = Tree( Point( random.randrange(10,W), 550 ), 400, W )
        tree.draw( win )
        trees.append( tree )
 
    # create the car
    car = Car( Point( 50,450 ), Point( 250, 550 ), W )
    car.setFill( "yellow", "white", "black" )
    car.draw( win )

    # animation loop
    while True:
        for tree in trees:
            tree.DTmove( -3, 0 )
        
        if win.checkMouse() != None:
            break
        
    waitForClick( win, "click to end" )
    win.close()
 
main()