Please download the following version of the pinball construction kit.
Be sure to compile the game (compiling PinBallGameDriver.java should do it), run it, and study the code in some detail before attempting this assignment.
As always, feel free to ask questions!
One last hint : I found this assignment much easier to work with if I increased the "sleep" between painting the balls so that things moved MUCH slower. In fact, I changed it by a factor of 10.
Pinball isn't an unlimited game normally. Your quarter (perhaps I show my age with that price) gets you some set number of balls. Modify the game so that the player may only launch ten balls TOTAL. They may certainly be present all at once, but any attempts to launch a ball after the tenth ball has been launched ends in failure (no action).
Having said this, you should recall what we have talked about this semester in regards to "hard coding" numbers. Any code you write for this task should be easily modifiable. That is, if I suddenly changed it from ten to six, this should be a minor change in your code.
It is really annoying to move a target from the pallet and then drop it in the wrong place. Allow the player to reposition targets after they have been placed in the "playing area" (for purposes of this assignment, the playing area is "inside" of the three walls. You should set up your code so that players are NOT able to move the three bounding walls).
If a mouse 'down' event occurs over a target in the "playing area", move the target to the spot of the next mouse 'up' event.
This is easy in theory, but we want to write good clean code to provide this functionality. To have clean code, you have to think through the problem.
One way to do this is to modify the mousePressed() method of the MouseKeeper class. Notice that the current mousePressed() method already extracts the x and y coordinates from MouseEvent e . You can use this to determine if you have pressed on a target that is in the toolkit area or one of the ones that is "in play." To determine WHICH one of the "in play" targets it was, you should add a method to PinBallTargets so that they can tell us if they contain the point clicked by the user. [Hint: Look into how you can simply delegate this behavior to super classes and/or composition classes rather than writing the check yourself].
Once you determine which target it is (regardless of whether it is in the toolkit palette of objects or is "in play"), you can use the existing element variable to remember which target was pressed on.
Warning : The previous code suggests that you store the moving target in the variable named element regardless of whether it is a new element (coming from the palette) or an old element (already in the field of play, just being moved). Carefully observe that what you do on mouseReleased() depends on whether it was old or new (you don't want to add it into the vector AGAIN if it is an old element). Make sure that you pay attention to details and that you don't do unintended things.
On the surface, all you have to do is modify PinBall to implement the PinBallTarget interface and store them as targets instead of balls. There are a couple of advantages to this, not the least of which is that you can actually eliminate the balls vector.
Test this carefully. For example, you should first make sure that one ball traveling on its own doesn't do anything "flaky" (it will if you aren't careful).
The other thing that you will want to look at is what a ball should do when it hits another ball. How should their motion change? Where should those new motion directions get set?
Finally, one side effect of this solution is that a user can reposition a pinball that is in play. NOT GOOD. Check out the keyword instanceof and figure out how you could use this test to prevent this.
For Bonus
You may earn bonus points on this lab by completing one or both of the following activities:
Submit the files:
Since there are so many of them, go ahead and zip them into a single file
PLEASE submit only *.java files.
Be sure that your submission follows all homework submission requirements. In particular, be sure to staple your print-outs in the order listed above!