Introduction to Computer Science
Union College
Spring 2009

Lots of bouncing balls

Download, run, and examine the following program: lots_of_balls_v0.py. It creates one ball and lets it bounce around.

The goal for this exercise is to create a list of lots of balls and have them all bounce around.

That means changes in three places: 1) Instead of creating a single 4-tuple representing one ball, you need to create a whole list of 4-tupels, each one of which is representing a ball. 2) Instead of moving and bouncing just a single ball, you need to move and bounce all balls in your list. 3) Instead of drawing just a single ball, you need to draw all balls in your list.

Creating a list of balls.

Here is one strategy for creating a list of balls: create an empty list. Then, in a loop that repeats 10 times, add 10 elements to this list. Each element should be a 4-tuple representing a ball. Each ball should be placed at a random location and should have random speeds.

Drawing all balls in the list.

Instead of having a draw statement for just one ball, create a loop that goes through your list of balls and draws each one of them.

When you run your code now, you should see a bunch of stationary balls on the screen.

Move and bounce all balls.

Instead of the single statement that just calls the move and bounce function for one ball, create a loop that goes through your list of balls and calls the function for each of them.

Submit on Blackboard

by Kristina Striegnitz