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

📄 fly.java

📁 类似于推箱子的手机游戏源码
💻 JAVA
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.Random;

/**
 *
 * @author TOM
 */
public class Fly {
  private Hunt_Canvas manCanvas;  //游戏画面
  private int[] Locus;        
  private Sprite fly;       
  private Random rnd;           // 随机种子
  private int SWidth, SHeight;  // 设备屏幕的宽度与高度
 
  static int t=0;

  public Fly(Hunt_Canvas manCanvas, Image img, int SWidth, int SHeight) {
    this.manCanvas = manCanvas;
    fly = new Sprite(img, img.getWidth()/3, img.getHeight());
    fly.setFrame(0);
    Locus = new int[5];   
    rnd = new Random();
    this.SWidth = SWidth;
    this.SHeight = SHeight;
   
  }


  // 初始化运行轨迹随机数组
  public void InitFlyLocus() {
    
    Locus[0] = (rnd.nextInt() & 0x7fffffff) % 4; 
    switch (Locus[0]) {
      case 0:
        Locus[1] = -50; //初始X坐标值
        Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40); //初始的Y坐标值
        Locus[3] = (rnd.nextInt() & 0x7fffffff) % 12 + 7; //X坐标增量
        Locus[4] = 0; //Y坐标增量
       
        
        break;
      case 1:
        Locus[1] = SWidth + 50;
        Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40);
        Locus[3] = ( (rnd.nextInt() & 0x7fffffff) % 12 + 7) * -1;
        Locus[4] = 0;
      
        
        break;
      case 2:
        Locus[1] = -50;
        Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40);
        Locus[3] = (rnd.nextInt() & 0x7fffffff) % 12 + 7; 
        Locus[4] = (rnd.nextInt()) % 10+5;
       
       
        break;
      case 3:
        Locus[1] = SWidth + 50;
        Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40);
        Locus[3] = (rnd.nextInt() & 0x7fffffff) % 12 + 7;
        Locus[4] = (rnd.nextInt()) % 10+5;
        
        
        break;
    }
  }
  // 显示
  public void paint(Graphics g) {
    
      fly.setPosition(Locus[1], Locus[2]);
      fly.nextFrame();
      fly.paint(g);
      UpdataFlyLocus(); // 更新运行轨迹数组
    }
  
  // 更新运行轨迹数组 
  private void UpdataFlyLocus() {
      
      Locus[1] += Locus[3];
      if(Locus[4]!=0){
        t++;
        if(t<10){
           Locus[2] += Locus[4]; 
        }
        else{
           Locus[4]*=-1;
           t=0;
        }
      }
      else 
          Locus[2] += (rnd.nextInt()) % 10+5;
    //超出边界的处理
    if (Locus[1] < -50 || Locus[1] > SWidth + 50) {
      InitFlyLocus();
      manCanvas.setFlag(4,false);
    }
    if (Locus[2] < 0 || Locus[2] > SHeight-40 ) {
      Locus[4] *= -1;
    }
  }
  public Sprite getFly(){
    return fly;
  }
}

⌨️ 快捷键说明

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