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.
bopa.pyis a fast 2D graphics module.
bopademo.pyis a demonstration program for it; it requires the image file
particleA.pyis an updated version of the
particletext.pycode that we discussed in class on Wednesday.
Rename the file
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.
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:
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:
__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 (
SOMETHINGis up to you).
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:
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
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
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.