模拟飞翔的小鸟android版 文档二

本文介绍了一款游戏中水管移动及小鸟与水管碰撞检测的实现方法。通过动态调整水管位置并利用矩形碰撞检测来判断小鸟是否碰到水管,实现了游戏的核心玩法。

这节主要是处理水管移动,以级水管与小鸟碰撞检测


水管manager

/**
 * 为了水管图片拉申会变形,采取了从一张大长管图片载取一部分(包括圆头),长直部分载取,这样圆头就不会变形
 * 先保存一张800*50的长圆管,然后获取变量temp,从而得出要载取图片的大小
 * @author Administrator
 *
 */
public class ObstacleManager {

	Context mContext;
	Bitmap bitmap_up, bitmap_down;
	float top, mScreenWidth, mScreenHeight;
	float left1, left2, left3, left4;
	float temp1,temp2,temp3,temp4;
	int bitmap_Obstacle_Width, bitmap_Obstacle_Height;
	RectF rectF1, rectF2, rectF3, rectF4, rectF5, rectF6, rectF7, rectF8;
	Rect rect1,rect2,rect3,rect4,rect5,rect6,rect7,rect8;
	boolean isImpact1,isImpact2,isImpact3,isImpact4,isImpact5,isImpact6,isImpact7,isImpact8;
	Random random = new Random();
	int randomMax;
	int speedNum = 5;
	Timer timer = new Timer(true);
	BirdManager birdManager;
	boolean isPlay = true;
	
	public ObstacleManager(Context context, float screenWidth,
			float screenHeight) {
		birdManager = MyGameView.getPlayerManager();
		mContext = context;
		mScreenWidth = screenWidth;
		mScreenHeight = screenHeight;
		left1 = mScreenWidth;
		left2 = mScreenWidth + mScreenWidth / 3;
		left3 = mScreenWidth + mScreenWidth *2/ 3;
		//left4 = mScreenWidth + mScreenWidth;
		top = 0;
		randomMax = (int) (mScreenHeight/100);
		System.out.println("randomMax="+randomMax);
		temp1 = mScreenHeight/2;
		temp2 = mScreenHeight/2-100;
		temp3 = mScreenHeight/2+250;
		temp4 = mScreenHeight/2-200;
		bitmap_down = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.downl);
		bitmap_up = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.upl);
		bitmap_Obstacle_Width = bitmap_down.getWidth();
		bitmap_Obstacle_Height = bitmap_down.getHeight();
		
		rect1 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp1), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
		rect2 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp1-100);
		
		rect3 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp2), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
		rect4 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp2-100);
		
		rect5 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp3), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
		rect6 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp3-100);
		
