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

📄 sprite.java

📁 Source code of J2ME game called Action Soccer. Great for used. Already configurations with JBuilder
💻 JAVA
字号:
package com.soccer;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public abstract class Sprite {
  public Sprite() {
  }

  public void draw(Graphics g) {
    g.setClip( -GameScreen.fieldX + x + 1,
              ( -GameScreen.fieldY + GameScreen.infoHeight + y + (3 * sH) / 4) -
              4, GameScreen.shadow.getWidth(), GameScreen.shadow.getHeight());
    g.drawImage(GameScreen.shadow, -GameScreen.fieldX + x + 1,
                ( -GameScreen.fieldY + GameScreen.infoHeight + y + (3 * sH) / 4) -
                4, 0);
    g.setClip( -GameScreen.fieldX + x,
              -GameScreen.fieldY + GameScreen.infoHeight + y,
              GameScreen.playerWidth, GameScreen.playerHeight);
    g.drawImage(GameScreen.players,
                ( -GameScreen.fieldX + x) -
                GameScreen.playerWidth * (direction % 4),
                ( -GameScreen.fieldY + GameScreen.infoHeight + y) -
                GameScreen.playerHeight * frame -
                3 * GameScreen.playerHeight *
                (type != 0 ? GameScreen.compCommandColor :
                 GameScreen.userCommandColor), 0);
    g.setClip(0, 0, GameScreen.gWidth, GameScreen.gHeight);
    g.setColor(0x80ff80);
    if (active) {
      g.drawArc( ( -GameScreen.fieldX + x) - 3,
                ( -GameScreen.fieldY + GameScreen.infoHeight + y) - 3, sW + 6,
                sH + 6, 0, 360);
    }
  }

  public int distance(Sprite sprite) {
    return ( (x + sW / 2) - (sprite.x + sprite.sW / 2)) *
        ( (x + sW / 2) - (sprite.x + sprite.sW / 2)) +
        ( (y + sH / 2) - (sprite.y + sprite.sH / 2)) *
        ( (y + sH / 2) - (sprite.y + sprite.sH / 2));
  }

  public int distance(int i, int j) {
    return ( (x + sW / 2) - i) * ( (x + sW / 2) - i) +
        ( (y + sH / 2) - j) * ( (y + sH / 2) - j);
  }

  public void setPos(int i, int j) {
    x = i;
    y = j;
    setSpeed();
  }

  public void setSpeed() {
    if (type == 0) {
      speed = 4;
    } else {
      switch (MenuScreen.difficulty) {
        case 0: // '\0'
          speed = 2;
          break;

        case 1: // '\001'
          speed = 3;
          break;

        case 2: // '\002'
          speed = 4;
          break;
      }
    }
  }

  public int move(int i, int j) {
    int k = 0;
    if (type == 2) {
      if (x + i < 10) {
        if (y + j <= 30) {
          x = 10;
          y = 30;
          GameScreen.nearestPlayerDirection = 3;
          return 1;
        }
        if (y + j >= 520) {
          x = 10;
          y = 520;
          GameScreen.nearestPlayerDirection = 1;
          return 1;
        } else {
          y += (j * (x - 10)) / -i;
          x = 10;
          GameScreen.nearestPlayerDirection = 2;
          return 1;
        }
      }
      if (x + i > 340) {
        if (y + j <= 30) {
          x = 340;
          y = 30;
          GameScreen.nearestPlayerDirection = 5;
          return 1;
        }
        if (y + j >= 520) {
          x = 340;
          y = 520;
          GameScreen.nearestPlayerDirection = 7;
          return 1;
        } else {
          y += (j * (340 - x)) / i;
          x = 340;
          GameScreen.nearestPlayerDirection = 6;
          return 1;
        }
      }
      if (y + j < 30) {
        x += (i * (y - 30)) / -j;
        y = 30;
        GameScreen.nearestPlayerDirection = 4;
        return 1;
      }
      if (y + j > 520) {
        x += (i * (520 - y)) / j;
        y = 520;
        GameScreen.nearestPlayerDirection = 0;
        return 1;
      }
      if (x + i > 10 && x + i < 340) {
        x += i;
      }
      if (y + j > 30 && y + j < 520) {
        y += j;
      }
      frame = (frame + 1) % 2;
    } else {
      if (GameScreen.mode != 4 && x + i * speed + sW >= 140 &&
          x + i * speed <= 210 &&
          (y + j * speed >= 0 && y + j * speed <= 40 ||
           y + j * speed >= 510 && y + j * speed <= 550)) {
        return -1;
      }
      if (x + i * speed + sW / 2 > 7 && x + i * speed + sW / 2 < 343) {
        x += i * speed;
      }
      if (y + j * speed + sH / 2 > 27 && y + j * speed + sH / 2 < 523) {
        y += j * speed;
      }
      frame = (frame + 1) % 3;
    }
    correctBallPosition();
    return k;
  }

  public void setTarget(int i, int j, int k) {
    targetReached = false;
    targetX = i;
    targetY = j;
    targetDirection = k;
    setSpeed();
  }

  public boolean goToTarget() {
    if (targetReached) {
      return true;
    }
    if (distanceAfterMove(targetX, targetY, targetX, targetY) <
        distance(targetX, targetY)) {
      moveTo(targetX, targetY, false);
    } else {
      targetReached = true;
      direction = targetDirection;
      setSpeed();
    }
    return targetReached;
  }

  public void moveTo(int i, int j, boolean flag) {
    byte byte0 = 0;
    byte byte1 = 0;
    if (x < i) {
      byte0 = 1;
    } else
    if (x > i) {
      byte0 = -1;
    }
    if (Math.abs(x - i) < speed) {
      byte0 = 0;
    }
    if (y < j) {
      byte1 = 1;
    } else
    if (y > j) {
      byte1 = -1;
    }
    if (Math.abs(y - j) < speed) {
      byte1 = 0;
    }
    direction = detectDirection(byte0, byte1);
    if (move(byte0, byte1) == -1 && move(byte0, 0) == -1) {
      if (move(0, byte1) != -1) {
        ;
      }
    }
  }

  public int distanceAfterMove(int i, int j, int k, int l) {
    byte byte0 = 0;
    byte byte1 = 0;
    if (x < i) {
      byte0 = 1;
    } else
    if (x > i) {
      byte0 = -1;
    }
    if (Math.abs(x - i) < speed) {
      byte0 = 0;
    }
    if (y < j) {
      byte1 = 1;
    } else
    if (y > j) {
      byte1 = -1;
    }
    if (Math.abs(y - j) < speed) {
      byte1 = 0;
    }
    if (GameScreen.mode != 4 && x + byte0 * speed + sW >= 140 &&
        x + byte0 * speed <= 210 &&
        (y + byte1 * speed >= 0 && y + byte1 * speed <= 40 ||
         y + byte1 * speed >= 510 && y + byte1 * speed <= 550)) {
      byte0 = byte1 = 0;
    }
    if (x + byte0 * speed + sW / 2 > 7 && x + byte0 * speed + sW / 2 < 343) {
      i = x + sW / 2 + byte0 * speed;
    } else {
      i = x + sW / 2;
    }
    if (y + byte1 * speed + sH / 2 > 27 && y + byte1 * speed + sH / 2 < 523) {
      j = y + sH / 2 + byte1 * speed;
    } else {
      j = y + sH / 2;
    }
    return (i - k) * (i - k) + (j - l) * (j - l);
  }

  public int detectDirection(int i, int j) {
    if (j == -1) {
      if (i == -1) {
        return 7;
      }
      return i != 1 ? 0 : 1;
    }
    if (j == 1) {
      if (i == -1) {
        return 5;
      }
      return i != 1 ? 4 : 3;
    }
    if (i == -1) {
      return 6;
    }
    return i != 1 ? -1 : 2;
  }

  public void setBallOwn() {
    hasBall = true;
    GameScreen.ball.ballSpeed = Ball.speeds.length;
    GameScreen.ball.lastBallOwner = type;
    GameScreen.stepsDone = 0;
    correctBallPosition();
  }

  public void correctBallPosition() {
    if (hasBall) {
      switch (direction) {
        case 0: // '\0'
          GameScreen.ball.setPos(x +
                                 (sW - ( (Sprite) (GameScreen.ball)).sW) / 2,
                                 y - ( (Sprite) (GameScreen.ball)).sH / 2);
          break;

        case 1: // '\001'
          GameScreen.ball.setPos(x + (sW * 3) / 4, y);
          break;

        case 2: // '\002'
          GameScreen.ball.setPos( (x + sW) -
                                 ( (Sprite) (GameScreen.ball)).sW / 2,
                                 y +
                                 (sH - ( (Sprite) (GameScreen.ball)).sH) / 2);
          break;

        case 3: // '\003'
          GameScreen.ball.setPos(x + (sW * 3) / 4, y + sH / 2);
          break;

        case 4: // '\004'
          GameScreen.ball.setPos(x +
                                 (sW - ( (Sprite) (GameScreen.ball)).sW) / 2,
                                 (y + sH) -
                                 ( (Sprite) (GameScreen.ball)).sH / 2);
          break;

        case 5: // '\005'
          GameScreen.ball.setPos(x, y + sH / 2);
          break;

        case 6: // '\006'
          GameScreen.ball.setPos(x - ( (Sprite) (GameScreen.ball)).sW / 2,
                                 y +
                                 (sH - ( (Sprite) (GameScreen.ball)).sH) / 2);
          break;

        case 7: // '\007'
          GameScreen.ball.setPos(x, y);
          break;
      }
    }
  }

  public int steps(int i, int j, int k, int l) {
    if (Math.abs(i - k) > Math.abs(j - l)) {
      return Math.abs(i - k) / speed;
    } else {
      return Math.abs(j - l) / speed;
    }
  }

  public int x;
  public int y;
  public int direction;
  public int frame;
  public int targetDirection;
  public int targetX;
  public int targetY;
  public int type;
  public int speed;
  public int sW;
  public int sH;
  public boolean hasBall;
  public boolean targetReached;
  public boolean active;
}

⌨️ 快捷键说明

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