Introduction to Computer Science
Union College
Spring 2009

Homework 2 - due Wednesday, April 15, before class

1) Working overtime

Many companies pay time-and-a-half for any hours worked above 40 in a given week. Write a program which asks the user to input the number of hours worked and the hourly rate and which then calculates the total wages for the week.

2) Leap years

A year is a leap year if it is divisible by 4, unless it is a century year that is not divisible by 400. (1800 and 1900 are not leap years while 1600 and 2000 are.) Write a program that calculates whether a year (inputted by the user) is a leap year.

3) Bouncing objects

(a) Download, run, and look at bouncing_balloon.py (you also need red_balloon.gif, if you don't have it already). Answer the following questions:

  • Assuming that a variable with the name screen which is referring to the pygame window object and a variable with the name balloon which is referring to an image object have been defined, where would the command screen.blit(balloon,(0,0)) place the image on the screen? (Explain in detail how the picture is located with respect to the edges - or draw a sketch illustrating the position, if you find that easier.)
  • When you run bouncing_balloon.py, the balloon bounces back as soon as the side of the balloon touches one of the edges. Describe how that is achieved in the code. (Why does the balloon bounce as soon as its edge touches the side and not when its center touches the side or when it has completely moved out of the picture?)

(b) Now, download, run, and look at bouncing_circle.py. Answer the following questions:

  • Assuming that a variable with the name screen which is referring to the pygame window object has been definited, where would the command pygame.draw.circle(screen, pygame.color.Color('navy'),(0,0),75) place the circle on the screen? (Explain in detail how the circle is located with respect to the edges - or draw a sketch illustrating the position, if you find that easier.)
  • When you run bouncing_circle.py, the circle bounces back as soon as the side of the circle touches one of the edges. Describe how that is achieved in the code.

Answers by Arkadiy Norkin

(c) Starting from bouncing_circle.py place a second circle on the screen, which does not move. Then make the first circle bounce off this new circle as well. That means, you need to detect whether the two circles are overlapping or touching. If so, move the first circle away from the second and revers both its x and y velocity.

Optional (only do this once you have managed to get part (c) to work): If you want to make the bouncing look more realistic, you have to do something more complicated than just reversing the x and y velocity. You can use this code instead, which calculates the proper angle of reflection:

        # distance between centers and between sides of circles
        center_d = math.sqrt((c2_x - c_x)**2 + (c2_y - c_y)**2)
        edge_d = center_d - radius - c2_radius
        # reflection normal (along the line connecting the centers of the circles)
        n_x = (c_x - c2_x) / center_d
        n_y = (c_y - c2_y) / center_d
        # move moving circle out of stationary circle
        c_x = c_x - edge_d+1 * n_x
        c_y = c_y - edge_d+1 * n_y
	# get new velocities by reflecting the speed about the normal
        dotprod = -speed_x * n_x - speed_y * n_y
        speed_x = speed_x + 2 * n_x * dotprod
        speed_y = speed_y + 2 * n_y * dotprod
Substitute your variable names for speed_x and speed_y (the speed of the moving circle) and for c_x and c_y (the center of the moving circle) and for c2_x and c2_y (the center of the stationary circle).

(d) Upload all of your python programs and answers to the questions to Blackboard.

by Kristina Striegnitz