CSC111 Homework 11 2011
--D. Thiebaut 17:50, 6 December 2011 (EST)
Traffic Light Problem: Part 1
- Create a class for a traffic light that can be drawn on the graphics window, with the bus/car and the trees.
- The traffic light is simply a tall long rectangle with three circles inside, one which is either black or yellow, one that is either black or green, and one that is either black or red.
- Your class should contain methods that should allow objects instantiated from it to
- Turn the green light on. This will set the other ones off (i.e. black)
- Turn the yellow light on. This will set the other ones off (i.e. black)
- Turn the red light on. This will set the other ones off (i.e. black)
- The default mode is for TrafficLight objects to be created with the green light on.
- Test your program and make sure you can draw a traffic light and control it in some ways. Below is an example of how you could test this:
def main():
win = GraphWin( "Hw 11 - 111b-xx", W, H )
x = ...
y = ...
light = TrafficLight( Point( x, y ) )
light.draw( win )
click = 0
while True:
if win.checkMouse() != None:
click = ( click + 1 ) % 3
if click == 0: light.setGreenOn()
if click == 1: light.setYellowOn()
if click == 2: light.setRedOn()
win.close()
main()
Part 2
- Same as Part 1, but the TrafficLight object will now switch on its own from green to yellow, from yellow to red, and from red back to green.
- The way to do that is for the traffic light class to contain a counter, and every time it is called to update itself, it increments a counter. If the counter passes a particular threshold, say 10, it decide to turn on anther light.
- There should be a car (or bus) going around the window, wrapping around the window as it leaves one edge and reappearing on the other side.
- There should be trees around as well.
- The car should stop if it is within 50 pixels in front of the traffic light and the light is red or yellow. Otherwise the car moves on...
- Here is an example below of what your main program could look like:
position = Point( ..., ... )
light = TrafficLight( position )
light.draw( win )
car = Car( ... )
car.draw( win )
while True:
light.update()
if ( not light.isGreen() ) and car.distanceFrontTo( light.getPosition() ) <= 50 :
car.move( )