CSC111 Homework 11 2011

From dftwiki3
Revision as of 18:15, 6 December 2011 by Thiebaut (talk | contribs)
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 the syllabus says, 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, 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( )


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