home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / Gamelicator / examples / Boink / Ball.class (.txt) next >
Encoding:
Java Class File  |  1998-04-15  |  1.6 KB  |  49 lines

  1. import com.next.gt.Actor;
  2. import com.next.gt.Gamelication;
  3. import java.awt.Image;
  4.  
  5. public class Ball extends Actor {
  6.    int bounceCount;
  7.    Image theImage;
  8.    int MAX_VELOLCITY_X = 200;
  9.    int MAX_VELOLCITY_Y = 1200;
  10.  
  11.    Ball(Gamelication var1) {
  12.       super.owner = var1;
  13.       super.x = Math.random() * (double)super.owner.getSize().width;
  14.       super.y = Math.random() * (double)super.owner.getSize().height;
  15.       super.velocity_x = (double)((int)Gamelication.randBetween((double)0.5F, (double)1.5F) * 2 - 1) * Math.random() * (double)this.MAX_VELOLCITY_X;
  16.       super.velocity_y = Math.random() * (double)this.MAX_VELOLCITY_Y;
  17.       this.bounceCount = 0;
  18.       this.theImage = super.owner.getImage(super.owner.getCodeBase(), "images/pingPong.gif");
  19.       ((Actor)this).setImage(this.theImage, 4, 16);
  20.       super.currentFrame = (int)Gamelication.randBetween((double)0.0F, (double)super.numFrames);
  21.       super.wrapAround = false;
  22.    }
  23.  
  24.    public void calculateNewPosition() {
  25.       super.calculateNewPosition();
  26.       if (super.x > (double)(super.owner.getSize().width - super.width)) {
  27.          super.velocity_x = -super.velocity_x;
  28.          super.x = (double)(super.owner.getSize().width - super.width);
  29.       } else if (super.x < (double)0.0F) {
  30.          super.velocity_x = -super.velocity_x;
  31.          super.x = (double)0.0F;
  32.       }
  33.  
  34.       if (super.y > (double)(super.owner.getSize().height - super.height)) {
  35.          super.velocity_y = -super.velocity_y * (double)0.75F;
  36.          super.y = (double)(super.owner.getSize().height - super.height);
  37.          if (this.bounceCount++ > 8) {
  38.             this.bounceCount = 0;
  39.             super.velocity_y = -Math.random() * (double)this.MAX_VELOLCITY_Y;
  40.          }
  41.       } else if (super.y < (double)0.0F) {
  42.          super.velocity_y = -super.velocity_y * (double)0.75F;
  43.          super.y = (double)0.0F;
  44.       }
  45.  
  46.       super.velocity_y += (double)5.0F;
  47.    }
  48. }
  49.