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

📄 balloon.java

📁 BravePiglet.rar 勇敢的猪Java游戏 由本人原创
💻 JAVA
字号:
package com.lovo.pig.model;

import java.awt.Graphics;
import java.awt.Rectangle;

import com.lovo.pig.view.MainFrame;

public class Balloon{
	private MainFrame mf;
	//气球基准位置
	public int x = 5,y = 422;
	//气球的校正位置(纠正误差)
	private int imgX = 5,imgY = 422;
    //气球图像下标
	private int indexImg = -1;
	//气球起飞位置,与狼起飞位置相同
	public int flyX;
	//气球是否被击中,用于碰撞检测
	public boolean hitted = false;
	//气球是否可见
    public boolean visible = false;
    //吊在气球下的狼
    public Wolf wolf;
    
	public Balloon(MainFrame mf,Wolf w){
		this.mf = mf;
		wolf = w;
		this.flyX=w.flyX;//取得狼起飞位置
	}

	/**
	 * 绘制气球
	 * @param g
	 */
	public void draw(Graphics g) {
		if(visible){
			g.drawImage(mf.images.getImgBalloons()[indexImg],imgX,imgY,null);	
		}
		move();
	}

    /**
     * 改变气球位置和图像,必须与狼的位置相对应
     */
	public void move() {
		if(!hitted){       
			if(x < flyX){//起飞前,前进,不可见
		        x = x + 5;  
		    }else if(x == flyX  ){//起飞后,气球可见,吹气球,只吹一次
		    	 this.visible = true;
		    	 indexImg++;
		    	 imgX = x + 60;
	    	     imgY = y + 105;
		    	 if(indexImg > 4){
	    	    	   x = x + 1;
	    	     }
	        }else if( x > flyX && y >= -20 ){//起飞后上升,可越界
	            y = y - 6;
	            if(!wolf.live){//狼挂了时,气球加速上升
	            	y -=15;
	            }
	            imgX = x ;
	    	    imgY = y;
	       }
		}else{//被击中后,连续绘制后四张图像(只绘制一次)
	    	     indexImg ++;
	         if(indexImg > 9) {
	 			mf.balloons.remove(this);//最后一张绘制后,清除气球对象
	         }	
		}
		
		if(y < -20){
			mf.balloons.remove(this);//越界到一定程度后后,清除气球对象
		}
      }
    
    /**
     * 返回气球边界矩形,用于碰撞检测
     * @return
     */
	public Rectangle getRect(){
		Rectangle rect = new Rectangle(x + 40,y + 20,37,51);
		return rect;
	}
}

⌨️ 快捷键说明

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