Difference between revisions of "CSC111 Lab 11 2015"
Line 125: | Line 125: | ||
<br /> | <br /> | ||
[[Image:MyRectClassLabel.png|right|200px]] | [[Image:MyRectClassLabel.png|right|200px]] | ||
+ | * Create a new program called '''Lab11_3.py''' and copy the code below into it. | ||
::<source lang="python"> | ::<source lang="python"> | ||
# lab11_3.py | # lab11_3.py | ||
Line 177: | Line 178: | ||
</source> | </source> | ||
+ | * Run the program. Verify that it displays a Yellow rectangle with a string in the middle. | ||
+ | * Add a loop to your main program, that will make the rectangle move to the right: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | for i in range( 20 ): | ||
+ | r1.move( 10,2 ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | * Do you observe something strange? What is happening? Think... | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | * Think some more. Do not move on until you are sure you know what is going on... | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | * Ok, if you thought that the inherited '''move()''' method from the Rectangle class is not what we want to use, because the original Rectangle class does not know anything about the label, you are totally correct. If, furthermore, you thought you needed to create a new '''move()''' method for '''MyRect()''' that will ''override'' the Rectangle '''move()''' method, then I'm really impressed! | ||
+ | * Create a new method called '''move()''' and base its design on the '''draw()''' method, so that it will call '''Rectangle.move()''' to make the yellow rectangle move, and it will activate the '''move()''' method of the label to make it move by the same dx, dy displacement. | ||
+ | * Verify that your new program makes the rectangle and the label move together (it's ok if you see some jitter in the motion). | ||
<br /> | <br /> | ||
=Circles with Labels Inside= | =Circles with Labels Inside= | ||
− | + | <br /> | |
+ | * Use the same approach to create a new class called '''MyCirc''' that is a subclass of Circle, and that displays a circle with a label inside. | ||
+ | * Modify the loop in your main program so that it makes both the MyRect object and the MyCirc object (that you will have created) move together. | ||
+ | <br /> | ||
=Car with 2 rectangles: one with a label showing Taxi, one without for the top.= | =Car with 2 rectangles: one with a label showing Taxi, one without for the top.= | ||
− | + | <br /> | |
+ | * Locate the program you wrote a while back, that displays a car on the graphic window (with a body, two wheels and a top). | ||
+ | * Save your code in a program called '''lab11_5.py'''. | ||
+ | * In case you had implemented a text label as part of your Car class, please remove it. Make sure your program displays only a car with 2 wheels and two rectangles, one for the body, one for the top. | ||
+ | <br /> | ||
+ | ==Using MyRect to build the Car== | ||
+ | <br /> | ||
+ | * Modify | ||
+ | <br /> | ||
=Animal Class, | =Animal Class, | ||