home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / de86gnzn / examples / boink / ball.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  1.6 KB  |  77 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(Gamelet theOwner) {
  23.  
  24.   owner= theOwner;
  25.   x= (Math.random()*owner.size().width);
  26.   y= (Math.random()*owner.size().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.size().width - width)) {
  50.       velocity_x= - velocity_x;
  51.       x= owner.size().width - width;
  52.   }
  53.   else if (x < 0) {
  54.     velocity_x= - velocity_x;
  55.     x= 0;
  56.   }
  57.   if (y > (owner.size().height - height)) {
  58.     velocity_y= - velocity_y * 0.75;
  59.     y= owner.size().height - height;
  60.     if(bounceCount++ > 8) {        // give it a kick of energy after a while
  61.       bounceCount= 0;
  62.       velocity_y= -Math.random() * MAX_VELOLCITY_Y;
  63.     } /*endif*/
  64.   
  65.   }
  66.   else if (y < 0) {
  67.     velocity_y= - velocity_y * 0.75;
  68.     y= 0;
  69.   }
  70.   
  71.   velocity_y+= 5; 
  72.  
  73. } /*calculateNewPosition*/
  74.  
  75.  
  76. } /*Ball*/
  77.