CSC111 A class for a simple car
--D. Thiebaut 12:51, 5 April 2010 (UTC)
wheel1.py
# wheel1.py
# first program using a class to define
# a new graphic object.
from graphics import *
W = 400
H = 400
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.radius1 = r1
self.radius2 = r2
def draw( self, win ):
self.circ2.draw( win )
self.circ1.draw( win )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
#----------------------------------------------------------------
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():
global W, H
win = GraphWin( "wheel demo", W, H )
waitForClick( win, "click to start" )
w = Wheel( Point( W/2, H/2 ), 20, 40 )
w.draw( win )
waitForClick( win, "click to end" )
main()
wheel2.py
# wheel2.py
# Same program as wheel1.py, but this time we add
# a method to define the color of the wheel.
from graphics import *
W = 400
H = 400
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.radius1 = r1
self.radius2 = r2
def draw( self, win ):
self.circ2.draw( win )
self.circ1.draw( win )
def setFill( self, color1, color2 ):
self.circ1.setFill( color1 )
self.circ2.setFill( color2 )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
#----------------------------------------------------------------
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():
global W, H
win = GraphWin( "wheel demo", W, H )
waitForClick( win, "click to start" )
w = Wheel( Point( W/2, H/2 ), 20, 40 )
w.draw( win )
w.setFill( "yellow", "black" )
waitForClick( win, "click to end" )
main()
wheel3.py
# wheel3.py
# A new method is added to wheel2.py so that the
# wheel can now move with a dx, dy speed and
# direction.
from graphics import *
W = 400
H = 400
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.radius1 = r1
self.radius2 = r2
def draw( self, win ):
self.circ2.draw( win )
self.circ1.draw( win )
def setFill( self, color1, color2 ):
self.circ1.setFill( color1 )
self.circ2.setFill( color2 )
def move( self, dx, dy ):
self.circ1.move( dx, dy )
self.circ2.move( dx, dy )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
#----------------------------------------------------------------
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():
global W, H
win = GraphWin( "wheel demo", W, H )
waitForClick( win, "click to start" )
w = Wheel( Point( W/2, H/2 ), 20, 40 )
w.draw( win )
w.setFill( "yellow", "black" )
waitForClick( win, "click to move" )
for i in range( 300 ):
w.move( 5, 3 )
waitForClick( win, "click to end" )
win.close()
main()
car1.py
See last version of car.py for full documentation.
# car1.py
# We add a rectangle and two wheels to create a new class for a
# car!
from graphics import *
W = 400
H = 400
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.radius1 = r1
self.radius2 = r2
def draw( self, win ):
self.circ2.draw( win )
self.circ1.draw( win )
def setFill( self, color1, color2 ):
self.circ1.setFill( color1 )
self.circ2.setFill( color2 )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
#----------------------------------------------------------------
class Car:
def __init__( self, P1, P2 ):
self.P1 = P1
self.P2 = P2
self.width = abs( P1.getX()-P2.getX() )
self.height= abs( P1.getY()-P2.getY() )
self.rect = Rectangle( P1, P2 )
center1 = Point( P1.getX()+self.width/8, P2.getY() )
center2 = Point( P1.getX()+self.width*7/8, P2.getY() )
radius2 = self.height/3
radius1 = radius2/2
self.wheel1 = Wheel( center1, radius1, radius2 )
self.wheel2 = Wheel( center2, radius1, radius2 )
def draw( self, win ):
self.rect.draw( win )
self.wheel1.draw( win )
self.wheel2.draw( win )
#----------------------------------------------------------------
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():
global W, H
win = GraphWin( "wheel demo", W, H )
waitForClick( win, "click to start" )
car = Car( Point( 100,100 ), Point( 250, 170 ) )
car.draw( win )
waitForClick( win, "click to end" )
main()
car2.py
# car2.py
# Same as car1, but we add a method to define the color of
# the car, including the wheels
#
from graphics import *
W = 400
H = 400
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.radius1 = r1
self.radius2 = r2
def draw( self, win ):
self.circ2.draw( win )
self.circ1.draw( win )
def setFill( self, color1, color2 ):
self.circ1.setFill( color1 )
self.circ2.setFill( color2 )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
#----------------------------------------------------------------
class Car:
def __init__( self, P1, P2 ):
self.P1 = P1
self.P2 = P2
self.width = abs( P1.getX()-P2.getX() )
self.height= abs( P1.getY()-P2.getY() )
self.rect = Rectangle( P1, P2 )
center1 = Point( P1.getX()+self.width/8, P2.getY() )
center2 = Point( P1.getX()+self.width*7/8, P2.getY() )
radius2 = self.height/3
radius1 = radius2/2
self.wheel1 = Wheel( center1, radius1, radius2 )
self.wheel2 = Wheel( center2, radius1, radius2 )
def draw( self, win ):
self.rect.draw( win )
self.wheel1.draw( win )
self.wheel2.draw( win )
def setFill( self, color1, color2, color3 ):
self.rect.setFill( color1 )
self.wheel1.setFill( color2, color3 )
self.wheel2.setFill( color2, color3 )
#----------------------------------------------------------------
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():
global W, H
win = GraphWin( "wheel demo", W, H )
waitForClick( win, "click to start" )
car = Car( Point( 100,100 ), Point( 250, 170 ) )
car.draw( win )
car.setFill( "red", "yellow", "black" )
waitForClick( win, "click to end" )
main()
car3.py
# car3.py
# Now the car can move!
from graphics import *
W = 400
H = 400
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.radius1 = r1
self.radius2 = r2
def draw( self, win ):
self.circ2.draw( win )
self.circ1.draw( win )
def setFill( self, color1, color2 ):
self.circ1.setFill( color1 )
self.circ2.setFill( color2 )
def getRadius1( self ):
return self.radius1
def getRadius2( self ):
return self.radius2
def move( self, dx, dy ):
self.circ1.move( dx, dy )
self.circ2.move( dx, dy )
#----------------------------------------------------------------
class Car:
"""a class containing a rectangle and 2 wheels"""
def __init__( self, P1, P2 ):
"""constructs the car. The top-left and bottom-right points
defining the body of the car are given."""
self.P1 = P1
self.P2 = P2
self.width = abs( P1.getX()-P2.getX() )
self.height= abs( P1.getY()-P2.getY() )
#--- define rectangle---
self.rect = Rectangle( P1, P2 )
#--- and the two wheels ---
center1 = Point( P1.getX()+self.width/8, P2.getY() )
center2 = Point( P1.getX()+self.width*7/8, P2.getY() )
radius2 = self.height/3
radius1 = radius2/2
self.wheel1 = Wheel( center1, radius1, radius2 )
self.wheel2 = Wheel( center2, radius1, radius2 )
def draw( self, win ):
"""draw rectangle and 2 wheels on window"""
self.rect.draw( win )
self.wheel1.draw( win )
self.wheel2.draw( win )
def setFill( self, color1, color2, color3 ):
"""defines the color of the car. First is body, then inside wheel, then tire color"""
self.rect.setFill( color1 )
self.wheel1.setFill( color2, color3 )
self.wheel2.setFill( color2, color3 )
def move( self, dx, dy ):
"""defines direction of movement for all 3 elements of car"""
self.rect.move( dx, dy )
self.wheel1.move( dx, dy )
self.wheel2.move( dx, dy )
#----------------------------------------------------------------
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():
global W, H
win = GraphWin( "wheel demo", W, H )
waitForClick( win, "click to start" )
car = Car( Point( 100,100 ), Point( 250, 170 ) )
car.draw( win )
car.setFill( "red", "yellow", "black" )
for i in range( 100 ):
car.move( 2, 0 )
waitForClick( win, "click to end" )
main()