在Breakout游戏碰撞bug上需要帮助

时间:2013-02-25 03:58:18

标签: java swing

这是一项12年级的OPP任务。

目前我有一个问题,即球偶尔会进入禁区而不是反射。

这是我的代码:

主要检查球是否击中了一个区块:

public void theBall(){
    //System.out.println(blocks.size());
    if(!started){
        balls.get(0).withPaddle(paddle.returnX());
    }
    if(started){
        for(int i=0;i<balls.size();i++){
            if(balls.get(i).returnX()>=0 && balls.get(i).returnX()<=800 && balls.get(i).returnY()>=0){
                balls.get(i).move();
            }
            if(balls.get(i).returnX()+balls.get(i).returnDx()<= 0){
                balls.get(i).collide(1);//collide with left wall
            }
            if(balls.get(i).returnX()+10+balls.get(i).returnDx() >= 800){
                balls.get(i).collide(2);//collide with right wall
            }
            if(balls.get(i).returnY()-balls.get(i).returnDy() <= 0){
                balls.get(i).collide(3);//collide with up wall
            }

            if(paddle.returnX() < balls.get(i).returnX() && balls.get(i).returnX()< paddle.returnX()+paddle.returnSize() && balls.get(i).returnY()-balls.get(i).returnDy() >= 545){//fix this && balls.get(i).returnY()-balls.get(i).returnDy() <= 560
                balls.get(i).collidePaddle(balls.get(i).returnX()-paddle.returnX());//collide with paddle
                //System.out.println(ball.returnDy());
            }
            for(int j=0;j<blocks.size();j++){
                //blocks.get(j).getHit(balls.get(i));
                if(balls.get(i).returnX()+10+balls.get(i).returnDx() >= blocks.get(j).returnX() && balls.get(i).returnX()+10+balls.get(i).returnDx() <= blocks.get(j).returnX()+5 && balls.get(i).returnY() >= blocks.get(j).returnY() && balls.get(i).returnY() < blocks.get(j).returnY()+50){
                    if(!blocks.get(j).returnHit()){
                        balls.get(i).collide(2);//collide with right wall (block's left)
                        blocks.get(j).getHit(balls.get(i));
                        System.out.println("boiboiboiboibo1");
                    }
                }

                if(balls.get(i).returnX()+balls.get(i).returnDx() <= blocks.get(j).returnX()+50 && balls.get(i).returnX()+balls.get(i).returnDx() >= blocks.get(j).returnX()+45 && balls.get(i).returnY() >= blocks.get(j).returnY() && balls.get(i).returnY() < blocks.get(j).returnY()+50){
                    if(!blocks.get(j).returnHit()){
                        balls.get(i).collide(1);//collide with left wall (block's right)
                        blocks.get(j).getHit(balls.get(i));
                        System.out.println("boiboiboiboibo2");
                    }
                }

                if(balls.get(i).returnX() >= blocks.get(j).returnX() && balls.get(i).returnX() < blocks.get(j).returnX()+50 && balls.get(i).returnY()-balls.get(i).returnDy() <= blocks.get(j).returnY()+50 && balls.get(i).returnY()-balls.get(i).returnDy() >= blocks.get(j).returnY()+45){
                    if(!blocks.get(j).returnHit()){
                        balls.get(i).collide(3);//collide with up wall (block's bottom)
                        blocks.get(j).getHit(balls.get(i));
                        System.out.println("boiboiboiboibo3");
                    }
                }
                if(balls.get(i).returnX() >= blocks.get(j).returnX() && balls.get(i).returnX() < blocks.get(j).returnX()+50 && balls.get(i).returnY()+10-balls.get(i).returnDy() >= blocks.get(j).returnY() && balls.get(i).returnY()+10-balls.get(i).returnDy() <= blocks.get(j).returnY()+5){
                    if(!blocks.get(j).returnHit()){
                        balls.get(i).collide(4);//collide with down wall (block's top)
                        blocks.get(j).getHit(balls.get(i));
                        System.out.println("boiboiboiboibo4");
                    }
                }
            }
            for(int j=0;j<specialBlocks.size();j++){
                if(balls.get(i).returnX()>=specialBlocks.get(j).returnX() && balls.get(i).returnX()<=specialBlocks.get(j).returnX()+100 && balls.get(i).returnY()>=specialBlocks.get(j).returnY() && balls.get(i).returnY()<=specialBlocks.get(j).returnY()+100){
                    oneAndThreeBalls(i, j);
                    balls.get(i).changeType(j);
                }
            }
        }
    }
}

球如何反映:

public void collide(int collideWith){
    if(collideWith==1){//collide with left wall
        if(90<direction && direction<180){
            direction = 180-direction;
        }
        if(180<direction && direction<270){
            direction = 540-direction;
        }
    }
    if(collideWith==2){//collide with right wall
        if(0<direction && direction<90){
            direction = 180-direction;
        }
        if(270<direction && direction<360){
            direction = 540-direction;
        }
    }
    if(collideWith==3){//collide with up wall
        direction = 360-direction;
    }
    if(collideWith==4){//collide with down wall
        direction = 360-direction;

    }
}

我很清楚这个错误是因为边界的双重检查。当球在一个街区的角落时,它就会发生。但这是我能想到的唯一方法来检查球的反映位置。有关如何修复此错误的任何想法?谢谢。

1 个答案:

答案 0 :(得分:1)

为什么不这样做:

1)在弹跳之后,你停止检查该球的碰撞,直到下一个游戏滴答为止。这样一个球每蜱只能反弹一次。

2)在与一个区块碰撞并反弹之后,你会忽略该特定区块与球之间的碰撞,直到球不与区块重叠为止。这样一来,球就不会在相同的撞击下两次虚假地反弹出来。

顺便说一下,你有很多复制和粘贴的代码,数字只有轻微的变化,等等。如果你找到了一种方法来重新设置方法,你可以使用稍微不同的参数进行调用,那么你的代码将变得更加整洁,问题的根本对称性也将更加明显。