# ChapterOO/car.py """ONE wheel moving on the canvas stops when reach border """ from graphics import * winW, winH = 400, 400 # window geometry class Wheel: # 2 concentric circles: big circle & small circle def __init__( self, c, rs, rb ): # constructor: c= center,rs = small radius, rb=big radius self.bCircle = Circle( c, rb ) self.sCircle = Circle( c, rs ) def move( self, dx, dy ): self.bCircle.move( dx, dy ) self.sCircle.move( dx, dy ) def setFill( self, colors, colorb ): self.bCircle.setFill( colorb ) self.sCircle.setFill( colors ) def draw( self, win ): self.bCircle.draw( win ) self.sCircle.draw( win ) def getCenter( self ): return self.bCircle.getCenter() def getRadius( self ): return self.bCircle.getRadius() class Car: def __init__( self, c, rs, rb ): # constructor: c=center, rs=small radius, rb=big radius self.w1 = Wheel( c, rs, rb ) c2 = Point(c.getX()+ 3 *rb,c.getY()) self.w2 = Wheel( c2, rs, rb ) self.rect = Rectangle(Point(c.getX(),c.getY()-2*rb),c2) def move( self, dx, dy ): self.w1.move( dx, dy ) self.w2.move( dx, dy ) self.rect.move(dx,dy) def setFill( self, colors, colorb ): self.w1.setFill( colors, colorb ) self.w2.setFill( colors,colorb ) self.rect.setFill("pink") def draw( self, win ): self.w1.draw( win ) self.w2.draw( win ) self.rect.draw(win) def getCenter( self ): return self.w2.bCircle.getCenter() def getRadius( self ): return self.w2.bCircle.getRadius() def main(): win = GraphWin( "Demo: Car moving", winW, winH ) cc1 = Car( Point( 50, 30 ), 10, 20 ) cc1.setFill( "black", "yellow" ) cc1.draw( win ) while True: cc1.move( 0.1, 0 ) if cc1.getCenter().getX() > winW - cc1.getRadius(): break main()