Exercises using PinBall Construction Kit

Download Report

Transcript Exercises using PinBall Construction Kit

Where the heck ARE we?
• We’ll finish covering the material through
chapter 7 today.
• We will be working on the material from 8
and 10.
• We are skipping Chapter 9 completely
(although you may want to look at Budd’s
examples since they show up in later
chapters.
Homework #5
• Design and implement a GUI front-end to the TicTac-Toe game.
• You may use your solution to homework #4, or
you may download my solution.
• This program should have the same functionality
as homework #4.
• I encourage you to look at past programs (e.g.,
MemoPad, CannonGame, etc.) as examples of
how the GUI components work. Your best
reference might be to read chapter 13 on the
AWT API.
Opening Exercise
• In the final program, the items on the pallet are
still stored in the targets vector, so that they will
be checked for a hit, even though they can never
be hit by a ball.
• A better solution would have been to create a
new vector pallet that will hold these items,
redraw both the pallet and the targets on a
repaint, but only if a target in the targets vector is
hit by a ball.
• Modify the program in this fashion.
Opening Exercise 2
• It’s very annoying to move a target from the
pallet and drop it in the wrong place.
• So, extend the solution to allow for the targets in
the playing field to be moved.
Opening Exercise 2 Solution
• Recall that we needed to know if a mousePress
occurred on a target. We tried to leverage the
intersects method of PinBallTarget interface by
creating a temporary ball at the point of the mouse
press:
Ball tempBall = new Ball( e.getX(), e.getY(), 1 );
for ( int j = 0; j < targets.size(); j++ ) {
PinBallTarget target=(PinBallTarget) targets.elementAt(j);
if ( target.intersects( tempBall ) ) {
startPressed = 1;
element = target;
} // end if
} // end for
Opening Exercise 2 Solution
• We didn’t want to create a PinBall because PinBalls
get constructed with motion and a large size.
• This forced us to change the PinBallTarget interface to
accept the more general Ball:
public interface PinBallTarget {
public boolean intersects( Ball aBall );
public void
moveTo
( int x, int y );
public void
paint
( Graphics g );
public void
hitBy
( PinBall aBall );
} // end PinBallTarget
• We also have to change the intersects methods in
each of the targets
• It turns out that we could have used a PinBall since it
exists for such a short time