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

# Particles

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:

• In its `__init__()` method, the particle must create some kind of Bopa shape. I chose to use triangles, but you might prefer some other shape. It should store this Bopa shape in an instance variable (`self.SOMETHING`, where `SOMETHING` is up to you).
• The particle knows its position in space, and the shape knows its position in the window, but the two do not automatically agree. Whenever the particle's location is changed, it must tell its shape to change its location too. Because of how we originally wrote `Particle`, there is one and only one primitive method that changes the location. Which one? Alter it so that it updates the position of the shape. After it updates the position of the shape, it must tell the window that the graphics have changed, using this line of code:
```bopa.getCurrentWindow().postRedisplay()
```
• Similarly, whenever the particle's direction changes, it must tell its shape to change its direction too. This requires you to alter one other primitive method.

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.