2010 February 26 / j d a v i s @ c a r l e t o n . e d u


In this laboratory exercise we practice with objects and the object-oriented graphics module Bopa. As was the case in our Elevation Models assignment, you will not be able to run the graphics software on your own computer; you must use the lab machines. Download these files to your computer.

Take a moment to read through particleA.py.

Make a Simulation

Rename the file particleA.py to particleB.py. Edit the file to add a new class, Simulation, with docstring """A Simulation object holds a list of particles and runs a dynamical simulation on them.""", and with these methods:

    def __init__(self, particles):
        """Initializes the simulation with a (copy of a) list of particles."""
    def getParticles(self):
        """Returns the list of particles in the simulation. The user can modify this list as she desires."""
    def simulate(self, delta):
        """Tells all particles to advance the simulation by delta seconds."""
    def printPositions(self):
        """Prints a message followed by the positions of all of the particles."""

At the bottom of particleB.py, change the demo code so that it creates a list of 10 particles, makes a simulation out of them, and runs the simulation for a few seconds, printing the positions of the particles after each second.

Write a Bopa Program

Now let's switch gears. Skim through the first three sections of the Bopa manual ("Getting Started", "Methods Common to All Shapes", and "The Various Kinds of Shapes"). Start a new Python file shapes.py. In this file, write a program that displays an triangle and an annulus, of different colors. Here's mine:

Make a Visual Simulation

Duplicate the file particleB.py and name the duplicate particleC.py. In this section we will add a bunch of Bopa stuff to particleC.py, to produce a visual particle simulation.

The first thing we must do is alter the Particle class to endow particles with Bopa shapes. The class must be altered in three spots:

The second thing we must do is hook the Simulation into the graphics system. This is a little tricky, so I'll just tell you what to do. Add this code to Simulation's __init__() method.

        present = time.time()
        animation = bopa.Animation(present, present + 1.0, self.simulate, 1.0, None)

This code creates a Bopa animation that runs forever. The frame rate of the animation depends on the operating system and what the user is doing. In any event, the result is that on each frame, the Simulator's simulate() method is called, with the number of seconds that have passed since the last frame. That is, the simulation runs in real time.

The third and final thing we must do is alter the demo code in particleC.py. The code should no longer create particles, create a simulation, and then tell the simulation to run a lot. Instead, the code should just create particles and create a simulation; the simulation automatically runs itself in real time. Of course, all Bopa programs begin and end with two window-handling lines; don't forget to insert those too.