CSC111 Homework 11 2011

From dftwiki3
Revision as of 22:33, 6 December 2011 by Thiebaut (talk | contribs) (Submission)
Jump to: navigation, search

--D. Thiebaut 17:50, 6 December 2011 (EST)


This is a make-up homework. You can use it to bring back the current lowest grade on Hw 1 to 7, and 9 to 10. Homework 8 can only be brought up by doing Make-up Homework 8.
You can work on this homework in pair mode.
This homework is optional. You will not be penalized if you don't turn it in.
This homework is due on Tuesday 12/13/11, at midnight.
As specified in the syllabus, once all the homework assignments have been graded, the lowest one will be dropped automatically. In other words, only the top 9 grades for homework assignments will count toward the final grade.

Traffic Light Problem: Part 1

TrafficLight.jpg
  • Create a class for a traffic light that can be drawn on the graphics window, along with the bus/car and the trees.
  • The traffic light is simply a tall long rectangle with three circles inside, one that 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 one to:
    • Turn the green light on. This will automatically set the other ones off (i.e. black)
    • Turn the yellow light on. This will automatically set the other ones off (i.e. black)
    • Turn the red light on. This will automatically set the other ones off (i.e. black)
  • Add any other methods you feel are necessary for your program to work well.
  • By default TrafficLight objects are 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

  • This is the continuation of Part 1: now the TrafficLight object will switch on its own from green to yellow, from yellow to red, and from red back to green.
  • The way to implement this is for the traffic light class to contain a counter, and every time the traffic light is called to update itself, it increments a counter. If the counter passes a particular threshold, say 10, it decides to turn on another light.
  • The main program should not be the place where the traffic light is told to change light! This should be done inside the TrafficLight class, in a method that is called every time through the animation loop.
  • There should be a car (or bus) going around the window, wrapping around the window as it leaves one edge and reappears on the other side.
  • There should be trees around as well. The trees do not move.
  • 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 through the light.
  • 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( )


Part 3

  • Once your program works well, put all the classes you have created in a separate file, called hw11class.py and your main program in a file called hw11.py where you will import hw11class or from hw11class import *.
  • Make sure your program works as well as when it was in one class.

Testing

  • Make your program stop when the user clicks the mouse on the graphics window (once to stop the animation, once to close the window)

Submission

  • Submit your class as follows:
  rsubmit hw11 hw11.py
  rsubmit hw11 hw11class.py