CSC111 Lab 11 2018

From dftwiki3
Revision as of 10:00, 15 April 2018 by Thiebaut (talk | contribs) (Main Module)
Jump to: navigation, search

D. Thiebaut (talk) 10:44, 15 April 2018 (EDT)


Problem 1: Class Inheritance in a Graphic Context


GenericCar1.png


GenericCar Class


This class will be our super class. Create a new program called genericCar.py with the code below:

# genericCar.py
# Your name here
#
# A module containing the definition for a graphic car with
# a rectangular body and two wheels.
from graphics import *
from random import *

class GenericCar:
    """Definition for a car with a body and two wheels"""

    def __init__(self, win, topLeft, width, height ):
        """constructs a car made of 1 rectangle with top-left
        point topLeft, dimension width x height, and two wheels
        away from left and right by 10 pixesl"""
        # save width and height of car
        self.width = width
        self.height = height
        
        # create bottom-right point
        x1 = topLeft.getX()
        y1 = topLeft.getY()
        P2 = Point( x1+width, y1+height )

        # body is a rectangle between topLeft and P2
        self.body = Rectangle( topLeft, P2 )
        self.body.setFill( "yellow" )

        # create wheel #1
        center1 = Point( x1+20, y1+height )
        self.wheel1 = Circle( center1, 20 )
        self.wheel1.setFill( "black" )

        # create wheel #2
        center2 = Point( x1+width-20, y1+height )
        self.wheel2 = Circle( center2, 20 )
        self.wheel2.setFill( "black" )

        # create random speed
        self.dx = randrange( -3, 3 )

        # save window width (so that a car can detect
        # that it's going outside the left or right
        # margins)
        self.windowWidth = win.getWidth()

    def setSpeed( self, sp ):
        '''sets the horizontal speed to sp.  Positive values will make
        the car go to the right.  Negative, to the left.'''
        self.dx = sp
        
    def setFill( self, color ):
        '''sets the color of the body of the car'''
        self.body.setFill( color )
        
    def draw( self, win ):
        """draw the car on the window"""
        self.body.draw( win )
        self.wheel1.draw( win )
        self.wheel2.draw( win )
            
    def move( self ):
        """move the body and wheels of the car by dx"""
        self.body.move( self.dx, 0 )
        self.wheel1.move( self.dx, 0 )
        self.wheel2.move( self.dx, 0 )


Main Module


Create another Python program called manyCars.py that will import the GenericCar module, and create a generic car:

# manyCars.py
# A program that use a generic car instantiated from
# the GenericCar class.

from genericCar import *
from graphics import *
WIDTH  = 700
HEIGHT = 500

def main():
    # open a graphic window
    win = GraphWin( "Cars Cars Cars", WIDTH, HEIGHT )

    # create a generic car, draw it, set its speed
    # and set its color to blue
    car = GenericCar( win, Point( 100, 100 ), 200, 50 )
    car.draw( win )
    car.setSpeed( -1.5 )
    car.setFill( "blue" )

    # keep on moving the car until the user clicks the mouse
    while win.checkMouse()==None:
        car.move( )

    # close the graphic window        
    win.close()

main()


  • Save your files in the same directory.
  • Run manyCars.py. Make sure your blue car moves out of the window. If your car is too fast, change the speed to 0.15.
  • Create a second generic car, at a different location in the window, with a different color and a positive speed.
  • Make sure your program still runs correctly.