Exercise 11 - Practice with object oriented programming#

Accept the exercise

Complete parts 1-4 by importing the MovingCircle() class from movingcircles.py and adding in the additional functionality. You should not change anything in movingcircles.py and you should not be copying/pasting any code from movingcircles.py to exercise_11.py. Your solutions should consist solely of editing exercise_11.py

Please tag each part separately as we did with the earlier assignments.

Part 1: Make it red!#

Add a method make_it_red() such that when you click on a moving circle, it turns red. All the circles should continue moving. There should not be any pauses.

Part 2: Make them stop#

Add a method make_them_stop() such that when you click on a moving circle, it keeps moving, but all the other circles stop moving.

Part 3 Make it drop!#

Add a method make_it_drop() such that when you click on a moving circle, it drops to the bottom and remains there (it’s ok if it’s not completely visible once it lands; it’s just a small tweak to avoid it having go totally off-screen and so remain visible). What is important is that the should be gradual. The circle should smoothly descend down rather than disappear from its original position and reappear in its new position. The other circles should keep moving while the one you clicked on is dropping to the ground.

Tip

Have make_it_drop() change an attribute in the clicked-on circle so that it moves downwards.

Part 4: Make it drop…. more naturally#

When things drop here on Earth, they don’t fall at a constant velocity. They accelerate. Because, you know, gravity! Extend your make_it_drop() method to accept a parameter accelerate. Its default value is False. When set to True, the circle should drop as in part 3, but it should accelerate downward according to the following equation.

h = .5*g*t**2

Note

In Python, as in many other programming languages ** is exponentiation, i.e, 3**3=27

If you remember your high school physics, g is the gravitational constant which is 9.8 m/s/s. In our case, the units can be pixels, so 9.8 pixels per second per second.

h is the distance travelled (in our case, the distance from the circle’s starting position), and t, is, of course, time. Use seconds for time. Remember that if your laptop is refreshing at 60Hz, each frame lasts ~17 msecs, i.e., .017 seconds. Refresh the position of the circle on each frame so that it’s nice and smooth. The other circles should keep moving as normal when the circle you clicked on drops down.

Bonus#

a. (1 pt) Right now the circles are wiggling around and when they hit the wall they kind of stick around there until, by chance, they reverse course. Change the code so that the circles move in straight lines, but when they hit a wall, they bounce in a geometrically appropriate way (like Pong).

b. (2 pts) Extend your code to make a little game. Position little rectangles along the bottom. These will represent enemy buildings. Now, clicking on a circle should cause it to stop moving horizontally and accelerate downwards (just like in part 4). If it hits a building, the building should turn red. When you run out of circles to click on, the game is over. If you want, put a little score in the corner that increases by 1 every time you hit a building.

Tip

You can check if a rectangle contains a point (like the position of a current circle; hint hint!) by using Rect.contains()