⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ball.java

📁 手机游戏的源代码
💻 JAVA
字号:
import javax.microedition.lcdui.*;

public class Ball {
  int x, y; //坐标
  int spd; //速度
  int Vx, Vy; //速度分量

  boolean moving; //是否移动
  int nx, ny; //下一次的坐标
  byte arraySign; //球在数组中的编号
  boolean onBag;//是否在袋上
  boolean alive; //是否在桌上
  int standForceX, standForceY;//外力的X,Y值
  int hittedBalls;//碰到过的球
  byte style;//球的样式
  byte ballSign;


  public Ball(Ball ball) {
    this.x = ball.x;
    this.y = ball.y;
    nx = ball.nx;
    ny = ball.ny;
    Vx = ball.Vx;
    Vy = ball.Vy;
    arraySign = ball.arraySign;
    alive = ball.alive;
    standForceX = ball.standForceX;
    standForceY = ball.standForceY;
    hittedBalls = ball.hittedBalls;
    onBag = ball.onBag;
    moving = ball.moving;
    style = ball.style;
    ballSign = ball.ballSign;

  }
  /**
   * @param x int X坐标
   * @param y int Y坐标
   * @param s int 编号
   * @param t int 样式
   */
  public Ball(int x, int y, int as, int bs) {
    this.x = x * 100;
    this.y = y * 100;
    nx = x * 100;
    ny = y * 100;
    Vx = 0;
    Vy = 0;
    arraySign = (byte) as;
    alive = true;
    standForceX = 0;
    standForceY = 0;
    hittedBalls = 0x0;
    onBag = false;
    moving = false;
    ballSign = (byte) bs;
    switch(bs){
      case 0: style = a.STYLE_WHITE;break;
      case 8: style = a.STYLE_BLACK;break;
      default:
        if( bs < 8){
          style = a.STYLE_SOLID;
        }
        else{
          style = a.STYLE_STRIPE;
        }
    }
  }
  /**
   * 画球
   * @param g Graphics
   * @param ballImage Image
   */
  public void drawBall(Graphics g, Image ballImage) {
    g.drawImage(ballImage, x / 100, y / 100, g.HCENTER | g.VCENTER);
    g.setColor(0xffffff);
    g.setFont(Font.getFont(0, 0, Font.SIZE_SMALL));
    g.drawString("" + arraySign, x / 100, y / 100 - 6, g.HCENTER | g.TOP);
    if (onBag) {
//      spd = 0;
//      a.setBallsState(arraySign,false);
      onBag = false;
    }
  }
  /**
   * 白球受力
   * @param force int
   * @param x1 int
   * @param y1 int
   */
  public void onHit(int force, int x1, int y1) {
    a.setBallsState(0, true);
    int s = a.sqrt( (x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
    Vx = (x1 - x) * 10000 / s;
    Vy = (y1 - y) * 10000 / s;
    spd = force;
    moving = true;
  }

  /**
   * 移动一浈
   */
  public void move() {
    spd = spd * 97 / 100;
    if (spd <= 70) {
      a.setBallsState(arraySign, false);
      spd = 0;
      moving = false;
      if (a.think) { //-------------think---------------------------------------
        if (arraySign == 0) {
          a.whiteBallStop = true;
        }
        else
        if (arraySign == a.curHitSign) {
          a.curBallStop = true;
        }
      } //------------------------end think--------------------------------
    }
  }

  /**
   * 移动一步
   */
  public void moveStep() {
    x = nx;
    y = ny;
    if (inBag()) {
      if (a.think) { //---------think-----------------------------------------
        if (this.arraySign == a.curHitSign) {
          a.curBallIn = true;
        }
        else
        if (this.arraySign == 0) {
          a.whiteBallIn = true;
          if (a.curTrackSign == 0) {
            a.fail = true;
          }
        }
      } //--------------------end think--------------------------------------
      else{
        if(!a.firstIn){
//          a.firstIn = true;
          switch(this.style){
            case a.STYLE_WHITE:
              break;
            case a.STYLE_BLACK:
              break;
            case a.STYLE_SOLID:
              a.setTargetBalls(a.isPlayerTurn,a.KIND_SOLID);
              a.setTargetBalls(!a.isPlayerTurn,a.KIND_STRIPE);
              break;
            case a.STYLE_STRIPE:
              a.setTargetBalls(a.isPlayerTurn,a.KIND_STRIPE);
              a.setTargetBalls(!a.isPlayerTurn,a.KIND_SOLID);
              break;
          }
        }
      }
      spd = 0;
      a.setBallsState(arraySign, false);
      alive = false;
    }
    resetData();
  }
  /**
   * 复位一些数据
   */
  public void resetData() {
    hittedBalls = 0;
    standForceX = 0;
    standForceY = 0;
  }
  /**
   * 计算和球的碰撞,得到一个新的状态
   */
  public void getNextPost() {
    for (int i = 0; i < a.tempBalls.length; i++) {
      if (i == arraySign
          || !a.tempBalls[i].alive
          || ( (hittedBalls >>> i) & 0x00000001) == 1
          ) {
        continue;
      }
      if (isNear(a.tempBalls[i])) {
        hit(a.tempBalls[i], true);
      }
    }
  }
  /**
   * 得到下一个不和球碰撞的坐标,和边碰撞的结果实现
   */
  public void getNextSet() {
    nx = x + spd * Vx / (a.t * 10000);
    ny = y + spd * Vy / (a.t * 10000);
    if (nx <= a.SIDE) {
      if ( (ny > 1960 && ny < 9134) || ny > 10866 && ny < 18040) {
        nx += (a.SIDE - nx) * 2;
        Vx = Math.abs(Vx);
      }
    }
    if (nx >= a.w - a.SIDE) {
      if ( (ny > 1960 && ny < 9134) || ny > 10866 && ny < 18040) {
        nx -= (nx - a.w + a.SIDE) * 2;
        Vx = -Math.abs(Vx);
      }
    }
    if (ny <= a.SIDE) {
      if (nx > 1960 && nx < 13040) {
        ny += (a.SIDE - ny) * 2;
        Vy = Math.abs(Vy);
      }
    }
    if (ny >= a.h - a.SIDE) {
      if (nx > 1960 && nx < 13040) {
        ny -= (ny - a.h + a.SIDE) * 2;
        Vy = -Math.abs(Vy);
      }
    }
  }
  /**
   * 判断是否撞到了其他球
   * @param ball Ball 被撞的球
   * @return boolean 返回是否撞到
   */
  private boolean isNear(Ball ball) {
    int dtx = Math.abs(nx - ball.nx);
    int dty = Math.abs(ny - ball.ny);
    if (dtx <= a.ballD && dty <= a.ballD) {
      if ( ( (dtx * dtx) + (dty * dty)) <= a.ballDD) {
        return true;
      }
    }
    return false;
  }
  /**
   * 撞到其他的球
   * @param ball Ball 撞到的球
   * @param initiative boolean 是否是主动撞到的
   */
  public void hit(Ball ball, boolean initiative) {
    if (a.think) { //--------------think--------------------------------------------------------
      if (a.curTrackSign == 0 && arraySign == 0) { //---------------当前目标球为白球
        if (ball.arraySign == a.curHitSign) { //---------撞到的球是目标球
          a.curTrackSign = a.curHitSign; //-------设置当前跟踪目标球
          a.curBallStop = false;
        }
        else { //----------------------------------撞到的球非目标球
          if (a.PRINT) System.out.println("!!!!!!!!!!" + "应该碰:" + a.curHitSign +"  结果是" + ball.arraySign); ///////////////
          a.fail = true; //-----------------------失败
          a.haveSet = false;
        }
      }
      else
      if (a.curTrackSign == a.curHitSign && arraySign == a.curHitSign) { //----------------------------------当前目标球为被碰撞球
        if (ball.arraySign != 0) { //------------------撞到的不是白球
          a.thinkStop = true; //----------------思考停止
          if (a.PRINT) System.out.println(
              "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!碰到的球:" + ball.arraySign +"  跟踪的球:" + a.curTrackSign);
        }
      }
      if (ball.arraySign == 0) {
        a.whiteBallStop = false;
      }
    } //------------------------end think-------------------------------------------------------

    a.setBallsState(arraySign, true); //将自己的状态改为动态
    hittedBalls |= (0x00000001 << ball.arraySign);
    boolean haveSpd = false;

    if (moving) {
      int c = 0, d = 0;
      c = ball.nx - nx;
      d = ball.ny - ny;

//      if(a.haveSet && ball.arraySign == a.curHitSign){//-------AI打中正确的球时-------
      if (a.haveSet) {
        if (a.think) {
          if (ball.arraySign == a.curHitSign && this.arraySign == 0) {
            c = ball.nx - a.AIx;
            d = ball.ny - a.AIy;
          }
        }
        else {
          if (ball.arraySign == (byte) a.dataLevel[a.hitLevel][6] && this.arraySign == 0) {
            c = ball.nx - a.dataLevel[a.hitLevel][3];
            d = ball.ny - a.dataLevel[a.hitLevel][4];
          }
        }
        a.haveSet = false;
      }

      int m = spd * Vx / 10200,
          n = spd * Vy / 10200;

      int temp0 = c * m + d * n;
      if (temp0 > 0) {
        int temp1 = c * c + d * d;
        int x1 = c * temp0 / temp1,
            y1 = d * temp0 / temp1;
        int x0 = m - x1,
            y0 = n - y1;
        ball.addForce(x1, y1);
        this.addForce(x0, y0);
      }
      else {
        haveSpd = true;
      }
    }
    if (initiative) {
      ball.hit(this, false);
      composeForce(haveSpd);
    }
    else {
      composeForce(haveSpd);
      return;
    }
  }

  /**
   * 给球加一个力量
   * @param forceX int X力量
   * @param forceY int Y力量
   */
  public void addForce(int forceX, int forceY) {
    standForceX += forceX;
    standForceY += forceY;
  }

  /**
   * 碰撞后计算合力
   * @param haveSpeed boolean 是否保留自己之前的速度
   */
  public void composeForce(boolean haveSpeed) {
    if (haveSpeed) {
      standForceX += (spd * Vx / 10000);
      standForceY += (spd * Vy / 10000);
    }
    spd = a.sqrt(standForceX * standForceX + standForceY * standForceY);
    if (spd <= 80) {
      spd = 0;
      Vx = 0;
      Vy = 0;
      moving = false;
    }
    else {
      Vx = standForceX * 10000 / spd;
      Vy = standForceY * 10000 / spd;
      moving = true;
    }

    standForceX = 0;
    standForceY = 0;
  }
  /**
   * 球进袋
   * @return boolean 是否进袋
   */
  public boolean inBag() {
    int dtx, dty;
    if (x <= 1960) {

      if (y >= 9134 && y <= 10866) { //1
        dtx = x - 500;
        dty = y - 10000;
        if ( (dtx * dtx + dty * dty) < 1000000) {
          x = 1100;
          y = 10000;
          onBag = true;
          return true;
        }
      }
      else if (y <= 1960) { //0
        dtx = x;
        dty = y;
        if ( (dtx * dtx + dty * dty) < 4840000) {
          x = 1100;
          y = 1100;
          onBag = true;
          return true;
        }
      }
      else if (y >= 18040) { //2
        dtx = x;
        dty = y - 20000;
        if ( (dtx * dtx + dty * dty) < 4840000) {
          x = 1100;
          y = 18900;
          onBag = true;
          return true;
        }
      }
    }
    else if (x >= 13040) {
      if (y >= 9134 && y <= 10866) { //4
        dtx = x - 14500;
        dty = y - 10000;
        if ( (dtx * dtx + dty * dty) < 1000000) {
          x = 13900;
          y = 10000;
          onBag = true;
          return true;
        }
      }
      else if (y <= 1960) { //3
        dtx = x - 15000;
        dty = y;
        if ( (dtx * dtx + dty * dty) < 4840000) {
          x = 13900;
          y = 1100;
          onBag = true;
          return true;
        }
      }
      else if (y >= 18040) { //5
        dtx = x - 15000;
        dty = y - 20000;
        if ( (dtx * dtx + dty * dty) < 4840000) {
          x = 13900;
          y = 18900;
          onBag = true;
          return true;
        }
      }
    }
    return false;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -