home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boink / Ball.java < prev    next >
Encoding:
Java Source  |  1998-04-15  |  1.7 KB  |  81 lines

  1. /**
  2.  *
  3.  * Ball.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    1.0
  6.  * Feb 23/1996
  7.  *
  8.  * A bouncy ball.
  9.  *
  10.  */
  11.  
  12. import java.lang.Math;
  13. import java.awt.Image;
  14. import com.next.gt.*;
  15.  
  16. public class Ball extends Actor {
  17.   int            bounceCount;
  18.   Image            theImage;
  19.   int            MAX_VELOLCITY_X= 200;
  20.   int            MAX_VELOLCITY_Y= 1200;
  21.   
  22. Ball(Gamelication theOwner) {
  23.  
  24.   owner= theOwner;
  25.   x= (Math.random()*owner.getSize().width);
  26.   y= (Math.random()*owner.getSize().height);
  27.       velocity_x= (double)((int)owner.randBetween(0.5,1.5)*2 - 1) * Math.random()*MAX_VELOLCITY_X;
  28.   velocity_y= Math.random()*MAX_VELOLCITY_Y;
  29.   bounceCount= 0;
  30.  
  31.   theImage= owner.getImage(owner.getCodeBase(), "images/pingPong.gif");
  32.  
  33.   setImage (theImage, 4, 16);
  34.   currentFrame= (int) owner.randBetween(0., (double)numFrames);
  35.   wrapAround= false;
  36.   
  37. } /*Ball()*/
  38.  
  39.  
  40.  
  41. /**
  42.  * Handle left rotation.
  43.  */
  44. public void calculateNewPosition() {
  45.   super.calculateNewPosition();
  46.   //
  47.   // check for out of bounds and rebound
  48.   //
  49.   if (x > (owner.getSize().width - width)) {
  50.       velocity_x= - velocity_x;
  51.       x= owner.getSize().width - width;
  52.       //owner.play( "./", "sounds/bounce.au" );
  53.   }
  54.   else if (x < 0) {
  55.     velocity_x= - velocity_x;
  56.     x= 0;
  57.     //owner.play( "./", "sounds/bounce.au" );
  58.   }
  59.   if (y > (owner.getSize().height - height)) {
  60.     velocity_y= - velocity_y * 0.75;
  61.     y= owner.getSize().height - height;
  62.     //owner.play( "./", "sounds/bounce.au" );
  63.     if(bounceCount++ > 8) {        // give it a kick of energy after a while
  64.       bounceCount= 0;
  65.       velocity_y= -Math.random() * MAX_VELOLCITY_Y;
  66.     } /*endif*/
  67.   
  68.   }
  69.   else if (y < 0) {
  70.     velocity_y= - velocity_y * 0.75;
  71.     y= 0;
  72.     //owner.play( "./", "sounds/bounce.au" );
  73.   }
  74.   
  75.   velocity_y+= 5; 
  76.  
  77. } /*calculateNewPosition*/
  78.  
  79.  
  80. } /*Ball*/
  81.