Moving Those Pucks


Handling the sprites here is a little different than in the Aquarium application, because when a puck slides into a goal, the user's score or the computer's score should be incremented and the puck should be taken out of actionwhich doesn't happen to fish. To make this happen, the code passes the position of both blockers to the method that moves each puck around, slide, and that method will return a value of 0 if the puck is still in play, -1 if it went into the computer's goal, and 1 if it went into the user's goal. Each Puck object also has a method named gone, which will return TRue if the puck is out of play (and therefore should not be moved or drawn).

The run method uses each puck's slide method to move the pucks and check to see if either the user's score or the computer's score needs to be incremented. When you call slide, you pass the rectangles specifying the location of both blockers (Pucks.elementAt(12) is the computer's blocker and Pucks.elementAt(13) is the user's) so that the method can see if a puck has hit a blocker. The slide method returns a value of 0 if the puck is still in play, -1 if it went into the computer's goal, and 1 if it went into the user's goal:

 public void run() {     Puck puck;     while (runOK) {         if(!stop){             int numberLeft;             for (int loopIndex = 0; loopIndex < 12; loopIndex++){                 puck = (Puck)pucks.elementAt(loopIndex);                 if(puck.gone()){                     continue;                 }                 retVal = puck.slide(pucks.elementAt                     (13).rectangle, pucks.elementAt                     (12).rectangle);                 numberLeft = 0;                 for (int loopIndex2 = 0; loopIndex2 < 12;                     loopIndex2++){                     if(!((Puck)pucks.elementAt(loopIndex2))                         .gone()){                         numberLeft++;                     }                 }                 if(retVal < 0){                     if(yourScore + theirScore + numberLeft == 11){                         label1.setText(String.valueOf                         (++yourScore));                     }                 }                 if(retVal > 0){                     if(yourScore + theirScore + numberLeft == 11){                         label2.setText(String.valueOf                             (++theirScore));                     }                 }         .         .         . 



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net