Java突破游戏 - 卸下砖块



我在突破游戏中添加了多行砖,但是现在我已经完成了一旦击中这些砖块的任务。我添加了一个for循环,似乎在中间大致从一排砖头反弹,但没有删除任何砖块。我在班级ActivePart工作。

class ActivePart
{
    private boolean runGame = true;
    public void stop()
    {
        runGame = false;
    }
    public void runAsSeparateThread()
    {
        final float S = 3; // Units to move (Speed) 
        try
        {
            synchronized ( Model.class ) // Make thread safe 
            {
                GameObj       ball   = getBall();     // Ball in game 
                GameObj       bat    = getBat();      // Bat 
                List<GameObj> bricks = getBricks();   // Bricks 
            }
            while (runGame)
            {
                synchronized ( Model.class ) // Make thread safe 
                {
                    float x = ball.getX();  // Current x,y position 
                    float y = ball.getY();
                    // Deal with possible edge of board hit 
                    if (x >= W - B - BALL_SIZE)  ball.changeDirectionX();
                    if (x <= 0 + B            )  ball.changeDirectionX();
                    if (y >= H - B - BALL_SIZE)  // Bottom 
                    { 
                        ball.changeDirectionY(); addToScore( HIT_BOTTOM ); 
                    }
                    if (y <= 0 + M            )  ball.changeDirectionY();
                    // As only a hit on the bat/ball is detected it is  
                    //  assumed to be on the top or bottom of the object. 
                    // A hit on the left or right of the object 
                    //  has an interesting affect 
                    boolean hit = false;
                    // *[3]******************************************************[3]* 
                    // * Fill in code to check if a visible brick has been hit      * 
                    // *      The ball has no effect on an invisible brick          * 
        // ************************************************************** 
        for ( int i = 0; i <= 60; i++ ){
                GameObj brick1 = bricks.get(i);
                if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
                    hit = true;
                    Debug.trace("BreakOut");
                }

            } // here is what i am working on 

        if (hit)
          ball.changeDirectionY();
        if ( ball.hitBy(bat) )
          ball.changeDirectionY();
      }
      modelChanged();      // Model changed refresh screen 
      Thread.sleep( fast ? 2 : 20 );
      ball.moveX(S);  ball.moveY(S);
    }
  } catch (Exception e) 
  { 
    Debug.error("Model.runAsSeparateThread - Errorn%s", 
                e.getMessage() );
  }
}
}
/**
* Model has changed so notify observers so that they
*  can redraw the current state of the game
*/
public void modelChanged()
{
setChanged(); notifyObservers();
}
}

我不知道如何删除游戏中的实际砖头,目前它只是从外观上弹出一条线,也不是实际上也不是砖头。我认为每个单独的行都需要循环。

添加砖块的代码:

bricks = new ArrayList<>();
        // *[1]******************************************************[1]*
        // * Fill in code to place the bricks on the board              *
        // **************************************************************/
        // pink first 2 rows
        for (int i = 0; i < NBRICK_ROWS; i++){
            int y = BRICK_Y_OFFSET + (i * (BRICK_HEIGHT + BRICK_SEP));
            for (int j = 0; j < NBRICKS_PER_ROW; j++){
                int x = (BRICK_X_OFFSET) + (j * (BRICK_WIDTH + BRICK_SEP));
                bricks.add(new GameObj (x, y, BRICK_WIDTH, BRICK_HEIGHT, Colour.PINK));
            }
        }

在" drawactualpicture方法"中的视图类中显示砖头:

for(GameObj brick : bricks) {
            displayGameObj( g, brick);    
        }

这就是我现在拥有的:

if( // ball Y pos < brick Y pos = collision )
                    {
                        brick = null;
                    }
                    for ( int i = 0; i <= 60; i++ ){
                        GameObj brick1 = bricks.get(i);
                        if ( y <= brick1.getY() - (BRICK_HEIGHT/2)){
                            bricks.set(i, null); // The brick in the position of i when the ball collides, will be null
                            hit = true;
                            Debug.trace("BreakOut");
                        }
                        List<GameObj> toRemove = new ArrayList<GameObj>();
                        for(GameObj a: bricks){
                            if(a.getY() <= brick1.getY() - (BRICK_HEIGHT/2)){
                                toRemove.add(a);
                            }
                        }
                        bricks.removeAll(toRemove);

                        if (hit)
                            ball.changeDirectionY();
                        if ( ball.hitBy(bat) )
                            ball.changeDirectionY();
                    }
                    modelChanged();      // Model changed refresh screen 
                    Thread.sleep( fast ? 2 : 20 );
                    ball.moveX(S);  ball.moveY(S);
                }
            } catch (Exception e) 
            { 
            Debug.error("Model.runAsSeparateThread - Errorn%s", 
                e.getMessage() );
        }
    }

为什么不销毁GameObj砖?

if(<ball collide with brick>)
{
    brick = null;
}

for ( int i = 0; i <= 60; i++ ){
   GameObj brick1 = bricks.get(i);
        if ( brick1.hitBy(ball) ){
             bricks.remove(i);
             //hit = true;
             ball.changeDirectionY();
        }
}

相关内容

  • 没有找到相关文章

最新更新