class Car: # Car class definition def __init__( self, back_wheel_pos, back_wheel_rad, front_wheel_pos, front_wheel_rad, car_height ): # constructor method to instantiate self.back_wheel = Wheel(back_wheel_pos, back_wheel_rad, back_wheel_rad + 5) # a class object self.front_wheel = Wheel( front_wheel_pos, front_wheel_rad, front_wheel_rad + 5 ) # attributes of the Car class are defined self.car_body = Rectangle( Point(back_wheel_pos.getX(), back_wheel_pos.getY() - car_height), front_wheel_pos # and initialized ) self.car_color = "" self.Road_1 = Line(Point(0, 100), Point(1200, 100)) self.Road_2 = Line(Point(0, 40), Point(1200, 40)) def carMove(self, dx, dy): # function to effect motion of the car object self.back_wheel.move(dx, dy) # by individually moving each item self.front_wheel.move(dx, dy) self.car_body.move(dx, dy) def setColour(self, tireColour, wheelColour, carColour): # function to set the colour attribute of each item self.back_wheel.set_color(wheelColour, tireColour) self.front_wheel.set_color(wheelColour, tireColour) self.car_body.setFill(carColour) def animate( self, win, dx, dy, n ): # function to perform the animation. This function is looped by the 'after' function until n = 0 if n > 0: self.carMove(dx, dy) win.after(100, self.animate, win, dx, dy, n - 1) def draw( self, win ): # draw the car object by drawing each individual item. The last item drawn appears on the foreground self.car_body.draw(win) self.back_wheel.draw(win) self.front_wheel.draw(win)
class Car(): WHEEL_TO_TIRE_RATIO = 0.6 def __init__(self, back_wheel_center, back_tire_radius, front_wheel_center, front_tire_radius, body_height): upper_left_point = Point(back_wheel_center.x, back_wheel_center.y - body_height) bottom_right_point = front_wheel_center self.body = Rectangle(upper_left_point, bottom_right_point) self.back_wheel = Wheel(back_wheel_center, back_tire_radius * Car.WHEEL_TO_TIRE_RATIO, back_tire_radius) self.front_wheel = Wheel(front_wheel_center, front_tire_radius * Car.WHEEL_TO_TIRE_RATIO, front_tire_radius) def set_color(self, tire_color, wheel_color, body_color): self.body.setFill(body_color) self.back_wheel.set_color(wheel_color, tire_color) self.front_wheel.set_color(wheel_color, tire_color) def draw(self, win): self.body.draw(win) self.back_wheel.draw(win) self.front_wheel.draw(win) def move(self, dx, dy): self.back_wheel.move(dx, dy) self.front_wheel.move(dx, dy) self.body.move(dx, dy) def animate(self, win, dx, dy, n): if n > 0: self.move(dx, dy) win.after(10, self.animate, win, dx, dy, n - 1)
class Car(): def __init__(self, wheel_point1, wheel_radius1, wheel_point2, wheel_radius2, height): self.wheel1 = Wheel(wheel_point1, wheel_radius1 * 0.6, wheel_radius1) self.wheel2 = Wheel(wheel_point2, wheel_radius2 * 0.6, wheel_radius2) # TODO - Get the point coordinates to get the corners self.body = Rectangle( Point(wheel_point1.getX(), wheel_point1.getY() - height), Point(wheel_point2.getX(), wheel_point2.getY())) def draw(self, win): self.wheel1.draw(win) self.wheel2.draw(win) self.body.draw(win) def move(self, dx, dy): self.wheel1.move(dx, dy) self.wheel2.move(dx, dy) self.body.move(dx, dy) def set_color(self, tire_color, wheel_color, body_color): # TODO - Set the colors self.wheel1.set_color(wheel_color, tire_color) self.wheel2.set_color(wheel_color, tire_color) self.body.setFill(body_color) def undraw(self): self.wheel1.undraw() self.wheel2.undraw() self.body.undraw() # def get_size(self): # return self.tire_circle.getRadius() # # def get_center(self): # return self.tire_circle.getCenter() def animate(self, win, dx, dy, n): # TODO - Move the body and the wheels if n > 0: self.wheel1.move(dx, dy) self.wheel2.move(dx, dy) self.body.move(dx, dy) win.after(50, self.animate, win, dx, dy, n - 1)