Brick Breaker游戏中的球破坏整条线而不触碰它们(Java)

时间:2017-06-30 14:44:12

标签: java android collision-detection

我一直在制作一个破砖游戏,但我在执行程序时遇到了一个奇怪的错误。当比赛开始时,球会远离砖块运动。然而,它几乎立即开始摧毁它们,从上排最右边的砖开始,并继续从右到左打破它们。然后它进入第二行并从左到右摧毁它们。整个时间,在屏幕上,球远离任何砖块。

我想我已经将问题确定为以下代码。在这里我创建了一个围绕球的矩形和围绕砖的矩形,如果它们相交,我在另一个名为setBrickValue的类中调用方法Map Generator,该类将砖的值设置为0,意思是,它不存在。

以下是方法:

    public void actionPerformed(ActionEvent arg0) {
            timer.start();
            //makes sure the ball collides with the paddle
            if(play){
                if( new Rectangle( ballPositionX, ballPositionY, 20, 20)
                        .intersects( new Rectangle( player, 550, 100, 8 ) ) ){
                    ballDirY = - ballDirY;
                }
                // beginning of the code with an error in it, the rectangle around the ball
                //is intersecting incorrectly the rectangle of the brick
                A:for(int i = 0; i < MGobj.map.length; i++){
                    for(int j = 0; j < MGobj.map[0].length; j++){
                        if(MGobj.map [i] [j] > 0){
                            int brickX = i * MGobj.brickWidth + 80;
                            int brickY = j * MGobj.brickHeight + 50;
                            int brickWidth = MGobj.brickWidth;
                            int brickHeight = MGobj.brickHeight;

                            //create the rectangle around the brick
                            Rectangle rect = new Rectangle( brickX, brickY,
                                    brickWidth, brickHeight );

                            //create the rectangle around the ball, so we can detect the intersection
                            Rectangle ballRect = new Rectangle( ballPositionX, ballPositionY, 20, 20 );

                            Rectangle brickRect = rect;
                            //when intersecting, delete the brick off the map
                            if( ballRect.intersects( brickRect )){
                                MGobj.setBrickValue( 0, i, j );
                                totalBricks--;
                                score += 5;
                                //
                                if( ballPositionX + 19 >= brickRect.x || 
                                         ballPositionX + 1  <=  brickRect.x + brickRect.width  ){
                                    ballDirX = - ballDirX;
                                }else{
                                    ballDirY = - ballDirY;
                                }
                                //creates a label that says when this code executes, get out of the 
                                //outer loop
                                break A;
                            }
                         }
                      }
                    }
                //end of the code with an error in it


                //makes sure the ball is moving
                ballPositionX += ballDirX;
                ballPositionY += ballDirY;

                //makes sure the ball isn't going beyond the borders
                if(ballPositionX < 0){
                    ballDirX = -ballDirX;
                }
                if(ballPositionY < 0){
                    ballDirY = -ballDirY;
                }
                //think this code doesn't work
                if(ballPositionX > 670){
                    ballDirX = -ballDirX;
                }
            }

            repaint();

        }

以下是setBrickValue方法:

    public void setBrickValue(int value, int row, int col){     
          map [row] [col] = value;  
        }

感谢所有帮助。先感谢您。

PS:如果有人想看到整个代码(400行),请告诉我,我会上传它。

0 个答案:

没有答案
相关问题