Difference between revisions of "CSC111 Homework 11 2011"
(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...") |
(→Submission) |
||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 17:50, 6 December 2011 (EST) | --[[User:Thiebaut|D. Thiebaut]] 17:50, 6 December 2011 (EST) | ||
---- | ---- | ||
+ | {| | ||
+ | | | ||
+ | __TOC__ | ||
+ | |- | ||
+ | | | ||
<bluebox> | <bluebox> | ||
− | + | 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. | |
+ | <br /> | ||
+ | You can work on this homework in pair mode. | ||
+ | <br /> | ||
+ | This homework is optional. You will not be penalized if you don't turn it in. | ||
+ | <br /> | ||
+ | This homework is due on Tuesday 12/13/11, at midnight. | ||
+ | <br /> | ||
+ | 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. | ||
</bluebox> | </bluebox> | ||
− | + | |} | |
− | =Traffic Light | + | =Traffic Light: Part 1= |
[[Image:TrafficLight.jpg|150px|right]] | [[Image:TrafficLight.jpg|150px|right]] | ||
− | * Create a class for a traffic light that can be drawn on the graphics window, with the bus/car and the trees. | + | * 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 | + | * 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 | + | * Your class should contain methods that should allow one to: |
− | ** Turn the green light on. This will set the other ones off (i.e. black) | + | ** Turn the green light on. This will automatically 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 yellow light on. This will automatically set the other ones off (i.e. black) |
− | ** Turn the red light on. This will 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: | * 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: | ||
Line 46: | Line 60: | ||
=Part 2= | =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 | + | * 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: | ||
<br /> | <br /> | ||
Line 56: | Line 74: | ||
position = Point( ..., ... ) | position = Point( ..., ... ) | ||
light = TrafficLight( position ) | light = TrafficLight( position ) | ||
+ | light.draw( win ) | ||
+ | car = Car( ... ) | ||
+ | car.draw( win ) | ||
while True: | while True: | ||
− | light. | + | light.update() |
− | + | ||
− | if light.isGreen(): | + | if ( not light.isGreen() ) and car.distanceFrontTo( light.getPosition() ) <= 50 : |
− | + | car.move( ) | |
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
+ | =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 program files as follows: | ||
+ | |||
+ | rsubmit hw11 hw11.py | ||
+ | rsubmit hw11 hw11class.py | ||
+ | |||
+ | * or use the submit form at http://cs.smith.edu/~111a/submit11.htm | ||
+ | |||
+ | <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Homework]] |
Latest revision as of 09:00, 8 December 2011
--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.
|
Traffic Light: Part 1
- 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 program files as follows:
rsubmit hw11 hw11.py rsubmit hw11 hw11class.py
- or use the submit form at http://cs.smith.edu/~111a/submit11.htm