Difference between revisions of "CSC111 Exercises on Writing Classes"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Exercise 1= * Create a class for a traffic light that can be drawn on the graphics window, with the bus and trees. * Your class should allow objects instantiated f...")
 
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 07:24, 6 December 2011 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] 07:24, 6 December 2011 (EST)
 
----
 
----
 
+
[[Image:TrafficLight.jpg|150px|right]]
 
=Exercise 1=
 
=Exercise 1=
  

Revision as of 07:29, 6 December 2011

--D. Thiebaut 07:24, 6 December 2011 (EST)


TrafficLight.jpg

Exercise 1

  • Create a class for a traffic light that can be drawn on the graphics window, with the bus and trees.
  • Your class should allow objects instantiated from it to
    • Turn the green light on. This will set the other ones off
    • Turn the yellow light on. This will set the other ones off
    • Turn the red light on. This will set the other ones off
  • The default mode is for TrafficLight objects to be created with the green light on.

Exercise 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( )