How the Computer Blocks Pucks


The computer is supposed to move its blocker to intercept as many of the pucks as it can before they hit its goal. Here's how that worksif any puck is on track to hit the computer's goal, it calculates how long the puck will take to get there. It then moves to intercept the one that will hit the goal first. This is how the code calculates the next Y coordinate, impactY, at which a puck will hit the computer's goal:

 int lowestTime = 10000; int impactY = -1; for (int loopIndex3 = 0; loopIndex3 < 12;     loopIndex3++){     Puck movingPuck = (Puck)pucks.elementAt         (loopIndex3);     Rectangle r = movingPuck.rectangle;     Point mPosition = new Point(r.x, r.y);     Point mVelocity = movingPuck.velocity;     if(mVelocity.x > 0 && !movingPuck.gone()){         int yHit = (mVelocity.y / mVelocity.x) *         (backGroundImage.getWidth(this) -             mPosition.x) + mPosition.y;         if(yHit > 115 && yHit < 223){             int time = (backGroundImage.getWidth                 (this) - mPosition.x)                 / mVelocity.x;             if(time <= lowestTime){                 impactY = yHit;             }         }     } 

If the variable impactY is greater than zero, it holds the location where the next puck impact will be on the computer's goal. You use the slide method to move objects around in this game, but you wouldn't want the blockers to be moved around at random, like the pucks. Instead, there's a special version of the slide method built into the Puck class that lets you simply pass a new Y location, and it'll move the Puck object to that position. This is the version you'll use to move the blockers with. To make sure the computer's blocker doesn't have an unfair advantage, the code restricts its movements to 40 pixels at a time, like this:

       if(impactY > 0){           Puck block = pucks.elementAt(12);           int blockPosition = block.rectangle.y;           if(blockPosition < impactY){               block.slide(Math.min(blockPosition +                   40, impactY));           } else {                 block.slide(Math.max(blockPosition -                     40, impactY));             }             repaint();         }         label2.setText(String.valueOf(theirScore));     } } 

After all the pucks and the computer's blocker have been moved, and after the scores have been updated, the run method calls the repaint method to update the screen and then sleeps for a number of milliseconds until the next iteration. The speed variable, set to a value from 1 to 100, determines how long the code sleeps between iterations. When the application starts, the speed is set to 50, but the user can change that as he likes using the File menu's Set Speed… itemthe game is very hard to beat at 95, for example. Here's what the repainting and sleeping code looks like at the very end of the run method:

             repaint();             try {                 Thread.sleep(speed);             }             catch (InterruptedException e) {                 System.out.println(e.getMessage());             }         }     } } 

The repaint method calls the update method, and there you simply need to draw the background image and the pucks in the memory imageyou draw the pucks with their drawPuckImage methodand then copy the result to the screen:

 public void update(Graphics g) {     memoryGraphics.drawImage(backGroundImage, 0, 0, this);     for (int loopIndex = 0; loopIndex < pucks.size(); loopIndex++){         if(!stop){             ((Puck)pucks.elementAt(loopIndex)).drawPuckImage                 (memoryGraphics);         }     }     g.drawImage(memoryImage, 0, 0, this); } 



    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