(120 points total) An ideal gas can be simulated by balls moving on a frictionless surface. For this homework you'll be developing the first part of such a simulation. You'll be working through several small steps, committing your work after each step. This will help us give you partial credit if you don't quite make it through all the steps.
(10 points)
Add the definition of a
Ball
class to your program (before main(), so other functions can use Balls). Your class must be named
Ball
. It should have a constructor that takes one non-self
argument, representing the window in which the ball will (eventually) be displayed. Once you have done this, you should be able to run the
ballSim
module, though it won't yet do anything interesting.
Ask for help if you get an error when trying to run the program!
Once the program runs without error, commit your work to your repository.
(10 points)
Edit the constructor of your
Ball
class so that it picks a random initial x and y position for the ball within the given window. Save the coordinates in instance variables of your class. Once this runs without error,
commit your work to your repository.
(Recall that you can use the randrange() function to get a random value in a particular range. For example, randrange(300) returns a random number between 0 and 299 inclusive.)
__str__
to your class that returns a string giving the ball's x and y coordinates. Recall that the
__str__()
method must take a
self
parameter, but doesn't need any other parameters.
(10 points)
Next, add a method named
timePasses
to your Ball class. The method should take one non-self
parameter giving the amount of time that has passed in seconds. For now, you'll ignore the formal parameter. Instead the method body should just contain the single line:
print self
Look in the
simulate()
function. You should find a commented-out line that calls the
timePasses()
method. Uncomment that line and run the program. The program should print the ball's x and y position to the Eclipse console view.
Ask for help if this doesn't work!
Once the program works correctly to this point, commit your work to your repository.
(15 points)
Edit the constructor of your Ball class to store an initial x and y velocity for the ball. For now you can just pick a constant velocity. Edit your
timePasses()
method to update the position of the ball. Recall that the new x position should be the old x position plus the x velocity multiplied the time that has elapsed, and similarly in the y direction.
Run your program again. You should see the ball's coordinates changing in the Eclipse console window.
Ask for help if this doesn't work!
Once the program works correctly to this point, commit your work to your repository.
print self
statement from your
timePasses()
method.
Commit your code to your repository.
random()
function to calculate an initial velocity in both the x and y directions.
(5 points) Make your ball start with a random initial color. You may find it helpful create a constant like this:
COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'violet' ]
You can then use the
randrange()
function to choose from the list.
Ball
class so that the ball detects when it has reached an edge of the window. When this happens, print which edge was reached. You should just use a print statement to print to the console.
Ball
class to make your ball change to a new color each time it bounces.
Commit your code to your repository.
(10 points)
Change your program to create multiple balls. You should create a constant at the top of your program that indicates the number of balls to create. Edit the
simulate()
function so it first builds a list of the right number of balls, then animates all of the balls in the list. You should be able to do this step without changing your
Ball
class. Do you see why?
After testing your code, commit it to your repository.
(5 points)
For this last step you do
not
have to write the code, just think about the problem and write down your thoughts. Add a comment to your program after the
simulate()
function, answering the following question:
Suppose you wanted to make the balls in your program bounce off each other. How would you need to change the
simulate()
function, and what method or methods would you add to your
Ball
class to implement this?