Lab 7: Flying Rocks

For this lab, you will prepare a single file, called rocks.py. I've prepared a starter file for you, available here on the course webpage.

Turning in your work

Be sure to turn in whatever you have finished, at the end of the lab session. You may continue to work until midnight, and submit your work again.

Late penalties: 1 day late: -5%, 2 days: -10%, -3 days: -20%.

To turn your work in, go to mycourses.unh.edu, find CS414, open the syllabus, and click on Lab 7. Then click Submit Assignment, click Choose file, and find rocks.py.

Your task:

Run Idle, and then open the file rocks.py. Choose "Run -> Run Module" to see the program in action. You may have to move your code windows to the right, to see the turtle window.

Then, complete these steps:

  1. The rock's initial motion is to the right. This is set in initialize_rocks(), with a seven-number list: [x,y, vx,vy, angle, rotation_rate, size]. Change that line to modify the rock's vy:
                 rock_motion = [0,0, 30,20, 0, 0, 20]
               
    The rock should now move up, and to the right.

  2. Now, make the rock turn, by changing its rotation rate to 100 degrees/second:
                 rock_motion = [0,0, 30,20, 0, 100, 20]
               
  3. Notice the random_motion function. It generates some random numbers for the rock's motion:
                 x = random.randrange(window_left, window_right)
                 y = random.randrange(window_bottom, window_top)
                 vx = random.random() * 200 - 100
                 vy = random.random() * 200 - 100
                 rotation_rate = random.random() * 200 - 100
               
    use it by replacing the rock_motion = line with this:
                 rock_motion = random_motion()
               
    Now, run the program again. If you don't see anything, the rock might have moved off-screen; run it again.

  4. Make the rock bigger. This will happen in several places:

  5. if the rock exist the right side window, make it reappear on the left side, and if it exits the left side, make it appear on the right. change the code thus:

  6. Create another rock. You will need to change several parts of the code:

  7. Give both rocks a random size, between 20 and 120 pixels. In random_motion, call random.randrange(20,120), and put its result as the last value in the returned list (it's currently 20).

  8. Make the rocks bounce, if they collide: in move_objects(T), add this code at the bottom:
                 x1   = rock_motion[0]
                 y1   = rock_motion[1]
                 rad1 = rock_motion[6]
                 x2   = rock2_motion[0]
                 y2   = rock2_motion[1]
                 rad2 = rock2_motion[6]
                 if collided(x1,y1,rad1, x2,y2,rad2):
                     resolve_collision(rock_motion, rock2_motion, T)
               
  9. (10% bonus): Add more rocks. MAKE A COPY OF YOUR rocks.py FIRST, in case this bonus effort doesn't work.

    Instead of having 2 rock motion records, create a list with 5 of them. This will have several consequences in the code: