CSC111 newCarLab10.py
--D. Thiebaut 13:46, 14 April 2010 (UTC)
# newCarLab10.py
# D. Thiebaut
# Another example of inheritance
from carLab10 import *
from graphics import *
class NewCar( Car ):
def __init__( self, P1, P2, dx, label="1" ):
Car.__init__( self, P1, P2, dx )
xc = (P1.getX() + P2.getX())/2
yc = (P1.getY() + P2.getY())/2
d = min( abs( P1.getX()-P2.getX() ),
abs( P1.getY()-P2.getY() ) )
self.labelCircle = Circle( Point( xc, yc ), d/2 )
self.label = Text( Point( xc, yc ), label )
Car.setFill( self, "red", "black", "grey" )
self.moveCount = 10
def draw( self, win ):
Car.draw( self, win )
self.labelCircle.draw( win )
self.label.draw( win )
def move( self, dx, dy ):
Car.move( self, dx, dy )
self.label.move( self.dx, dy )
self.labelCircle.move( self.dx, dy )
self.moveCount += 1
if self.moveCount % 10 == 0:
self.dx = -self.dx
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():
"""displays the graphics window and an instance of myRect"""
win = GraphWin( "Inheritance", 400, 400 )
c = NewCar( Point( 20,120 ), Point( 150, 150 ), 3, "10" )
c.draw( win )
waitForClick( win, "Click to move" )
for i in range( 100 ):
c.move( 3, 0 )
waitForClick( win, "Click to quit" )
main()