CSC111 Car and Trees program
--D. Thiebaut 11:49, 1 December 2011 (EST)
from graphics import *
import random
W = 800
H = 600
#----------------------------------------------------------------
class Wheel:
"""A class with two concentric circles"""
def __init__( self, center, r1, r2 ):
r1, r2 = min( r1, r2 ), max( r1, r2 )
self.circ1 = Circle( center, r1 )
self.circ2 = Circle( center, r2 )
self.radius1 = r1
self.radius2 = r2
def setFill( self, col1, col2 ):
self.circ1.setFill( col1 )
self.circ2.setFill( col2 )
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 DTmove( self, dx, dy ):
self.circ1.move( dx, dy )
self.circ2.move( dx, dy )
class Car:
def __init__( self, p1, p2, width ):
self.width = width
self.rect = Rectangle( p1, p2 )
length = abs( p1.getX()-p2.getX() )
xWheel1 = p1.getX() + length/4
yWheel1 = p2.getY()
xWheel2 = p2.getX() - length/4
yWheel2 = yWheel1
self.w1 = Wheel( Point( xWheel1, yWheel1 ), length/16, length/8 )
self.w2 = Wheel( Point( xWheel2, yWheel2 ), length/16, length/8 )
def draw( self, win ):
self.w1.draw( win )
self.rect.draw( win )
self.w2.draw( win )
def setFill( self, col1, col2, col3 ):
self.rect.setFill( col1 )
self.w1.setFill( col2, col3 )
self.w2.setFill( col2, col3 )
def DTmove( self, dx, dy ):
self.rect.move( dx, dy )
self.w1.DTmove( dx, dy )
self.w2.DTmove( dx, dy )
x = self.rect.getP1().getX()
if x > self.width:
self.rect.move( -self.width, 0 )
self.w1.DTmove( -self.width, 0 )
self.w2.DTmove( -self.width, 0 )
class Tree:
def __init__( self, point, height, width ):
self.width = width
x = point.getX()
y = point.getY()
p1 = Point( x-20, y )
p2 = Point( x+20, y - height * 2 /3 )
self.trunk = Rectangle( p1, p2 )
self.trunk.setFill( "brown" )
self.head = Circle( Point( x, y - height * 5/ 6 ), height/6 )
self.head.setFill( "green" )
def draw( self, win ):
self.trunk.draw( win )
self.head.draw( win )
def DTmove( self, dx, dy ):
self.trunk.move( dx, dy )
self.head.move( dx, dy )
x = self.trunk.getP1().getX()
if x < 0:
self.trunk.move( self.width, 0 )
self.head.move( self.width, 0 )
#----------------------------------------------------------------
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 )
trees = []
for i in range( 10 ):
tree = Tree( Point( random.randrange(10,W), 550 ), 400, W )
tree.draw( win )
trees.append( tree )
car = Car( Point( 50,450 ), Point( 250, 550 ), W )
car.setFill( "yellow", "white", "black" )
car.draw( win )
while True:
#car.DTmove( 3, 0 )
for tree in trees:
tree.DTmove( -3, 0 )
if win.checkMouse() != None:
break
waitForClick( win, "click to end" )
win.close()
main()