CSC111 Homework 11 2011

From dftwiki3
Revision as of 17:50, 6 December 2011 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- <bluebox> </bluebox> =Traffic Light Problem: Part 1= 150px|right * Create a class for a traffic light that can be drawn on the graphics...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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


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 Exercise 1, but the objects are designed to be part of an animation loop. In the loop objects such as cars, buses or trees move left or right. TrafficLight object may move as well. However, their main function is to slowly change state, going from Green to Yellow, to Red, and back to Green. But they do change state slowly, every couple seconds or so. This is done by having a method called tick(), which is called once every time through the animation loop. Every 10 loops the traffic light changes state.
  • The Traffic Light objects can be probed in the animation loop to see which light is on.
  • Here is an example of an animation loop with a bus, trees, and a traffic light:


position = Point( ..., ... )
light = TrafficLight( position )

while True:
    light.tick()
    
    if light.isGreen():
         bus.move( )