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

📄 manbullets.java

📁 该文件主要是为了给要快速度的 了解 java 的 0基础的 人 给予 成功的 快感... 也是 我 对 本站的支持
💻 JAVA
字号:
package man;

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.Random;

/**
 * 子弹操作类
 */

public class ManBullets {

  private int isPower;           // 绝妙值
  private ManGameCanvas manGC;   // 主类对象
  // 子弹运动的坐标轨迹,Locus[i][j]
  // i为子弹数,j为0-5,表示:5种子弹类型,位置x,y,速度vx,vy
  private int[][] Locus;
  private Sprite Bullets;        // 子弹图象
  private Random rnd;            // 随机种子
  private int SWidth, SHeight;   // 设备屏幕的宽度与高度
  private int speed;

  //--- 构造函数 ----------------------------------------------------------------
  public ManBullets(ManGameCanvas manGC, Image img, int SWidth, int SHeight) {
    this.manGC = manGC;
    Bullets = new Sprite(img, img.getWidth(), img.getHeight());
    Bullets.setFrame(0);

    int bullets=18;
    if(SWidth>180&&SHeight>180){
      bullets=bullets*3/2;
    }
    else if(SWidth>240&&SHeight>240){
      bullets=bullets*2;
    }
    Locus = new int[bullets][5];//[20][5]
    rnd = new Random();
    this.SWidth = SWidth;
    this.SHeight = SHeight;
  }

  //--- 初始化运行轨迹 -----------------------------------------------------------
  public void InitBulletsLocus() {
    this.speed=speed;
    for (int i = 0; i < Locus.length; i++) {
      InitBulletLocus(i);
    }
  }

  //--- 初始化运行轨迹随机数组 ----------------------------------------------------
  private void InitBulletLocus(int i) {
    // 子弹的类型,0:上 1:下 2:左 3:右
    Locus[i][0] = (rnd.nextInt() & 0x7fffffff) % 4;//%4
    switch (Locus[i][0]) {
      case 0:
        Locus[i][1] = -5;
        Locus[i][2] = (rnd.nextInt() & 0x7fffffff) % SHeight;
        Locus[i][3] = (rnd.nextInt() & 0x7fffffff) % (speed) + 1; //vx %3
        Locus[i][4] = (rnd.nextInt()) % (speed+2); //vy %3
        break;
      case 1:
        Locus[i][1] = SWidth + 5;
        Locus[i][2] = (rnd.nextInt() & 0x7fffffff) % SHeight;
        Locus[i][3] = ((rnd.nextInt() & 0x7fffffff) % (speed+2) + 1) * -1; //vx %3
        Locus[i][4] = (rnd.nextInt()) % (speed); //vy %3
        break;
      case 2:
        Locus[i][1] = (rnd.nextInt() & 0x7fffffff) % SWidth;
        Locus[i][2] = -5;
        Locus[i][3] = (rnd.nextInt()) % (speed); //vx
        Locus[i][4] = (rnd.nextInt() & 0x7fffffff) % (speed+2) + 1; //vy %3
        break;
      case 3:
        Locus[i][1] = (rnd.nextInt() & 0x7fffffff) % SWidth;
        Locus[i][2] = SHeight + 5;
        Locus[i][3] = (rnd.nextInt()) % (speed+2); //vx %3
        Locus[i][4] = ((rnd.nextInt() & 0x7fffffff) % (speed) + 1) * -1; //vy %3
        break;
    }
  }

  //--- 显示子弹 ----------------------------------------------------------------
  public void paint(Graphics g, int planeX, int planeY) {
    for (int i = 0; i < Locus.length; i++) {
      if (isPower(planeX, planeY, i, 12)) {  // 绝妙判断
        if(isPower>=49) continue;
        isPower++;
      }
      if (isCollision(planeX, planeY, i, 6)) {   // 碰撞判断
        if (!manGC.getGameInfo()) manGC.setGameInfo(true);
        manGC.getSpExplosion().setPosition(Locus[i][1] - 16, Locus[i][2] - 16);
        continue;
      }
      Bullets.setPosition(Locus[i][1], Locus[i][2]);
      Bullets.paint(g);
      UpdataBulletLocus(i); // 更新运行轨迹数组
    }
  }

  //--- 更新运行轨迹数组 ---------------------------------------------------------
  private void UpdataBulletLocus(int i) {
    Locus[i][1] += Locus[i][3];
    Locus[i][2] += Locus[i][4];
    if (Locus[i][1] < -5 || Locus[i][1] > SWidth + 5) {
      Locus[i][3] *= -1;
    }
    if (Locus[i][2] < -5 || Locus[i][2] > SHeight + 5) {
      Locus[i][4] *= -1;
    }
  }

  //--- 碰撞判断 ----------------------------------------------------------------
  private boolean isCollision(int planeX, int planeY, int i, int range) {
    boolean result = false;
    // 将飞机近似的看成圆,取得其圆心坐标
    int planeXCenter  = planeX + 12;//12
    int planeYCenter  = planeY + 12;//12
    // 同理,取得子弹的圆心坐标
    int bulletXCenter = Locus[i][1] + 3;//3
    int bulletYCenter = Locus[i][2] + 3;//3
    // 理由半径检测,小于range即已碰撞
    if (Math.abs(planeXCenter - bulletXCenter) < range) {
      if (Math.abs(planeYCenter - bulletYCenter) < range) {
        result = true;
        isPower=0;
      }
    }
    return result;
  }

  //--- 绝妙判断 ----------------------------------------------------------------
  private boolean isPower(int planeX, int planeY, int i, int range) {
    boolean power=false;
    // 将飞机近似的看成圆,取得其圆心坐标
    int planeXCenter = planeX + 12; //12
    int planeYCenter = planeY + 12; //12
    // 同理,取得子弹的圆心坐标
    int bulletXCenter = Locus[i][1] + 3;//3
    int bulletYCenter = Locus[i][2] + 3;//3
    // 理由半径检测,小于range即一绝妙
    if (Math.abs(planeXCenter - bulletXCenter) < range) {
      if (Math.abs(planeYCenter - bulletYCenter) < range) {
        power=true;
      }
    }
    return power;
  }

  //--- 炸弹爆炸操作 ------------------------------------------------------------
  public void BombBullets(int planeX, int planeY) {
    for (int i = 0; i < Locus.length; i++) {
      if (isCollision(planeX, planeY, i, 32)) {//爆炸半径 32
        InitBulletLocus(i);
      }
    }
  }

  //--- 设置游戏难度 ------------------------------------------------------------
  public void setSpeed(int gamelevel){
    if(gamelevel==1){
      speed=2;
    }
    if(gamelevel==2){
      speed=4;
    }
    if(gamelevel==3){
      speed=4;
    }
  }
  //--- 设置能量值 --------------------------------------------------------------
  public void setPower(int power){
    this.isPower=power;
  }

  //--- 得到能量值 --------------------------------------------------------------
  public int getPower(){
    return isPower;
  }
}

⌨️ 快捷键说明

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