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

📄 balloon.java

📁 类似于推箱子的手机游戏源码
💻 JAVA
字号:
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
/*
 * Balloon.java
 *
 * Created on 2007年10月29日, 下午10:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author liang
 */
public class Balloon {
    
    /**
     * Creates a new instance of Balloon
     */
      private Hunt_Canvas manCanvas;  //游戏画面
      private int[] Locus;        //存储鸟数组
      private Sprite balloon;       // 鸟图象
      private Random rnd;           // 随机种子
      private int SWidth, SHeight;  // 设备屏幕的宽度与高度
      private boolean isFly;
      private static int t=0;     
      private boolean isActive;
      private int flag;
  
    public Balloon(Hunt_Canvas manCanvas, Image img, int SWidth, int SHeight,int flag) {
    this.manCanvas = manCanvas;    
    balloon = new Sprite(img, img.getWidth()/4, img.getHeight());  
    balloon.setFrame(0);
    Locus = new int[5];   
    rnd = new Random();
    this.SWidth = SWidth;
    this.SHeight = SHeight;   
    isActive = true;
    this.flag = flag;
  }
    
    public void InitBalloonLocus() {
        Locus[4] = (rnd.nextInt() & 0x7fffffff) % 2;  
        switch(Locus[4]){
            case 0:
                isFly = true;
                break;
            case 1:
                isFly = false;
                break;
        }
//        System.out.println(isFly);
        Locus[0] = (rnd.nextInt() & 0x7fffffff) % (SWidth-60); //初始的x坐标值
        Locus[1] = SHeight + 60; //初始y坐标值
        Locus[2] = (rnd.nextInt() & 0x7fffffff) % 6; //X坐标增量
        Locus[3] = -( (rnd.nextInt() & 0x7fffffff) % 5 + 3 ); //y坐标增量             
                
  }
  // 显示
  public void paint(Graphics g) {
    
     balloon.setPosition(Locus[0], Locus[1]);
     balloon.paint(g);
     
     if(isActive){
         balloon.setFrame(0);     
     }
     else if(!isActive ){ 
         if(balloon.getFrame() == 0)
            balloon.setFrame(1);
         else if( balloon.getFrame() == 3){           
             manCanvas.setFlag(flag, false);
             InitBalloonLocus();
             isActive = true;
         }
         else 
            balloon.nextFrame();
     } 
                   
      UpdataBalloonLocus(); // 更新运行轨迹数组
    
  }
  // 更新运行轨迹数组 
  private void UpdataBalloonLocus() {
      if(isActive && isFly){
            Locus[1] += Locus[3];         
            t++;
            if(t<8){
               Locus[0] += Locus[2];                
            }
            else{
               Locus[2]*=-1;
               t=0;
            }
      }
      else if(isActive && !isFly){
             Locus[1] += Locus[3];     
      }    
      
    //超出边界的处理
    if (Locus[1] < -60) {
          InitBalloonLocus();
          manCanvas.setFlag(flag, false);
          isActive = true;
    }
    if (Locus[0] < -10 || Locus[0] > SWidth - 50) {
        Locus[2] *= -1;
    }
      
  }

  public Sprite getBalloon(){
    return balloon;
  }

  public boolean getIsBalloon(){
         return isActive;
  }
     
 public void setIsBalloon(boolean f){
     isActive = f;
 }
 
 
}
    

⌨️ 快捷键说明

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