为什么球的大小不会增加?(游戏)

时间:2015-06-22 01:37:42

标签: java

我试图增加一个乒乓球游戏框架。我希望每次球与球拍接触时球的大小都会增加。

这是我的尝试。第一个代码块是我认为问题所在。第二个街区是全班。

public class PlayState extends State {
private Paddle paddleRight, paddleLeft;
private static final int PADDLE_WIDTH = 30;
private static final int PADDLE_HEIGHT = 60;

private Ball ball;

//bDiam stands for ball diameter

private static int bDiam = 10;

private int playerScore = 0;
private Font scoreFont;

@Override
public void init() {
    paddleLeft = new Paddle(0, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
    paddleRight = new Paddle(785, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
    scoreFont = new Font("SansSerif", Font.BOLD, 25);
    ball = new Ball(300, 200, bDiam, bDiam);
}

@Override
public void onClick(MouseEvent e) {
//  playerScore = 5;


}

@Override
public void update() {
    paddleLeft.update();
    paddleRight.update();
    ball.update();

    if (ballCollides(paddleLeft)) {
        playerScore++;

        bDiam+=  20;
        ball.onCollideWith(paddleLeft);
        Resources.hit.play();
    } else 
        if (ballCollides(paddleRight)) {
        playerScore++;
        bDiam= bDiam + 20;
        ball.onCollideWith(paddleRight);
        Resources.hit.play();
    } else if (ball.isDead()) {
        playerScore -= 3;

        ball.reset();

    }
}

完整班级

public class PlayState extends State {
private Paddle paddleRight, paddleLeft;
private static final int PADDLE_WIDTH = 30;
private static final int PADDLE_HEIGHT = 60;

    private Ball ball;

    //bDiam stands for ball diameter

    private static int bDiam = 10;

    private int playerScore = 0;
    private Font scoreFont;

    @Override
    public void init() {
        paddleLeft = new Paddle(0, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
        paddleRight = new Paddle(785, 195, PADDLE_WIDTH, PADDLE_HEIGHT);
        scoreFont = new Font("SansSerif", Font.BOLD, 25);
        ball = new Ball(300, 200, bDiam, bDiam);
    }

    @Override
    public void onClick(MouseEvent e) {
    //  playerScore = 5;


    }

    @Override
    public void update() {
        paddleLeft.update();
        paddleRight.update();
        ball.update();

        if (ballCollides(paddleLeft)) {
            playerScore++;

            bDiam+=  20;
            ball.onCollideWith(paddleLeft);
            Resources.hit.play();
        } else 
            if (ballCollides(paddleRight)) {
            playerScore++;
            bDiam= bDiam + 20;
            ball.onCollideWith(paddleRight);
            Resources.hit.play();
        } else if (ball.isDead()) {
            playerScore -= 3;

            ball.reset();

        }
    }

    @Override
    public void render(Graphics g) {
        // Draw Background
        g.setColor(Resources.darkBlue);
        g.fillRect(0, 0, GameMain.GAME_WIDTH, GameMain.GAME_HEIGHT);
        g.setColor(Resources.darkRed);
        g.fillRect(GameMain.GAME_WIDTH / 2, 0, GameMain.GAME_WIDTH / 2,
                GameMain.GAME_HEIGHT);
        // Draw Separator Line
        g.drawImage(Resources.line, (GameMain.GAME_WIDTH / 2) - 2, 0, null);

        // Draw Paddles
        g.setColor(Color.white);
     g.fillRect(paddleLeft.getX(), paddleLeft.getY(), paddleLeft.getWidth(),
            paddleLeft.getHeight());
        g.fillRect(paddleRight.getX(), paddleRight.getY(),
                paddleRight.getWidth(), paddleRight.getHeight());

        // Draw Ball
        g.drawRect(ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

        // Draw UI
        g.setFont(scoreFont); // Sets scoreFont as current font
        g.drawString("" + playerScore, 350, 40); // Draws String using current
                                                    // font
    }



    @Override
    public void onKeyPress(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            paddleLeft.accelUp();
            paddleRight.accelUp();
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            paddleLeft.accelDown();
            paddleRight.accelDown();
        }
    }

    @Override
    public void onKeyRelease(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP
                || e.getKeyCode() == KeyEvent.VK_DOWN) {
            paddleLeft.stop();
            paddleRight.stop();
        }
    }

    private boolean ballCollides(Paddle p) {
        return ball.getRect().intersects(p.getRect());
    }
}

1 个答案:

答案 0 :(得分:6)

您的直径变量正在按值传递到Ball构造函数中。这意味着在主程序中更改其值不会为球改变它。

你需要像

这样的东西
ball.setDiameter(ball.getDiameter() +20)