CSE 142 Spring 2002Homework #3Pinball (Minus a Few Things)Due: Electronically, 11:00pm,
Sun., April 21;
|
00:49:00, 4/16/02: New section, Running the Starter Code of the Box, added to this page.
Homework 3 Frequently Asked Questions
This misnamed page really contains
some information about the kinds of things that
might be going wrong for you trying to obtain or use the skeleton code files.
It is not mandatory reading, but is worth a look if you're having trouble
setting up your system (as contrasted with issues like understanding what
the assignment is asking you to do, or the code itself, neither of which
is talked about in the FAQ).
This assignment involves much (much) more reading of code than writing. You will be given an incomplete implementation of a program that bounces balls inside a closed container, and be asked to complete it. The assignment has a number of phases. You will save time by completing each entirely before moving on to the next, rather than trying to do them all at once.
Now you're ready to roll. You can use BlueJ, which requires creating a BlueJ project from them. (Select "open non-BlueJ from the file menu, then select the folder containing the .java files.) Or you can just compile them and run them from the command line ("java *.java" and then "java BallGame" in a DOS (command) window). Or you can compile them ("javac *.java") and then use jeva to test and run them.
Here is a brief description of the two classes involved. Complete interface documentation is available here.
BallGame
This is the main class. It's constructor creates a window and a bounding rectangle. All action takes place inside the bounding rectangle. Methods are available to create up to two balls and two obstacles, all in random locations. The obstacles are in fixed positions, but the balls move. When a ball encounters an obstacle, or the boundary rectangle, it bounces.
BallGame also "drives" the simulation - it cycles as fast as it can, asking the balls to move and then to check to see if they should bounce off anything else in the virtual world.
Player
This is the class that implements both balls and obstacles. Abstractly, the two are the same, at the level of the Java implementation. (Obstacles are just balls whose velocity is zero. The user (you) will probably be tempted to use pictures of balls for the balls, and pictures of other things for the obstacles, but what the pictures are is irrelevant to the Java code.)
A Player object knows its velocity (and so how far to move itself every time step), and it knows under what conditions it should bounce, and how to bounce. It does any of those things only when asked to do so, though, by a BallGame object.
It's important to note that a ball is actually a rectangular object, no matter what the image is that appears to be moving. That is, even though the .jpg might show a yellow, round ball, the Player object itself doesn't know anything about that. All it knows is that it's a rectangle. You don't see it because the area in the rectangle around the ball is white, just like the background. (You will see it when a corner of the ball passes through another object, though.)
The files you will start from are what's left of a working program once a few things were removed. The total amount of code removed is very small. Reactivating each of the disabled features (described next) should take only a few lines of code. This is intentional. You will have to understand how the code works to know which few lines of code to add where. Reading code is a lot easier than writing it, because for the most part the messy little details (like "Do I have to have a '(' here?" or "Am I allowed to have a '(' here?") are irrelevant when you're reading. So, the goal is to save you time and frustration. At the same time, this program is complicated enough that trial-and-error will not work, period. Make sure to spend the time to understand what is going on before attempting to modify the code.
Here's a sample sequence of method invocations:
BallGame bg = new BallGame(); (a window appears)If the ball disappears off the screen, you can get it back with something like
Player ball = bg.createBall("soccer.jpg"); (a ball appears)
bg.go(100); (the ball moves)
ball.setPosition(50,50); (the ball puts itself at (50,50), which is visible)You can also adjust a ball's velocity. See the documentation for Player objects.
If you're using the command line to compile ("javac *.java") and run, to do the latter just type "java BallGame." The method main() that you'll find in BallGame.java is invoked. It runs through a sequence of commands something like the example above, except that it will ask you to supply the number of time steps to give to the go() method invocation. (It will wait for you to type an integer on the keyboard, and then use that number.)
In case you'd like to try more, though, for optional no-credit extra-credit, try fixing the corner bounce glitch. Or try making balls bounce off each other, as well as off the things they already bounce off. Or try figuring out how this code could have been made simpler while at the same time allowing for arbitrary numbers of balls and obstacles, up to the limits of the machine's performance. (Hint: ArrayList and its cousins.) Or, if you really want a challenge, make the program work even when the maximum distance a ball can move in a single time step is increased arbitrarily. (In the base code it can never be greater than 4 units in either dimension in a single move.)
Turn-in form. (now available)
In lecture on Monday, April 22, you must turn in a short (no more than one page) written report about your project. Include your name, section, homework number and date at the top. If you didn't get the program finished, describe what still does not work, and your best guess as to what is needed to fix it. Whether or not you finished it, describe the most important things you learned from this project, which could be new ideas that you have never seen before, or misconceptions that you had that the project helped you straighten out. Comments on the positives and negatives of reading versus writing code would also be appropriate.