位图之间的碰撞

时间:2018-08-07 16:37:36

标签: java android

我想介绍两个位图(鸟和管)之间的碰撞,但是我找不到在Android中如何做到这一点。我尝试在某处使用可绘制外观,但是它不起作用。我需要以某种方式使管子与鸟相交,如果它检测到碰撞,我将结束比赛。

此后,当小鸟在试管之间通过时,我还需要添加带有标点符号的标签。

public class GameView extends View {
    Handler handler;  
    Runnable runnable;
    final int UPDATE_MILLIS = 30;
    Bitmap background;
    Display display;
    Point point;
    int dWidth, dHeight; 
    Rect rect;
    Bitmap[] birds;
    Bitmap birds;
    int birdFrame = 0;
    int velocity = 0, gravity = 2; 

    // We need to keep track of the bird position
    int birdX;
    int birdY;
    boolean gameState=false;
    int gap = 600; //gap between the bottom tube and the top tube
    int minTubeOffset, maxTubeOffset;
    int numberOfTubes = 4;
    int distanceBetweenTubes;
    int[] tubeX = new int[numberOfTubes];
    int[] topTubeY = new int[numberOfTubes];
    Bitmap topTube, bottomTube;
    Random random;
    int tubeVelocity = 8;
    boolean gameover;

    public GameView(Context context) {
        super(context);
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                invalidate();  // This will call onDraw()
            }
        };

gameover=false;
        background = BitmapFactory.decodeResource(getResources(),R.drawable.background);
        topTube = BitmapFactory.decodeResource(getResources(), R.drawable.toptube);
        bottomTube = BitmapFactory.decodeResource(getResources(), R.drawable.bottomtube);
        display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
        point = new Point();
        display.getSize(point);
        dWidth = point.x;
        dHeight = point.y;
        rect = new Rect(0, 0, dWidth, dHeight);
        birds = new Bitmap[2];
        // birds = new Bitmap();
        birds[0] = BitmapFactory.decodeResource(getResources(), R.drawable.bird);
        birds[1] = BitmapFactory.decodeResource(getResources(), R.drawable.bird);
        birdX = dWidth / 2 - birds[0].getWidth() / 2;
        birdY = dHeight / 2 - birds[0].getHeight() / 2;

        Drawable drawable1 = new BitmapDrawable(context.getResources(), birds[0]);
        Drawable drawable2 = new BitmapDrawable(context.getResources(), topTube);
        Drawable drawable3 = new BitmapDrawable(context.getResources(), bottomTube);

        drawable1.setBounds(100, 100, 400, 400);
        drawable2.setBounds(150, 150, 350, 350);
        drawable3.setBounds(150, 150, 350, 350);

        distanceBetweenTubes = dWidth * 3 / 2;
        minTubeOffset = gap / 2;
        maxTubeOffset = dHeight - minTubeOffset - gap;
        random = new Random();

        for (int i = 0; i < numberOfTubes; i++) {
            tubeX[i] = dWidth + i*distanceBetweenTubes;
            topTubeY[i] = minTubeOffset + random.nextInt(maxTubeOffset - minTubeOffset + 1);
        }

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(background, null, rect, null);

        if (birdFrame == 0) {
            birdFrame = 1;
        } else {
            birdFrame = 0;
        }

        if (gameState) {

        // The bird should be on the screen

        if (birdY < dHeight - birds[0].getHeight() || velocity < 0) { //In this way the bird does not go beyond the bottom edge of the screen

            velocity += gravity; //
            birdY += velocity;
        }
        }
            // We want the bird to be displayed at the centre of the screen
        canvas.drawBitmap(birds[birdFrame], birdX, birdY, null);

        for (int i = 0; i < numberOfTubes; i++) {

            tubeX[i] -= tubeVelocity;

            if(tubeX[i]<-topTube.getHeight()) {

                tubeX[i] += numberOfTubes*distanceBetweenTubes;
                topTubeY[i] = minTubeOffset+random.nextInt(maxTubeOffset-minTubeOffset+1);
            }

            canvas.drawBitmap(topTube, tubeX[i], topTubeY[i] - topTube.getHeight(), null);
            canvas.drawBitmap(bottomTube, tubeX[i], topTubeY[i] + gap, null);
        }

        handler.postDelayed(runnable, UPDATE_MILLIS);

if(drawable1.intersects(drawable2) || drawable1.intersects(drawable3)){
 gameover =true:

}

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {

            // Here we need the bird to move upwards by some unit

            velocity = -20;
            gameState=true;

        }
        return true; 
    }

    public boolean colisiona(){
        System.out.println("Collision!");
        return drawable1.intersects(topTube) || birds.intersects(bottomTube);
    }


}

0 个答案:

没有答案