//		rect7 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp4), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
//		rect8 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp4-100);
	
		timer.schedule(task, 30000, 30000);
	}


	//绘制障碍物,即圆柱
	public void drawObstacle(Canvas canvas) {
		// System.out.println("drawDownObstacle");
		if (left1 > -bitmap_Obstacle_Width) {
			rectF1 = new RectF(left1, top, left1 + bitmap_Obstacle_Width,
					mScreenHeight - temp1);
			rectF2 = new RectF(left1, mScreenHeight - temp1 + 100, left1
					+ bitmap_Obstacle_Width, mScreenHeight);
			isImpact1 = PengZhuang.isPengZhuang(left1, top, left1 + bitmap_Obstacle_Width, mScreenHeight - temp1,birdManager.getLeft(),birdManager.getTop(),birdManager.getRight(),birdManager.getBottom());
			isImpact2 = PengZhuang.isPengZhuang(left1, mScreenHeight - temp1 + 100, left1
					+ bitmap_Obstacle_Width, mScreenHeight,birdManager.getLeft(),birdManager.getTop(),birdManager.getRight(),birdManager.getBottom());
			
			
		} else {
			System.out.println("1号柱子过界归原");
			left1 = mScreenWidth;
			temp1 = (random.nextInt(randomMax)+1)*100;
			System.out.println("temp1="+temp1);
			rect1 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp1), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
			rect2 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp1-100);

		}
		
		
		if (left2 > -bitmap_Obstacle_Width) {
			rectF3 = new RectF(left2, top, left2 + bitmap_Obstacle_Width,
					mScreenHeight - temp2);
			rectF4 = new RectF(left2, mScreenHeight - temp2 + 100, left2
					+ bitmap_Obstacle_Width, mScreenHeight);
			isImpact3 = PengZhuang.isPengZhuang(left2, top, left2 + bitmap_Obstacle_Width, mScreenHeight - temp2,birdManager.getLeft(),birdManager.getTop(),birdManager.getRight(),birdManager.getBottom());
			isImpact4 = PengZhuang.isPengZhuang(left2, mScreenHeight - temp2 + 100, left2
					+ bitmap_Obstacle_Width, mScreenHeight,birdManager.getLeft(),birdManager.getTop(),birdManager.getRight(),birdManager.getBottom());

		} else {
			System.out.println("2号柱子过界归原");
			left2 = mScreenWidth;
			temp2 = (random.nextInt(randomMax)+1)*100;
			rect3 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp2), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
			rect4 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp2-100);

		}
		
		
		if (left3 > -bitmap_Obstacle_Width) {
			rectF5 = new RectF(left3, top, left3 + bitmap_Obstacle_Width,
					mScreenHeight - temp3);
			rectF6 = new RectF(left3, mScreenHeight - temp3 + 100, left3
					+ bitmap_Obstacle_Width, mScreenHeight);
			isImpact5 = PengZhuang.isPengZhuang(left3, top, left3 + bitmap_Obstacle_Width, mScreenHeight - temp3,birdManager.getLeft(),birdManager.getTop(),birdManager.getRight(),birdManager.getBottom());
			isImpact6 = PengZhuang.isPengZhuang(left3, mScreenHeight - temp3 + 100, left3
					+ bitmap_Obstacle_Width, mScreenHeight,birdManager.getLeft(),birdManager.getTop(),birdManager.getRight(),birdManager.getBottom());

		} else {
			System.out.println("3号柱子过界归原");
			left3 = mScreenWidth;
			temp3 = (random.nextInt(randomMax)+1)*100;
			rect5 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp3), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
			rect6 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp3-100);

		}
		//如果有一个碰撞=true,游戏结束
		if(isImpact1 || isImpact2 || isImpact3 || isImpact4 || isImpact5 || isImpact6){
			isPlay = false;
			birdManager.setIsPlay(false);
		}
		
//		if (left4 > -bitmap_Obstacle_Width) {
//			rectF7 = new RectF(left4, top, left4 + bitmap_Obstacle_Width,
//					mScreenHeight - temp4);
//			rectF8 = new RectF(left4, mScreenHeight - temp4 + 100, left4
//					+ bitmap_Obstacle_Width, mScreenHeight);
//		} else {
//			System.out.println("4号柱子过界归原");
//			left4 = mScreenWidth;
//			temp4 = (random.nextInt(randomMax)+1)*100;
//			rect7 = new Rect(0, bitmap_Obstacle_Height-(int)(mScreenHeight - temp4), bitmap_Obstacle_Width, bitmap_Obstacle_Height);
//			rect8 = new Rect(0, 0, bitmap_Obstacle_Width, (int)temp4-100);
//
//		}

		canvas.drawBitmap(bitmap_down, rect1, rectF1, null);
		canvas.drawBitmap(bitmap_up, rect2, rectF2, null);
		
		canvas.drawBitmap(bitmap_down, rect3, rectF3, null);
		canvas.drawBitmap(bitmap_up, rect4, rectF4, null);
		
		canvas.drawBitmap(bitmap_down, rect5, rectF5, null);
		canvas.drawBitmap(bitmap_up, rect6, rectF6, null);
		
//		canvas.drawBitmap(bitmap_down, rect7, rectF7, null);
//		canvas.drawBitmap(bitmap_up, rect8, rectF8, null);
		
		if(isPlay){			
			left1 = left1 - speedNum;
			left2 = left2 - speedNum;
			left3 = left3 - speedNum;
//		left4 = left4 - 15;
		}

	}
	
	TimerTask task = new TimerTask(){
		public void run() {
			System.out.println("执行task--run");
			speedNum = speedNum+2;
		}
	};

}



碰撞manager

/*四方体碰撞检测
 * 1、通过A,B两个四方体的2个坐标比较,可以检测到是否碰撞
 * Ax1<BX2 && Ay1<By2 && Ax2>BX1 && Ay2>By1
 * 即是A的start点,比B的end点小
 *    A的end点,比B的start点大
 *    符合以上两个条件,就表示碰撞上了
 * */
public class PengZhuang {
	public static boolean isPengZhuang(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4){
			if(x1<x4 && y1<y4 && x2>x3 && y2>y3){
			return true;
		}
		return false;
	}
	
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值