CSC111 Lab 12 Solution Program Part 1
--D. Thiebaut 11:23, 5 December 2011 (EST)
# undocumented code to help with Lab 12, Part 1
from graphics import *
import random
class Wheel:
def __init__(self, center, r1, r2 ): #__init__() is constructor; self is part of init; center, r1, r2 match parameters
self.center = center
r1, r2 = min(r1, r2), max(r1, r2)
self.radius1 = r1
self.radius2 = r2
self.c1 = Circle( center, r1 )
self.c2 = Circle( center, r2 )
def setFill( self, col1, col2 ):
self.c2.setFill( col1 )
self.c1.setFill( col2 )
def draw( self, win): #win matches parameter sent to draw in main
self.c2.draw( win )
self.c1.draw( win )
def move( self, dx, dy ):
self.c2.move( dx, dy )
self.c1.move( dx, dy )
class Bus:
def __init__( self, topLeftPoint, width, height ):
x1 = topLeftPoint.getX()
y1 = topLeftPoint.getY()
x2 = x1 + width
y2 = y1 + height
self.body = Rectangle( topLeftPoint, Point( x2, y2 ))
self.wheels = []
self.wheels.append( Wheel( Point( x1+width*2/12,y2 ), width/12/2, width/12 ) )
self.wheels.append( Wheel( Point( x1+width*9/12,y2 ), width/12/2, width/12 ) )
self.wheels.append( Wheel( Point( x1+width*11/12,y2), width/12/2, width/12 ) )
self.window = Rectangle( Point( x1+width/12/2, y1+height/4/2 ),
Point( x1+width/12*3/2, y1+height/4*3/2 ) )
def setFill( self, bodyCol, windowCol, wheelCol1, wheelCol2 ):
self.window.setFill( windowCol )
self.body.setFill( bodyCol )
for wheel in self.wheels:
wheel.setFill( wheelCol1, wheelCol2 )
def draw( self, win ):
self.body.draw( win )
self.window.draw( win )
for wheel in self.wheels:
wheel.draw( win )
def move( self, dx, dy ):
self.body.move( dx, dy )
self.window.move( dx, dy )
for wheel in self.wheels:
wheel.move( dx, dy )
def getX( self ):
return self.body.getP1().getX()
class Tree:
def __init__( self, basePoint, height ):
x1 = basePoint.getX() - 10
y1 = basePoint.getY()
x2 = x1 + 20
y2 = height*3/4
self.trunk = Rectangle( Point(x1,y1), Point(x2,y2) )
radius = height/3
self.radius = radius
x3 = x1 + 10
y3 = y1 - height + radius
self.head = Circle( Point( x3, y3 ), radius )
self.trunk.setFill( random.choice( ["brown", "orange", "black" ] ) )
self.head.setFill( random.choice( ["green", "lightgreen", "darkgreen" ] ) )
def draw( self, win ):
self.trunk.draw( win )
self.head.draw( win )
def move( self, dx, dy, W ):
self.trunk.move( dx, dy )
self.head.move( dx, dy )
if self.trunk.getP1().getX() > W + self.radius:
self.trunk.move( -W-self.radius*2, 0 )
self.head.move( -W-self.radius*2, 0 )
def main():
W = 600
H = 400
win = GraphWin( "Lab 12", W, H )
bus = Bus( Point( W/16, H/4 ), W/16*12, H/2 )
bus.setFill( "orange", "lightblue", "white", "black" )
bus.draw( win )
trees = []
for i in range( 3 ):
trees.append( Tree( Point( W/4*(i+1), H*7/8 ), H/8*6 ) )
for tree in trees:
tree.draw( win )
# first loop: bus moves, trees stay still...
while True:
bus.move( -3, 0 )
if win.checkMouse() != None:
break
# second loop, bus still, trees move...
x = bus.getX()
bus.move( -x+W/4, 0 )
while True:
for tree in trees:
tree.move( 3, 0, W)
if win.checkMouse() != None:
break
win.getMouse()
win.close()
main()