这节主要是处理水管移动,以级水管与小鸟碰撞检测
水管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 = PlayerManager();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.Resources(),R.drawable.downl);bitmap_up = BitmapFactory.Resources(),R.drawable.upl);bitmap_Obstacle_Width = Width();bitmap_Obstacle_Height = Height();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 - Left(),Top(),Right(),Bottom());isImpact2 = PengZhuang.isPengZhuang(left1, mScreenHeight - temp1 + 100, left1+ bitmap_Obstacle_Width, Left(),Top(),Right(),Bottom());} else {System.out.println("1号柱子过界归原");left1 = mScreenWidth;temp1 = (Int(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 - Left(),Top(),Right(),Bottom());isImpact4 = PengZhuang.isPengZhuang(left2, mScreenHeight - temp2 + 100, left2+ bitmap_Obstacle_Width, Left(),Top(),Right(),Bottom());} else {System.out.println("2号柱子过界归原");left2 = mScreenWidth;temp2 = (Int(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 - Left(),Top(),Right(),Bottom());isImpact6 = PengZhuang.isPengZhuang(left3, mScreenHeight - temp3 + 100, left3+ bitmap_Obstacle_Width, Left(),Top(),Right(),Bottom());} else {System.out.println("3号柱子过界归原");left3 = mScreenWidth;temp3 = (Int(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 = (Int(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;}};}
/*四方体碰撞检测* 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;}}
本文发布于:2024-02-01 05:53:23,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170673800334372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |