CSC111 Lab 11 2018
D. Thiebaut (talk) 10:44, 15 April 2018 (EDT)
Problem 1: Class Inheritance in a Graphic Context
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