为数组的每一行分配不同的值

时间:2017-12-15 12:57:24

标签: java arrays

我正在尝试对我的项目进行一些更改,但是当我想根据我用球击中哪一行来更改得分值时,我遇到了问题。如果你点击第一行得分增加5,如果你点击第二行得分增加10,如果你击中第三行得分增加15。我使用二维数组为我的砖矩形public int map [] [];

public void actionPerformed(ActionEvent e) {
    // Starting the timer.
    timer.start();
    // The brain of the game.
    if(play) {
        // Detecting intersection between the ball and the paddle.
        if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerOne, 550, 100, 8))) {
            ballYdir = -ballYdir;
        }

    A:  for(int i = 0; i < map.map.length; i++) {
            for(int j = 0; j < map.map[0].length; j++) {
                if(map.map[i][j] > 0) {
                    int brickX = j * map.brickWidth + 80;
                    int brickY = i * map.brickHeight + 50;
                    int brickWidth = map.brickWidth;
                    int brickHeight = map.brickHeight;
                    // Creating a rectangle that will host the bricks.
                    Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
                    Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20);
                    Rectangle brickRect = rect;
                    // Check if the ball intersects with the bricks. If you hit a brick add 5 points to the score, delete the brick from the frame and change the ball's direction.
                    if(ballRect.intersects(brickRect)) {
                        map.setBrickValue(0, i, j);
                        totalBricks --;
                        score += 5;
                        if(ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) {
                            ballXdir = - ballXdir;
                        }
                        else {
                            ballYdir = - ballYdir;
                        }
                        break A;
                    }
                }
            }
        }

        ballposX += ballXdir;
        ballposY += ballYdir;

        // Conditions that make the ball to interact with the borders and to change the ball's direction.
        // Left border.
        if(ballposX < 0) {
            ballXdir = -ballXdir;
        }
        // Top border.
        if(ballposY < 0) {
            ballYdir = -ballYdir;
        }
        // Right border.
        if(ballposX > 670) {
            ballXdir = -ballXdir;
        }
    }
    // Redraw all of the graphic objects.
    repaint();
}

0 个答案:

没有答案