CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Assignment 6: Raindrops

Due: 8:00am, Thursday, February 25. Value: 30 pts. Submit to Sauron.

This assignment again uses the graphics package. If you lost it, right-click graphics.py, select Save Link As… or Save Target As… from the pop-up menu, and save the file on your computer. Then start a new file in the Wing IDE for your own work and save it in the same directory where you placed the graphics.py file.

In class we saw a program in which the user places ten raindrops on the screen, and the program animates them falling.

from graphics import *
import time

win = GraphWin('Raindrops')  # Create the window.

drops = 10 * [0]
for j in range(len(drops)):  # Accept 10 clicks from the user,
    loc = win.getMouse()     # creating a blue circle at each click,  
    drop = Circle(loc, 5)    # and saving it into the list "drops".
    drop.setFill('blue')
    drop.draw(win)
    drops[j] = drop

while not win.isClosed():    # Then animate the circles falling.
    for i in range(len(drops)):
        drops[i].move(01)
    time.sleep(0.05)

Your assignment is to write a program where the raindrops start falling as soon as the user creates them, and the user can create as many raindrops as desired. At a fundamental level, this requires merging the for j … loop's body into the while not win.isClosed() … loop of the above program, since you'll want the new drops to be created as the other drops move downward. Consequently, the resulting program ends up being very different from the original program. Below is a rough outline of it.

win = GraphWin('Raindrops')
drops = []                    # (We start out having no drops.)
while not win.isClosed():
    # Tell all in the list "drops" to go down one pixel
    # If mouse is clicked, add new circle into list "drops".

    time.sleep(0.05)

The original program uses a list that constantly has 10 slots for remembering the 10 drops. In your program, though, you'll want the list to grow larger with each new mouse click. You can do this by creating a list that is initially empty, but with each mouse click you use the list's append method to make the list gain a new slot with a value you provide: drops.append(value).

Since you'll want the program do things between mouse clicks (namely, animate the falling drops), you'll want to use checkMouse rather than getMouse. Recall that checkMouse returns None when the user hasn't clicked the mouse since the last time you asked the method.